python基礎(chǔ)(三)

1、使用列表的一部分

? ? 1.1 切片

????players=['charles','martina','michael','florence','eli']

????print(players[0:3])? ? //切片,指定索引,與range()函數(shù)一樣

????print(players[:4])? ? //沒有指定第一個(gè)索引,python自動(dòng)從列表開頭開始

????print(players[2:])? ? //沒有指定第二個(gè)索引,python自動(dòng)到列表末尾

????print(players[-3:])? ? //負(fù)數(shù)索引返回離列表末尾相應(yīng)距離的元素

? ? -->['charles', 'martina', 'michael']

????????['charles', 'martina', 'michael', 'florence']

????????['michael', 'florence', 'eli']

? ??????['michael', 'florence', 'eli']

? ? 1.2 遍歷切片

????for player in players[:3]:? ? //用for循環(huán)使用切片

????????print(player)

? ? -->charles

????????martina

????????michael

? ? 1.3 復(fù)制列表

????my_foods=['pizza','falafel','carrot cake']

????friend_foods=my_foods[:]? ? //創(chuàng)建列表的副本

????my_foods.append('cannoli')? ? //在原列表中添加一個(gè)元素

????friend_foods.append('ice cream')? ? //在副本列表中添加一個(gè)元素

????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']

? ? 1.4 列表賦值

????friend_foods=my_foods? ? //兩個(gè)變量指向同一個(gè)列表

? ? -->My favorite foods are:

????????['pizza', 'falafel', 'carrot cake', 'cannoli']

????????My friend's favorite foods are:

????????['pizza', 'falafel', 'carrot cake', 'cannoli']

2、元組

? ? 2.1 定義元組

????dimensions=(200,50)? ? //元組使用圓括號(hào)標(biāo)識(shí)

????print(dimensions[0])? ? //訪問語法與列表語法相同

????print(dimensions[1])

? ? -->200

????????50

????注意:dimensions[0]=250 ? //禁止修改元組元素

? ? 2.2 遍歷元組中的所有值

????for di in dimensions:? ? //使用for循環(huán)

????????print(di)

? ? -->200

????????50

? ? 2.3 修改元組變量

????dimensions=(400,100)? ? //給變量重新賦值

? ? 注意:如果需要存儲(chǔ)的一組值在程序的整個(gè)生命周期內(nèi)都不變,可使用元組

3、if語句

? ? 3.1 一個(gè)簡單示例

????cars=['audi','bmw','subaru','toyota']

????for car in cars:

????if car=='bmw':? ? //不能漏掉后面的冒號(hào)

????????print(car.upper())

????else:? ? //else也同理

????????print(car.title())

? ? 3.2 條件測試

? ??car='fdf'? ? //一個(gè)等號(hào)是陳述

????car=='Fdf'? ? //兩個(gè)等號(hào)是發(fā)問,Python中區(qū)分大小寫

? ? -->False

? ? 3.3 檢查不相等

????requested_topping='mushroom'

????if requested_topping!='anchovies':? ? //!=表示不相等

????????print("Hold the anchovies!")

? ? -->Hold the anchovies!

? ? 注意:數(shù)字同理

? ? 3.4 檢查多個(gè)條件

????age1=22

????age2=18

????age1>=21 and age2>=21? ? //使用and檢查

????age1>=21 or age2>=21? ? //使用or檢查

? ? -->False

????????True

????3.5 檢查特定值是否包含在列表中

????requested_topping=['mushrooms','onions','pineapple']

????'mushrooms' in requested_topping? ? //用關(guān)鍵字in判斷是否包含特定值

? ? -->True

? ? 3.6 檢查特定值是否不包含在列表中

????banned_users=['andrew','carolina','david']

????user='marie'

????if user not in banned_users:? ? //使用not in關(guān)鍵字,檢查特定值不在列表中

????????print(user.title()+",you can post a response if you wish.")

? ? -->Marie,you can post a response if you wish.

? ? 3.7 布爾表達(dá)式

? ? 布爾表達(dá)式的結(jié)果要么為True,要么為False

? ? 3.8 if-elif-else結(jié)構(gòu)

????age=20

????if age<4:

????????print("Your admission cost is $0.")

????elif age<18:? ? ? ? ? ? //多條件判斷時(shí)用elif語句

????????print("Your admission cost is $5.")

????else:

????????print("Your admission cost is $10.")

? ? -->Your admission cost is $10.

4、字典

? ? 4.1 一個(gè)簡單的字典

????alien_0={'color':'green','points':5}? ? //該字典存儲(chǔ)了兩條信息,字典是一系列鍵-值對,每個(gè)鍵都與一個(gè)值相關(guān)聯(lián),鍵與值之間用冒號(hào)分隔,而鍵-值對之間用逗號(hào)分隔

????print(alien_0['color'])? ? //打印第一條信息

????print(alien_0['points'])? ? //打印第二條信息

????-->green

????????5

? ? 4.2 添加鍵-值對

????alien_0['x-position']=0? ? // 直接賦值,用方括號(hào)括起的鍵以及與該鍵相關(guān)聯(lián)的值

????alien_0['y=position']=25

????print(alien_0)

? ? -->{'color': 'green', 'points': 5, 'x-position': 0, 'y=position': 25}? ? //? ? python不關(guān)心鍵-值對的添加順序,只關(guān)心鍵和值之間的關(guān)聯(lián)關(guān)系

? ? 4.3 刪除鍵-值對

? ??del alien_0['points']? ? //用del語句刪除

????print(alien_0)

? ? -->{'color': 'green', 'x-position': 0, 'y=position': 25}

? ? 注意:刪除的鍵-值對永遠(yuǎn)消失了

????4.4 由類似對象組成字典

? ??favorite_language={? ? //用字典來存儲(chǔ)眾多對象的同一種信息

????????'jen':'python',

????????'sarah':'c',

????????'edward':'ruby',

????????'phil':'python',

????}

????print("Sarah's favorite landuage is "+favorite_language['sarah'].title()+".")

? ? -->Sarah's favorite landuage is C.

? ? 4.5 遍歷字典

????user_0={

????????'username':'efermi',

????????'first':'enrico',

????????'last':'fermi',

????}

????for key,value in user_0.items(): //for循環(huán) 包含字典名user_0和方法名items(),返回的是鍵-值對列表

????????????print("\nkey:"+key+"\nvalue:"+value)

? ? -->key:username

????????value:efermi

????????key:first

????????value:enrico

????????key:last

????????value:fermi

? ? 注意:for k,v in user_0.items()? ? //遍歷字典時(shí),返回的順序也與存儲(chǔ)順序不同

? ? 4.6 遍歷字典中的所有鍵

? ??for name in favorite_language.keys():? ? //for循環(huán)中用字典名.方法名keys()取所有鍵值

????????print(name.title())

? ? -->Jen

????????Sarah

????????Edward

????????Phil

? ? 4.7按順序遍歷字典中的所有鍵

????for name in sorted(favorite_language.keys()):? ? //使用sorted()函數(shù)

????????print(name.title()+" ,thank you for taking the poll!")

? ? 4.8 遍歷字典中的所有值

? ??for language in favorite_language.values():? ? //用字典名.方法名values()

????????print(language)

? ? -->python

????????c

????????ruby

????????python

? ? 4.9 set()函數(shù)剔除重復(fù)值

? ??for language in set(favorite_language.values()):? ? //調(diào)用set()函數(shù)創(chuàng)建集合,每個(gè)元素獨(dú)一無二

????????print("\n"+language)

? ? -->c

????????ruby

????????python

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容