python学习(字典、用户输入和while循环)
前言:上次學習到了if語句,這次接著進行學習。
字典
字典能夠準確地為各種真實物體建模,且能夠將相關信息關聯起來。
使用字典
字典是一系列的鍵——值對,一個建對應一個值,值可以為數字、字符串等
在Python中,字典用放在花括號{} 中的一系列鍵—值對表示。
訪問字典中的值
score = {'shuxu':'80','yuwen':'90'}print(score['shuxu']) print(score['yuwen']) #輸出結果: 80 90添加鍵——值對
字典是一種動態結構,可隨時在其中添加鍵—值對
添加時值用方括號[]括起來
創建一個空字典
score = {}score['wuli'] = 60 score['yingyu'] = 90 print(score) #輸出結果: {'wuli': 60, 'yingyu': 90}修改字典中的值
score = {'yuwen':'80'} print(score) score['yuwen'] = '90' print(score) #輸出結果: {'yuwen': '80'} {'yuwen': '90'}刪除鍵——值對
使用del 語句可以將相應的鍵—值對徹底刪除,使用del 語句時,必須指定字典名和要刪除的鍵。
score = {'yuwen':80,'shuxu':90} print(score) del score['shuxu'] print(score) #輸出結果: {'yuwen': 80, 'shuxu': 90} {'yuwen': 80}由類似對象組成的字典
如果用字典來存儲眾多對象的同一種信息,可以用這樣的形式
遍歷所有的鍵——值對
遍歷鍵值——對時,可聲明兩個變量,用于存儲鍵—值對中的鍵和值。對于這兩個變量,可使用任何名稱。
yuwen_score = {'me': '90','you':'80','he':'70','she':'60',} for key,value in yuwen_score.items():print("\nkey: " + key)print("value: " + value) #items() 函數以列表返回可遍歷的(鍵, 值) 元組數組。 #輸出結果: key: me value: 90key: you value: 80key: he value: 70key: she value: 60遍歷字典中的所有鍵
keys() 函數返回一個列表包含所有鍵
yuwen_score = {'me': '90','you':'80','he':'70','she':'60',} for key in yuwen_score.keys():print("\nkey: " + key) #輸出結果: key: mekey: youkey: hekey: she按順序遍歷字典中的所有值
函數sorted() 來獲得按特定順序排列的鍵列表的副本
yuwen_score = {'me': '90','you':'80','he':'70','she':'60',} for key in sorted(yuwen_score.keys()):print("\nkey: " + key) #輸出結果: key: hekey: mekey: shekey: you遍歷字典中的所有值
方法values() ,返回一個值列表,不包含任何鍵。
yuwen_score = {'me': '90','you':'80','he':'70','she':'60',} for score in yuwen_score.values():print("\nscore: " + score) #輸出結果: score: 90score: 80score: 70score: 60如果值中有重復的,可以用集合set,如:
yuwen_score = {'me': '90','you':'80','he':'70','she':'90',} for score in set(yuwen_score.values()):print("\nscore: " + score) #輸出結果: score: 90score: 80score: 70嵌套
將一系列字典存儲在列表中,或將列表作為值存儲在字典中,這稱為嵌套
字典列表
score_0 = {'subject':'yuwen','point':60} score_1 = {'subject':'shuxu','point':70} score_2 = {'subject':'yingyu','point':80}scores = [score_0,score_1,score_2]for score in scores:print(score) #輸出結果: {'subject': 'yuwen', 'point': 60} {'subject': 'shuxu', 'point': 70} {'subject': 'yingyu', 'point': 80}在字典中儲存列表
每當需要在字典中將一個鍵關聯到多個值時,都可以在字典中嵌套一個列表。
school = {'teacher': 'wang','subjects': ['shuxu','yuwen'],} print(school['teacher'])for subject in school['subjects']:print("\t" + subject) #輸出結果: wangshuxuyuwen用戶輸入和while循環
函數input
函數input() 讓程序暫停運行,等待用戶輸入一些文本。獲取用戶輸入后,Python將其存儲在一個變量中。
使用函數input() 時,輸入解讀為字符串。
函數int
使用函數int() 來獲取數值輸入 ,input()解讀的為字符串,不能直接和整數比較
將數值輸入用于計算和比較前,需將其轉換為數值表示.
求模運算符
%,將兩個數相除并返回余數
print(4 % 3) print(5 % 3) print(6 % 3) #輸出結果: 1 2 0使用while 循環
通過一個例子來了解while的語法
在循環中使用continue
返回到循環開頭,并根據條件測試結果決定是否繼續執行循環
number = 0 while number <10:number += 1if number % 2 ==0:continueprint(number)#輸出結果:1 3 5 7 9在列表之間移動元素
numbers = ['a','b','c'] confirmed_numbers = []while numbers:middle_number = numbers.pop()#刪除末尾賦給新的變量confirmed_numbers.append(middle_number)for confirmed_number in confirmed_numbers:print(confirmed_number.title()) #輸出結果: C B A刪除包含特定值的所有列表元素
messages = ['a','b','c','d','a','a'] print(messages)while 'a' in messages:messages.remove('a')print(messages) #輸出結果: ['a', 'b', 'c', 'd', 'a', 'a'] ['b', 'c', 'd']使用用戶輸入來填充字典
#創建一個空字典 responses = {} #設置一個標志 active = Truewhile active:name = input("\nWhat is your name?")like_food = input("your like food is ?")#將答案存儲在字典中responses[name] = like_foodrepeat = input("Would you like to let another person respond? (yes/ no) ")if repeat =='no':active = False for name,like_food in responses.items():print(name+":"+like_food) #輸出結果: What is your name?222 your like food is ?222 Would you like to let another person respond? (yes/ no) no 222:222這次就先學習到這,下次繼續學習。
總結
以上是生活随笔為你收集整理的python学习(字典、用户输入和while循环)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: sql注入学习——时间盲注
- 下一篇: 文件上传漏洞——upload-labs(