range( ) does not create a list of numbers — it creates a range object (an iterable sequence) which is memory-efficient.
syntax :
Python
range(stop) range(start, stop) range(start, stop, step) | Parameter | Meaning |
|---|---|
| start | starting value (default = 0) |
| stop | ending value (not included) |
| step | difference between values (default = 1) |
1. range(stop) :
Here , the range starts from 0. The stop value is not Included.
Python
for i in range(4): print(i) #output 0 1 2 3 2. range(start, stop) :
The range starts at start value and ends at stop - 1 value.
Python
for i in range(2, 7): print(i) #output 2 3 4 5 6 3. range(start, stop, step) :
Python
for i in range(1, 10, 2): print(i) #output 1 3 5 7 9 4. negative step :
Python
for i in range(10, 0, -2): print(i) #output 10 8 6 4 2 5. Using List to display Ranges :
Python
print(list(range(5))) #output [0, 1, 2, 3, 4]