How to Create a List of Integers in Python Using the Range Function?

Estimated read time 1 min read

You can create a list of integers in Python using the built-in range() function. The range() function returns a sequence of numbers, and you can convert that sequence into a list using the list() function. Here’s an example:

my_list = list(range(1, 11))
print(my_list)

In this example, the range() function returns a sequence of integers from 1 to 10 (inclusive), and then we use the list() function to convert that sequence into a list. The resulting list my_list contains the integers 1, 2, 3, 4, 5, 6, 7, 8, 9, and 10.

You can also use the range() function to generate a sequence of integers with a specified step value. Here’s an example:

my_list = list(range(0, 20, 2))
print(my_list)

In this example, the range() function returns a sequence of even integers from 0 to 18 (inclusive), with a step value of 2. The resulting list my_list contains the integers 0, 2, 4, 6, 8, 10, 12, 14, 16, and 18.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply