Python編程入門到實踐 - 筆記( 4 章)

昨天下午又看了一遍第4章,今天早上本身來個總結吧。python

複習內容以下:git

經過 for 循環遍歷表中內容以及在循環中打印和循環外打印app

rang() 建立列表和設置步長ide

數字列表的簡單統計spa

  1)mix 最小索引

  2)max 最大three

  3)sum 和ci

C語言風格的運算加 for 循環it

列表中元素的切片,賦值列表io

遍歷元組以及元組的從新賦值



經過 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

相關文章
相關標籤/搜索