数到20
for number in range(1,21):
print(number)
4-4 一百万
numbers = list(range(1,1000001))
for number in numbers:
print(number)
中间用Ctrl + C停止输出。
不打断输出的最后结果:
4-5 计算1~1 000 000的总和
numbers = list(range(1,1000001))
print(min(numbers))
print(max(numbers))
print(sum(numbers))
(将一百万个数字相加比起打印一百万个数字快许多)
4-6 奇数
odd_numbers = list(range(1,21,2))
for number in odd_numbers:
print(number)
4-7 3的倍数
mutiples_of_3 = list(range(3,31,3))
for number in mutiples_of_3:
print(number)
4-8 立方
cubes = []
for value in range(1,11):
cubes.append(value**3)
print(cubes)
4-9 立方解析
cubes = [value ** 3 for value in range(1,11)]
print(cubes)