- range 函数.是python中创建范围的内置函数。
范围类似于切片。它们包含起始位置(这里为0),但不包含结束位置(这里为10)。还可以指定范围,如range(1,5)
>>> list(range(5))
[0, 1, 2, 3, 4]
>>> list(range(1,5))
[1, 2, 3, 4]
for i in range(5):
print("hello")
for i in range(5):
print("hello", i)
➜ pythoncode python3 holly_03_17.py
hello
hello
hello
hello
hello
hello 0
hello 1
hello 2
hello 3
hello 4
numbers = [1,2,3,4,5]
for number in numbers:
print(number)
➜ pythoncode python3 holly_03_17.py
1
2
3
4
5
x = 1
while x <=5:
print(x)
x += 1
pythoncode python3 holly_03_17.py
1
2
3
4
5