Python range() Function
Example
Create a number sequence from 0 to 5 and print each item in the sequence:
x = range(6) for n in x: print(n)
Definition and Usage
The range() function returns a number sequence, which defaults to start from 0, default increment by 1, and end with the specified number.
Syntax
range(Start, Stop, Step)
Parameter Value
Parameter | Description |
---|---|
Start | Optional. Integer, specifies the starting position. The default is 0. |
Stop | Optional. Integer, specifies the position to end. |
Step | Optional. Integer, specifies the increment. The default is 1. |
More Examples
Example
Create a number sequence from 3 to 7 and print each item in the sequence:
x = range(3, 8) for n in x: print(n)
Example
Create a number sequence from 2 to 19, but increase by 2 instead of 1:
x = range(2, 20, 2) for n in x: print(n)