一、For循环
>>> singers = ['奕迅','Jay','Bigbang','Jj','邓紫棋']
>>> for singer in singers:
print(singer)
奕迅
Jay
Bigbang
Jj
邓紫棋
>>> for singer in singers:
print("我最喜欢爱的歌手是:" + singer.title())
print("我们期待 " + singer.title() + " 的再一次演唱会" + "。\n")
我最喜欢爱的歌手是:奕迅
我们期待 奕迅 的再一次演唱会。
我最喜欢爱的歌手是:Jay
我们期待 Jay 的再一次演唱会。
我最喜欢爱的歌手是:Bigbang
我们期待 Bigbang 的再一次演唱会。
我最喜欢爱的歌手是:Jj
我们期待 Jj 的再一次演唱会。
我最喜欢爱的歌手是:邓紫棋
我们期待 邓紫棋 的再一次演唱会。
二、range(start, stop,step),该函数是非常实用创建数字集,例如一些数字集初始化等
>>> for value in range(1,10,2):
print(value)
1
3
5
7
9
>>> numbers = list(range(1,6))
>>> print(numbers)
[1, 2, 3, 4, 5]
>>> numbers = list(range(2,11,2))
>>> print(numbers)
[2, 4, 6, 8, 10]
>>>squares = []
>>>for value in range(1,11):
square = value ** 2
squares.append(square)
print(squares)
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
>>> squares = [value ** 2 for value in range(1,11)]
>>> print(squares)
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
>>> num = [1,2,3,4,5,6,7,8,9,0]
>>> min(num)
0
>>> max(num)
9
>>> sum(num)
45
切片,用于处理列表的所有元素或部分元素
一、切片基本操作
>>> players = ['charles','martina','michael','florence','eli']
>>> print(players[0:3])
['charles', 'martina', 'michael']
>>> print(players[1:5])
['martina', 'michael', 'florence', 'eli']
>>> print(players[:4])
['charles', 'martina', 'michael', 'florence']
>>> print(players[2:])
['michael', 'florence', 'eli']
>>> print(players[-3:])
['michael', 'florence', 'eli']
二、遍历切片
1.for循环遍历切片
>>> for player in players[:3]:
print(player.title())
Charles
Martina
Michael
2.复制列表
>>> my_foods = ['牛肉','鸡肉','猪蹄']
>>> friend_foods = my_foods[:]
>>> print(my_foods)
['牛肉', '鸡肉', '猪蹄']
>>> print(friend_foods)
['牛肉', '鸡肉', '猪蹄']
>>>
>>> my_foods.append('冰激凌')
>>> friend_foods.append('油泼面')
>>> print(my_foods)
['牛肉', '鸡肉', '猪蹄', '冰激凌']
>>> print(friend_foods)
['牛肉', '鸡肉', '猪蹄', '油泼面']
一、元组
元组与列表在组成上较相似,区别在于:
1、() 符号
2、元组元素不可变,即称为不可变列表
所以,元组多用于整个生命周期内都不变的元素
二、元组操作
>>> dimensions = (200,50)
>>> print(dimensions[0])
>>>> print(dimensions[1])
200
50
>>> dimensions[0] = 250
Traceback (most recent call last):
File "<pyshell#12>", line 1, in <module>
dimensions[0] = 250
TypeError: 'tuple' object does not support item assignment
>>>
练习题
for num in range(1,21):
print(num)
million_nums = list(range(1,1000000))
for million_num in million_nums:
print(million_nums)
million_nums = list(range(1,1000001))
print(min(million_nums))
print(max(million_nums))
print(sum(million_nums))
oddnums = list(range(1,21,2))
for oddnum in oddnums:
print(oddnum)
nums = list(range(3,31,3))
for num in nums:
print(num)
nums = list(range(1,11))
for num in nums:
cubenum = num**3
print(cubenum)
nums = [num**3 for num in range(1,11)]
for x in nums:
print(x)
#4-10
#切片 :选择你在本章编写的一个程序,在末尾添加几行代码,以完成如下任务。
#打印消息“The first three items in the list are:”,再使用切片来打印列表的前三个元素。
#打印消息“Three items from the middle of the list are:”,再使用切片来打印列表中间的三个元素。
#打印消息“The last three items in the list are:”,再使用切片来打印列表末尾的三个元素。
nums = list(range(1,11))
print(nums)
#The first three items in the list are:
three_num = nums[0:3]
print("The first three items in the list are:" + str(three_num))
#Three items from the middle of the list are:
middle = len(nums)
middle_num = nums[middle -2:middle+1]
print("Three items from the middle of the list are:" + str(middle_num))
#The last three items in the list are:
last_num = nums[-3:]
print("The last three items in the list are:" + str(last_num))
#4-11
#你的比萨和我的比萨 :在你为完成练习4-1而编写的程序中,创建比萨列表的副本,并将其存储到变量friend_pizzas 中,再完成如下任务。
#在原来的比萨列表中添加一种比萨。
#在列表friend_pizzas 中添加另一种比萨。
#核实你有两个不同的列表。为此,打印消息“My favorite pizzas are:”,再使用一个for 循环来打印第一个列表;打印消息“My friend's favorite pizzas are:”,再使用一
#个for 循环来打印第二个列表。核实新增的比萨被添加到了正确的列表中。
mypizzas = ['beefpizza','hampizza','saladpizza']
#利用切片进行拷贝列表,不会改变原有列表元素
friend_pizzas = mypizzas[:]
friend_pizzas.append('chickenpizza')
print("My favorite pizzas are:")
for mypizza in mypizzas:
print(mypizza)
print("My friend's favorite pizzas are:")
for friend_pizza in friend_pizzas:
print(friend_pizza)
#4-12
#使用多个循环 :在本节中,为节省篇幅,程序foods.py的每个版本都没有使用for 循环来打印列表。请选择一个版本的foods.py,在其中编写两个for 循环,将各个食品列表都打印出来。
my_foods = ['pizza','falafel','carrot cake']
friend_food = my_foods[:]
print("My favorite foods are:")
for food in my_foods:
print(food)
print("My friend's favorite foods are:")
for food in friend_food:
print(food)
cafeterias = ('apple','banana','orange','pea','peach')
for cafeteria in cafeterias:
print(cafeteria)
"""
cafeterias[0] = 'coffe'
Traceback (most recent call last):
File "C:/Users/Administrator/PycharmProjects/untitled/test_dog.py", line 9, in <module>
cafeterias[0] = 'coffe'
TypeError: 'tuple' object does not support item assignment
"""
print("\nchange the tuple:\n")
cafeterias = ('coofe','chicken','orange','pea','peach')
for cafeteria in cafeterias:
print(cafeteria)
4-14 PEP 8 :请访问https:
4-15 代码审核 :从本章编写的程序中选择三个,根据PEP 8指南对它们进行修改。
每级缩进都使用四个空格。对你使用的文本编辑器进行设置,使其在你按Tab键时都插入四个空格;如果你还没有这样做,现在就去做吧(有关如何设置,请参
阅附录B)。
每行都不要超过80字符。对你使用的编辑器进行设置,使其在第80个字符处显示一条垂直参考线。
不要在程序文件中过多地使用空行。
略.......