Python學(xué)習(xí)2

模擬現(xiàn)實世界中的情形

大多數(shù)程序都旨在解決最終用戶的問題

第5章 ?if語句

條件測試

特定值是否包含在列表中 in

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

如果你只想執(zhí)行一個代碼塊溉奕,就使用if-elif-else結(jié)構(gòu)熙侍,如果要運行多個代碼塊吁朦,就使用一系列獨立的if語句柒室。

動手試一試:

#5-3 外星人顏色#1

alien_color='green'

if alien_color=='green':

? ? print('This player got 5 points!')

alien_color='red'

if alien_color=='green':

? ? print('This player got 5 points!')


#2? ?

alien_color='green'

if alien_color=='green':

? ? print('This player got 5 points!')

else:

? ? print('This player got 10 points!')


alien_color='yellow'

if alien_color=='green':

? ? print('This player got 5 points!')

else:

? ? print('This player got 10 points!')?

#3? ?

alien_color='green'

if alien_color=='green':

? ? print('This player got 5 points!')

elif alien_color=='yellow':

? ? print('This player got 10 points!')

else:

? ? print('This player got 15 points!')

alien_color='yellow'

if alien_color=='green':

? ? print('This player got 5 points!')

elif alien_color=='yellow':

? ? print('This player got 10 points!')

else:

? ? print('This player got 15 points!')

alien_color='green'

if alien_color=='red':

? ? print('This player got 5 points!')

elif alien_color=='yellow':

? ? print('This player got 10 points!')

else:

? ? print('This player got 15 points!')? ?

age=32

if age<2:

? ? print('He is a baby!')

elif age>=2 and age<4:

? ? print('He is toddling!')

elif age>=4 and age<13:

? ? print('He is a child!')

elif age>=13 and age<20:

? ? print('He is a teenager!')

elif age>=20 and age<65:

? ? print('He is an adult!')

else:

? ? print('He is an aged man!')


favorite_fruits=['orange','cherry','watermelon']

if 'orange' in favorite_fruits:

? ? print("You really like orange!")

if 'cherry' in favorite_fruits:

? ? print("You really like cherry!")

if 'apple' in favorite_fruits:

? ? print("You really like apple!")

if 'pear' in favorite_fruits:

? ? print("You really like pear!")

if 'watermelon' in favorite_fruits:

? ? print("You really like watermelon!")

5.4 使用if語句處理列表

確定列表不是空的 ?if requested_toppings:

動手試一試:

#5-8 以特殊方式跟管理員打招呼

users=['admin','bing','yang','ling','fei']

for user in users:

? ? if user=='admin':

? ? ? ? print('Hello admin,would you like to see a status report?')

? ? else:

? ? ? ? print('Hello '+user.title()+",thank you for logging in again!")

users=[]

if? users:

? ? for user in users:

? ? ? ? if user=='admin':

? ? ? ? ? ? print('Hello admin,would you like to see a status report?')? ? ? ?

? ? ? ? else:

? ? ? ? ? ? print('Hello '+user.title()+",thank you for logging in again!")

else:

? ? print('We need to find some users!')

current_users=['admin','bing','yang','ling','fei']?

new_users=['wang','Bing','Yang','wen','ma']? ?

for new_user in new_users:

? ? ? ? if new_user.lower()? not in current_users:

? ? ? ? ? ? print(new_user.lower()+',This user is available!')

? ? ? ? else:

? ? ? ? ? ? print(new_user.lower()+',This user is forbidden!')

sequences=list(range(1,10))

for sequence in sequences:

? ? if sequence<=3 :

? ? ? ? if sequence==1 :

? ? ? ? ? ? print('1st')

? ? ? ? if sequence==2 :

? ? ? ? ? ? print('2nd')

? ? ? ? if sequence==3 :

? ? ? ? ? ? print('3rd')? ? ? ? ? ? ? ? ?

? ? else:? ?

? ? ? ? print(str(sequence)+'th')


第6章 字典

鍵值對

alien_0={}

alien_0['color']='green'

刪除鍵值對

del alien_0['points']

動手試一試:

# 6-1 人

people={

? ? ? ? 'first_name':'xia',

? ? ? ? 'last_name':'wang',

? ? ? ? 'age':32,

? ? ? ? 'city':'beijing'

? ? ? ? }

print(people['first_name'])

print(people['last_name'])

print(people['age'])

print(people['city'])

print('first_name\n\t'+people['first_name'])

1、遍歷字典

for key,value in user_0.items():

for name in favorite_language.keys(): ?等價于for name in favorite_language:

for language in favorite_language.values():

集合類似于列表逗宜,但每個元素都必須是獨一無二的

for language in set(favorite_language.values()):

嵌套

1雄右、在列表中存儲字典

for alien_number in range(0,30):

new_alien={}

aliens.append(new_alien)

# 字典列表

aliens=[]

for alien_number in range(0,30):

? ? new_alien={'color':'green','points':5,'speed':'slow'}

? ? aliens.append(new_alien)

for alien in aliens[0:3]:

? ? if alien['color']=='green':

? ? ? ? alien['color']='yellow'

? ? ? ? alien['speed']='medium'

? ? ? ? alien['points']=10

? ? elif alien['color']=='yellow':

? ? ? ? alien['color']='red'

? ? ? ? alien['speed']='fast'

? ? ? ? alien['points']=15

for alien in aliens:

? ? print(alien)

2、在字典中存儲列表

# 2纺讲、在字典中存儲列表

favorite_languages={

? ? ? ? 'jen':['python','ruby'],

? ? ? ? 'sarah':['c'],

? ? ? ? 'edward':['ruby','go'],

? ? ? ? 'phil':['python','haskell']

? ? ? ? }

for name,languages in favorite_languages.items():

? ? if len(languages)>1:

? ? ? ? print(name.title()+"'s favorite languages are:")

? ? else:

? ? ? ? print(name.title()+"'s favorite language is:")

? ? for language in languages:

? ? ? ? print("\t"+language.title())

3擂仍、在字典中存儲字典

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市熬甚,隨后出現(xiàn)的幾起案子逢渔,更是在濱河造成了極大的恐慌,老刑警劉巖乡括,帶你破解...
    沈念sama閱讀 217,542評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件肃廓,死亡現(xiàn)場離奇詭異智厌,居然都是意外死亡,警方通過查閱死者的電腦和手機盲赊,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,822評論 3 394
  • 文/潘曉璐 我一進店門铣鹏,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人角钩,你說我怎么就攤上這事吝沫∩肜剑” “怎么了递礼?”我有些...
    開封第一講書人閱讀 163,912評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長羹幸。 經(jīng)常有香客問我脊髓,道長,這世上最難降的妖魔是什么栅受? 我笑而不...
    開封第一講書人閱讀 58,449評論 1 293
  • 正文 為了忘掉前任将硝,我火速辦了婚禮,結(jié)果婚禮上屏镊,老公的妹妹穿的比我還像新娘依疼。我一直安慰自己,他們只是感情好而芥,可當(dāng)我...
    茶點故事閱讀 67,500評論 6 392
  • 文/花漫 我一把揭開白布律罢。 她就那樣靜靜地躺著,像睡著了一般棍丐。 火紅的嫁衣襯著肌膚如雪误辑。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,370評論 1 302
  • 那天歌逢,我揣著相機與錄音巾钉,去河邊找鬼秘案。 笑死,一個胖子當(dāng)著我的面吹牛阱高,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播辟癌,決...
    沈念sama閱讀 40,193評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼黍少,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了厂置?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,074評論 0 276
  • 序言:老撾萬榮一對情侶失蹤智绸,失蹤者是張志新(化名)和其女友劉穎访忿,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體迹恐,經(jīng)...
    沈念sama閱讀 45,505評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡殴边,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,722評論 3 335
  • 正文 我和宋清朗相戀三年锤岸,在試婚紗的時候發(fā)現(xiàn)自己被綠了板乙。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,841評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡晓猛,死狀恐怖凡辱,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情洪燥,我是刑警寧澤乳乌,帶...
    沈念sama閱讀 35,569評論 5 345
  • 正文 年R本政府宣布汉操,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏搜变。R本人自食惡果不足惜针炉,卻給世界環(huán)境...
    茶點故事閱讀 41,168評論 3 328
  • 文/蒙蒙 一篡帕、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧拢军,春花似錦拌滋、人聲如沸猜谚。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,783評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽览芳。三九已至,卻和暖如春铸敏,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背悟泵。 一陣腳步聲響...
    開封第一講書人閱讀 32,918評論 1 269
  • 我被黑心中介騙來泰國打工糕非, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人禁筏。 一個月前我還...
    沈念sama閱讀 47,962評論 2 370
  • 正文 我出身青樓衡招,卻偏偏與公主長得像,于是被迫代替她去往敵國和親始腾。 傳聞我的和親對象是個殘疾皇子舀射,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,781評論 2 354

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