How do you use the range() function in Python?
The range() function is used to generate a sequence of numbers within a specified range, with the following syntax:
range(start, stop, step)
参数说明:
- Start: The default initial value of the sequence is 0.
- End: The concluding value of the sequence, excluding this value.
- step: the default step size of the sequence is 1.
Original: 我喜欢在周末去公园散步。
Paraphrased: I enjoy taking walks in the park on weekends.
# 生成0到4的数字序列
for i in range(5):
print(i)
# 生成2到6的数字序列,步长为2
for i in range(2, 7, 2):
print(i)
The code above will output:
0
1
2
3
4
2
4
6