14天Python编程从入门到实践--Day5:操作列表
2018-03-19 本文已影响19人
想飞了
#4.1
foods = ['cake','noodle','rice']
for food in foods:
print('I like:' + food)
print('I really love food!')
#4.2
pets = ['狗','猫','猪']
for pet in pets:
print(pet + '是一种很棒的宠物!')
print('这些动物都是很棒的宠物!')
~~~~~~~~~~~~~~~~~~~~~~~~~~
I like:cake
I like:noodle
I like:rice
I really love food!
狗是一种很棒的宠物!
猫是一种很棒的宠物!
猪是一种很棒的宠物!
这些动物都是很棒的宠物!
#4.3
for i in range(1,21):
print(i)
#4.4
nums = list(range(1,1000001))
for i in nums:
print(i)
#4.5
print(min(nums))
print(max(nums))
print(sum(nums))
~~~~~~~~~~~~~~~~~
1
1000000
500000500000
#4.6
odds = list(range(1,21,2))
for odd in odds:
print(odd)
~~~~~~~~~~~~~~~~~~
1
3
5
7
9
11
13
15
17
19
#4.7
lst3s = list(range(3,31,3))
for lst3 in lst3s:
print(lst3)
~~~~~~~~~~~~~~~~~~~~~~
3
6
9
12
15
18
21
24
27
30
#4.8
cubes = []
for value in range(1,11):
cube = value ** 3
print(cube)
cubes.append(cube)
print(cubes)
~~~~~~~~~~~~~~~~~~~~~~
1
8
27
64
125
216
343
512
729
1000
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]