Python range() Function Explained
The range function is used to create a list of integers, allowing you to specify a start value, stop value, and step size. The basic syntax is:
range(start, stop, step)
- start: initial value, defaulted to 0
- stop: exclusive endpoint
- Step: Default value is 1.
For example, use the range function to generate a list of integers from 0 to 9.
for i in range(10):
print(i)
The output results are:
0
1
2
3
4
5
6
7
8
9
Additionally, you can also specify a starting value and step size, for example generating a list of integers from 1 to 10 with a step size of 2.
for i in range(1, 11, 2):
print(i)
The output result is:
1
3
5
7
9