昨天下午又看了一遍第4章,今天早上自己来个总结吧。
复习内容如下:
通过 for 循环遍历表中内容以及在循环中打印和循环外打印
rang() 创建列表和设置步长
数字列表的简单统计
1)mix 最小
2)max 最大
3)sum 和
C语言风格的运算加 for 循环
列表中元素的切片,赋值列表
遍历元组以及元组的重新赋值
通过 for 循环遍历列表中的内容
---------------------------------------------------
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
print(magician)
---------------------------------------------------
alice
david
carolina
在 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")
------------------------------------------------------------------------------
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.
在 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!
range()创建数值列表
这里的 range() 只打印数字 1~4,range() 会在达到指定的第二个值时停止打印
----------------------------------------
for value in range(1,5):
print(value)
----------------------------------------
1
2
3
4
使用 range() 创建数字列表
------------------------------------
numbers = list(range(1,6))
print(numbers)
------------------------------------
[1, 2, 3, 4, 5]
range() 还可以在第三位指定步长
第三位指定 2,表示数字不断 +2 打印
--------------------------------------------
numbers = list(range(1,11,2))
print(numbers)
--------------------------------------------
[1, 3, 5, 7, 9]
将前 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]
对数字列表执行简单的统计
min 最小
max 最大
sum 和
--------------------------------------------
digits = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
print(min(digits))
print(max(digits))
print(sum(digits))
--------------------------------------------
0
9
45
C 语言风格的列表解析
------------------------------------------------------------
squares = [ value**2 for value in range(1,11) ]
print(squares)
------------------------------------------------------------
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
使用列表的一部分(切片)
取出列表的前三个元素
-----------------------------------------------------------------------
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[0:3])
------------------------------------------------------------------------
['charles', 'martina', 'michael']
取出列表的第 2~4 个元素
-----------------------------------------------------------------------
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[1:4])
-----------------------------------------------------------------------
['martina', 'michael', 'florence']
如果没有指定索引,python 会自动从头开始切
-----------------------------------------------------------------------
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']
元组
元组是用圆括号来标识
定义元组后可以用索引来访问其元素,就像访问列表元素一样
不能给元组中的元素赋值
------------------------------
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 = (100, 500)
print("\nModified dimensions:")
for dimension in dimensions:
print(dimension)
----------------------------------------------
Original dimensions:
200
50
Modified dimensions:
100
500