Python之字典及while循環(huán)解讀
1.字典涉及的知識點(diǎn)
1.1 遍歷字典所有的鍵-值對
要點(diǎn):用item()方法
favorite_languages ={ 'jen':'python' 'sarch':'C' 'enward':'ruby' 'phil':'python' } for name, language in favorite_languages.item(): print(name.title() + "'s favorite language is " + languages.title() + ".")
這里的name,以及l(fā)anguage,都是變量,不用非要命名為key,value。
這樣命名可以方便用戶理解。
同時item()方法返回一個鍵-值對列表。
1.2 按順序遍歷字典所有的鍵
要點(diǎn):按順序用sorted()函數(shù),用for語句遍歷整個字典
favorite_languages ={ 'jen':'python' 'sarch':'C' 'enward':'ruby' 'phil':'python' } for name in sorted(favorite_languages.keys()): print(name.titile() + ",thank you for taking the poll.")
這里的for語句類似于其他for語句,但對方法dictionary.keys()的結(jié)果調(diào)用了函數(shù)sorted()。
這使得Python列出了字典中的所有鍵,并在遍歷前對這個列表進(jìn)行排序。
1.3 考慮字典內(nèi)元素是否重復(fù)
要點(diǎn):set()集合類似與列表,確保每個元素獨(dú)一無二。
favorite_languages ={ 'jen':'python' 'sarch':'C' 'enward':'ruby' 'phil':'python' } print("The following languages have been metionted.") for language in set(favorite_languages.value()): print(languages.title())
1.4 嵌套字典列表
要求
1.生成30個外星人;
2.每個外星人包含顏色、生命值、和速度,并輸出;
3.前3個外星人的中,顏色為綠色的外星人,將其顏色更改為黃色;
4.并將對應(yīng)這些外星人的速度改為中速,生命值改為10。
//創(chuàng)建一個用于存儲外星人的空列表 aliens = [] //創(chuàng)建30個綠色的外星人 for alien_number in range(30): new_alien = ['color' = 'green','point' = 5, 'speed' = 'slow'] aliens.append(new_alien) for aliens in range[:3]: if alien['color' = 'green']: alien['color'] = 'yellow' alien['point'] = 10 alien['speed'] = 'medium' //顯示前五個外星人 for alien in aliens[:5]: print(alien) print("...")
1.5 在字典中存儲列表
//存儲所點(diǎn)披薩的信息 pizza = { 'crust': 'thick', 'toppings' = ['mushrooms','extra cheese'], } //概述所點(diǎn)的披薩 print("You order a " + pizza['crust'] + "-crust pizza " + "with the following toppings: ") for topping in pizza['toppings']: print("\t" + topping)
每當(dāng)一個鍵需要關(guān)聯(lián)多個值時,就可以在字典中嵌套一個列表。在字典中存儲字典也是同理。
2.用戶輸入
2.1 input工作原理
input()讓程序暫停運(yùn)行,等待用戶輸入一些文本,當(dāng)用戶按回車鍵后繼續(xù)運(yùn)行。
輸入的變量存儲在message中,當(dāng)print時將值呈現(xiàn)給用戶。
prompt = "If you tell us who you are ,we can personalize the messages you see." prompt += "\nWhat is your first name? "
這是創(chuàng)建多行字符串的格式,運(yùn)算符 += 在存儲在prompt中的字符串在末尾加一個字符串。
2.2 使用int()來獲取數(shù)值輸入
age = input("How old are you?") print('age')
'19'
會看到輸出的age的數(shù)字帶有單引號,屬于字符串類型。
因此當(dāng)此值與其他數(shù)值做比較的時候就會發(fā)生錯誤。
因此使用 age = int(age)
age = input("How old are you?") age = int(age) age >= 18 True
2.3求模運(yùn)算符
判斷數(shù)字是奇是偶
number = input(" Enter a number ,and I'll tell you if it's even or odd:") number = int(number) if number % 2 == 0: print("\nThe number " + str(number) + " is even. ") else: print("\nThe number " + str(number) + " is odd. ")
3. while循環(huán)
3.1 while 和 if 混合使用
for循環(huán)用于針對集合中的每個元素的一個代碼塊,而while循環(huán)則不斷運(yùn)行,直到指定的條件不滿足為止。
eg: 判斷是否退出游戲
prompt = "\nTell me something, and I Will repect it back you:" prompt += "\nEnter 'quit' to end the program. " message = " " if message != 'quit': message = input(prompt) print(message)
但是此方法的缺點(diǎn)是 會將quit也打印出來。
因此 改進(jìn):
prompt = "\nTell me something, and I Will repect it back you:" prompt += "\nEnter 'quit' to end the program. " message = " " while message != 'quit': message = input(prompt) if message != 'quit': print(message)
也可使用標(biāo)志,這里我們名命此標(biāo)志為acitve(可指定任何名字),來判斷游戲是否運(yùn)行:
prompt = "\nTell me something, and I Will repect it back you:" prompt += "\nEnter 'quit' to end the program. " active = True while avtive: message = input(prompt) if message == 'quit': active = Flase else: print(message)
此結(jié)果與上一個方法的結(jié)果相同。
3.2 使用break退出循環(huán)
prompt = "\nTell me something, and I Will repect it back you:" prompt += "\nEnter 'quit' to end the program. " while True: message = input(prompt) if message == 'quit': break else: print(message)
3.3 使用continue
要返回循環(huán)的開頭,并根據(jù)條件測試結(jié)果決定是否繼續(xù)執(zhí)行循環(huán),可以使用continue語句,它不像break語句那樣不再執(zhí)行余下的代碼并退出。
current_number = 0 while current_number < 10: current_number += 1 if current_number % 2 ==0: continue print(current_number)
結(jié)果
1
3
5
7
9
總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
基于TensorFlow的CNN實(shí)現(xiàn)Mnist手寫數(shù)字識別
這篇文章主要為大家詳細(xì)介紹了基于TensorFlow的CNN實(shí)現(xiàn)Mnist手寫數(shù)字識別,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-06-06Python卷積神經(jīng)網(wǎng)絡(luò)圖片分類框架詳解分析
在機(jī)器視覺領(lǐng)域中,卷積神經(jīng)網(wǎng)絡(luò)算法作為一種新興算法出現(xiàn),在圖像識別領(lǐng)域中,卷積神經(jīng)網(wǎng)絡(luò)能夠較好的實(shí)現(xiàn)圖像的分類效果,而且其位移和形變具有較高的容忍能力2021-11-11python的pdb調(diào)試命令的命令整理及實(shí)例
這篇文章主要介紹了python的pdb調(diào)試命令的命令整理及實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-07-07Python3之外部文件調(diào)用Django程序操作model等文件實(shí)現(xiàn)方式
這篇文章主要介紹了Python3之外部文件調(diào)用Django程序操作model等文件實(shí)現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04