本文记录是自己这个小菜鸡Python学习笔记
《Python编程从入门到实践》第四章
#第4章 操作列表
#4.1 遍历整个列表
#for循环打印列表中所有元素
#for循环的使用
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
print(magician)
'''
alice
david
carolina
'''
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
print(magician.title() + ", that was a great trick!")
'''
Alice, that was a great trick!
David, that was a great trick!
Carolina, that was a great trick!
'''
#在for循环结束后执行一些操作
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
print(magician.title() + ", that was a great trick!")
print("I can't wait to see your next trick, " + magician.title() + ".\n")
print("Thank you, everyone. That was a great magic show!")
'''
Alice, that was a great trick!
I can't wait to see your next trick, Alice.
David, that was a great trick!
I can't wait to see your next trick, David.
Carolina, that was a great trick!
I can't wait to see your next trick, Carolina.
Thank you, everyone. That was a great magic show!
'''
#4.2 避免缩进错误
#位于for语句后面且属于循环组成部分的代码行,一定要缩进
#忘记for循环缩进,会产生报错
#**********************************************
#magicians = ['alice', 'david', 'carolina']
#for magician in magicians:
#print(magician)
#**********************************************
#忘记缩进额外的代码行,结果可能会出乎意料
#不必要的缩进,会产生报错
#**********************************************
#message = "Hello Python world!"
# print(message)
#**********************************************
#遗漏了冒号
#**********************************************
#magicians = ['alice', 'david', 'carolina']
#for magician in magicians
# print(magician)
#**********************************************
#4.3 创建数值列表
#使用函数range()
#函数range()让你能够轻松地生成一系列的数字
#函数range()从你指定的第一个值开始数,并在到达指定的第二个值后停止,输出不包含第二个值
for value in range(1,5):
print(value)
'''
1
2
3
4
'''
#使用range()创建数字列表
#可使用函数list()将range()的结果直接转换为列表
#如果将range()作为list()的参数,输出将为一个数字列表
numbers = list(range(1,6))
print(numbers)
'''
[1, 2, 3, 4, 5]
'''
#使用函数range()时,还可指定步长
#函数range()从0开始数,然后不断地加2,直到达到或超过终值11
even_numbers = list(range(0,11,2))
print(even_numbers)
'''
[0, 2, 4, 6, 8, 10]
'''
#两个星号**表示乘方运算
squares = []
for value in range(1,11):
square = value**2
squares.append(square)
print(squares)
#更为简洁的代码
'''
squares = []
for value in range(1,11):
squares.append(value**2)
print(squares)
'''
'''
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
'''
#对数字列表执行简单的统计计算
digits = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
min(digits)
max(digits)
sum(digits)
print (min(digits),max(digits),sum(digits))
'''
0,9,45
'''
#列表解析
#首先指定一个描述性的列表名,如squares
#然后,指定一个左方括号,并定义一个表达式,用于生成你要存储到列表中的值
#接下来,编写一个for循环,用于给表达式提供值,再加上右方括号
#注意这里的for语句末尾没有冒号
squares = [value**2 for value in range(1,11)]
print(squares)
'''
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
'''
#4.4 使用列表的一部分
#切片
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[0:3])
'''
['charles', 'martina', 'michael']
'''
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[1:4])
'''
['martina', 'michael', 'florence']
'''
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[:4])
'''
['charles', 'martina', 'michael', 'florence']
'''
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[2:])
'''
['michael', 'florence', 'eli']
'''
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[-3:])
'''
['michael', 'florence', 'eli']
'''
#遍历切片
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print("Here are the first three players on my team:")
for player in players[:3]:
print(player.title())
'''
Here are the first three players on my team:
Charles
Martina
Michael
'''
#复制列表
#复制列表,可创建一个包含整个列表的切片
my_foods = ['pizza', 'falafel', 'carrot cake']
friend_foods = my_foods[:]
print("My favorite foods are:")
print(my_foods)
print("\nMy friend's favorite foods are:")
print(friend_foods)
'''
My favorite foods are:
['pizza', 'falafel', 'carrot cake']
My friend's favorite foods are:
['pizza', 'falafel', 'carrot cake']
'''
my_foods = ['pizza', 'falafel', 'carrot cake']
friend_foods = my_foods[:]
my_foods.append('cannoli')
friend_foods.append('ice cream')
print("My favorite foods are:")
print(my_foods)
print("\nMy friend's favorite foods are:")
print(friend_foods)
'''
My favorite foods are:
['pizza', 'falafel', 'carrot cake', 'cannoli']
My friend's favorite foods are:
['pizza', 'falafel', 'carrot cake', 'ice cream']
'''
#4.5 元组
#定义元组
#元组看起来犹如列表,但使用圆括号()而不是方括号来标识
dimensions = (200, 50)
print(dimensions[0])
print(dimensions[1])
'''
200
50
'''
#遍历元组中所有值
dimensions = (200, 50)
for dimension in dimensions:
print(dimension)
'''
200
50
'''
#修改元组变量
dimensions = (200, 50)
print("Original dimensions:")
for dimension in dimensions:
print(dimension)
dimensions = (400, 100)
print("\nModified dimensions:")
for dimension in dimensions:
print(dimension)
'''
Original dimensions:
200
50
Modified dimensions:
400
100
'''
#错误修改元组dimensions中的一个元素
#***************************************
#imensions = (200, 50)
#dimensions[0] = 250
#***************************************
#60页,动手试一试
dimensions = ('ice cream','drink','fish')
for food in dimensions:
print (food)
#dimensions[0] = milk错误语句试验
dimensions = ('milk','soft drink','fish')
for food in dimensions:
print (food)