动手试一试
1.将三种食物存放在一个列表里,再使用for循环将每种食物打印出来
foods = ['beaf','tomato','potato']
for food in foods:
print(food)
结果
beaf
tomato
potato
1.1 修改for循环,使其打印包含食物名称的句子,对每种食物都显示一行指定内容
foods = ['beaf','tomato','potato']
for food in foods:
print(food.title()+"is toady's food")
结果
Beafis toady's food
Tomatois toady's food
Potatois toady's food
1.2 在程序末尾添加一行代码,它不在for循环中,指出你多喜欢美食,并包含每种食物名称和一个总结性句子
foods = ['beaf','tomato','potato']
for food in foods:
print(food.title())
print('All I like!')
结果
Beaf
Tomato
Potato
All I like!
2.想出三种有共同特征的动物,将这些动物的名称存储在一个列表中,再使用for循环将每种动物的名称都打印出来
animals = ['pandas','pig','monkey']
for baby in animals:
print(baby.title())
结果
Pandas
Pig
Monkey
2.1修改for循环,使每一种动物都加上同一条句子
animals = ['pandas','pig','monkey']
for baby in animals:
print(baby.title()+' is so cute!')
结果
Pandas is so cute!
Pig is so cute!
Monkey is so cute!
2.2在程序末尾添加一行代码,指出这些动物的共同之处,如打印诸如Any of these animals would make a great pet!
animals = ['pandas','pig','monkey']
for baby in animals:
print(baby.title()+' is so cute!')
print('Any of these animals would make a great pet!')
结果
Pandas is so cute!
Pig is so cute!
Monkey is so cute!
Any of these animals would make a great pet!
3.使用一个for循环打印数字1~20(包含)
for num in range(1,21):
print(num)
4.创建一个列表其中包含数字1~10001,再使用一个for循环将这些数字打印出来
numlist=[]
for num in range(1,10001):
numlist.append(num)
print(numlist)
5.计算1~10001:创建一个列表1-10001,再使用一个min()和max()核实该列表确实是从1开始到10001结束,另外用sum()计算
numlist=[]
for num in range(1,10001):
numlist.append(num)
print(min(numlist),max(numlist),sum(numlist))
结果
1 10000 50005000
6.奇数:通过函数range()指定第三个参数来创建一个列表,其中包含1-20的奇数,再用一个for循环将这些数字打印出来
value = list(range(1,21,2))
#print(value)
for num in value:
print(num)
7.3的倍数:创建一个列表其中包含3-30内能被3整除的数字,再使用一个for循环将这个列表中的数字都打印出来
values = [value*3 for value in range(3,11)]
print(values)
结果:
[9, 12, 15, 18, 21, 24, 27, 30]
8.立方:将同一个数字乘三次成为立方,创建一个列表其中包含钱10个整数的立方,再用for循环将这些立方数打印出来
list = []
for value in range(1,11):
value = value ** 3
list.append(value)
for num in list:
print(num)
9.立方解析:使用一个列表解析生成一个列表其中包含前10个整数的立方
values = [value**3 for value in range(1,11)]
for num in values:
print(num)
10.切片:
list =['beaf','tomato','potato','chicken','fruits','vegetable','nooddle']
print('The first three items in the list are: '+ str(list[:3]))
print('The first three items in the list are: '+ str(list[2:5]))
print('The first three items in the list are: '+ str(list[-3:]))
结果
The first three items in the list are: ['beaf', 'tomato', 'potato']
The first three items in the list are: ['potato', 'chicken', 'fruits']
The first three items in the list are: ['fruits', 'vegetable', 'nooddle']
11.你的食物和我的食物:
mylist =['beaf','tomato','potato','chicken','fruits','vegetable']
yourlist = mylist[:]
#print(yourlist) ['beaf', 'tomato', 'potato', 'chicken', 'fruits', 'vegetable']
mylist.append('apple')
#print(mylist) ['beaf', 'tomato', 'potato', 'chicken', 'fruits', 'vegetable', 'apple']
yourlist.append('watermelon')
#print(yourlist) ['beaf', 'tomato', 'potato', 'chicken', 'fruits', 'vegetable', 'watermelon']
print('mylist are: ')
print(mylist)
print('yourlist are: ')
print(yourlist)
结果
mylist are:
['beaf', 'tomato', 'potato', 'chicken', 'fruits', 'vegetable', 'apple']
yourlist are:
['beaf', 'tomato', 'potato', 'chicken', 'fruits', 'vegetable', 'watermelon']
12.使用多个循环:
mylist =['beaf','tomato','potato','chicken','fruits','vegetable']
yourlist = mylist[:]
#print(yourlist) ['beaf', 'tomato', 'potato', 'chicken', 'fruits', 'vegetable']
mylist.append('apple')
#print(mylist) ['beaf', 'tomato', 'potato', 'chicken', 'fruits', 'vegetable', 'apple']
yourlist.append('watermelon')
#print(yourlist) ['beaf', 'tomato', 'potato', 'chicken', 'fruits', 'vegetable', 'watermelon']
for value in mylist:
print(value)
for food in yourlist:
print(food)
13.自助餐:
有一家自助式餐馆只提供5种简单的食物,存储元组中。
13.1使用一个for循环将该参观提供的五种食物打印出来
mylist =('beaf','tomato','potato','chicken','fruits','vegetable')
for value in mylist:
print(value)
# beaf
# tomato
# potato
# chicken
# fruits
# vegetable
13.2尝试修改其中的一个元素,核实python会拒绝
mylist =('beaf','tomato','potato','chicken','fruits','vegetable')
mylist[2] = 'e'
print(mylist)
#TypeError: 'tuple' object does not support item assignment
13.3给元组变量赋值并使用一个for循环将新的元组的每个元素都打印出来
mylist = ('beaf','tomato','potato','chicken','fruits','vegetable')
print('original mylist: ',mylist)
mylist = ('beaf','vegetable')
print('modified mylist: ',mylist)
#original mylist: ('beaf', 'tomato', 'potato', 'chicken', 'fruits', 'vegetable')
#modified mylist: ('beaf', 'vegetable')
小结:
在本章我学习了如何高效地处理列表的元素;如何用for循环遍历列表,python如何根据缩进来确定程序的结构以及如何避免一些常见的缩进错误;如何创建简单的数字列表以及对数字列表执行一些操作;如何通过切片来使用列表的一部分和复制列表;学习了元组(对不应变化的值提供了一些程度的保护)