Holla Tech - Learn

Range
 

The range function creates a sequential list of numbers.
The code below generates a list containing all of the integers, up to 10. 

numbers = list(range(10))
print(numbers

 

Result: 

>>
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>>

 

NOTE!
The call to list is necessary because range by itself creates a range object, and this must be converted to a list if you want to use it as one.

Range
 

If range is called with one argument, it produces an object with values from 0 to that argument.
If it is called with two arguments, it produces values from the first to the second.
For example:

numbers = list(range(3, 8))
print(numbers)

print(range(20) == range(0, 20)) 

 

Result: 

<

p dir=”ltr”>

>>>
[3, 4, 5, 6, 7]

True
>>> 


Range
 

range can have a third argument, which determines the interval of the sequence produced. This third argument must be an integer.

numbers = list(range(5, 20, 2))
print(numbers

 

Result: 

>>
[5, 7, 9, 11, 13, 15, 17, 19] >>> 

 


BACK NEXT

CLICK ON THE BUTTON BELOW TO GO TO THE PYTHON MAIN COURSE PAGE. 

PYTHON MAIN COURSE PAGE

 


©️ License: All Rights Reserved 


CONTACT HOLLA TECH – LEARN SUPPORT