用代碼幫你了解Python基礎(chǔ)(3)
1.循環(huán)
# 1.for...in循環(huán),依次把list或tuple中的每個(gè)元素迭代出來(lái)
studentNames = ["Willard","ChenJD","ChenBao","LinWenYu"]
for studentName in studentNames:
print(studentName)
print("------------------------------------------------")
# 計(jì)算1-10的累加和
sumOfNums = 0
for num in [1,2,3,4,5,6,7,8,9,10]:
sumOfNums = sumOfNums + num
print("1-10的累加和是:",sumOfNums)
print("------------------------------------------------")
# 使用range()函數(shù)進(jìn)行整數(shù)序列生成,range()函數(shù)為左閉右開(kāi)
# 計(jì)算1-1000的累加和
sumOfNums = 0
for num in range(1001):
sumOfNums += num
print("1-1000的累加和是:",sumOfNums)
# 結(jié)果輸出:
Willard
ChenJD
ChenBao
LinWenYu
------------------------------------------------
1-10的累加和是: 55
------------------------------------------------
1-1000的累加和是: 500500
# 2.while循環(huán),只要條件滿足,就不斷循環(huán),條件不滿足時(shí)退出循環(huán)
# 計(jì)算100以內(nèi)的奇數(shù)和
sumOfNums = 0
n = 99
while n > 0:
sumOfNums += n
n = n -2
print("100以內(nèi)的奇數(shù)累加和為:",sumOfNums)
# 結(jié)果輸出:
100以內(nèi)的奇數(shù)累加和為: 2500
# 3.使用break語(yǔ)句提前退出循環(huán)
n = 1
while n < 20:
if n > 10:
break
print("n的值為:",n)
n += 1
print("The End.")
n的值為: 1
n的值為: 2
n的值為: 3
n的值為: 4
n的值為: 5
n的值為: 6
n的值為: 7
n的值為: 8
n的值為: 9
n的值為: 10
The End.
# 4.continue語(yǔ)句,跳過(guò)當(dāng)前循環(huán),直接開(kāi)始下一次循環(huán)
n = 10
while n < 20:
n += 1
if n == 15:
continue
print("n的值為:",n)
print("The End.")
# 結(jié)果輸出:
n的值為: 11
n的值為: 12
n的值為: 13
n的值為: 14
n的值為: 16
n的值為: 17
n的值為: 18
n的值為: 19
n的值為: 20
The End.
# 5.登錄實(shí)例
totalFrequency = 3
inputFrequency = 0
userName = input("請(qǐng)輸入您的賬號(hào):")
password = input("請(qǐng)輸入您的密碼:")
while inputFrequency < totalFrequency:
if ((userName == "Willard") and (password == "JD584520")):
print("賬號(hào)密碼正確,登錄成功!")
break
else:
print("賬號(hào)或密碼輸入錯(cuò)誤,登錄失敗!")
if (totalFrequency - inputFrequency - 1) == 0:
break
print("請(qǐng)重新登錄!您還有%d次輸入賬號(hào)密碼的機(jī)會(huì)!"%(totalFrequency - inputFrequency - 1))
print("----------------------------------------")
inputFrequency += 1
userName = input("請(qǐng)重新輸入您的賬號(hào):")
password = input("請(qǐng)重新輸入您的密碼:")
# 結(jié)果輸出:
# 輸出1:
請(qǐng)輸入您的賬號(hào):Willard
請(qǐng)輸入您的密碼:JD584520
賬號(hào)密碼正確,登錄成功!
---------------------------# 輸出2:
請(qǐng)輸入您的賬號(hào):Willard
請(qǐng)輸入您的密碼:jd584520
賬號(hào)或密碼輸入錯(cuò)誤,登錄失??!
請(qǐng)重新登錄!您還有2次輸入賬號(hào)密碼的機(jī)會(huì)!
----------------------------------------
請(qǐng)重新輸入您的賬號(hào):Willard
請(qǐng)重新輸入您的密碼:JD584520
賬號(hào)密碼正確,登錄成功!--------------------------
# 輸出3:
請(qǐng)輸入您的賬號(hào):willard
請(qǐng)輸入您的密碼:JD584520
賬號(hào)或密碼輸入錯(cuò)誤,登錄失??!
請(qǐng)重新登錄!您還有2次輸入賬號(hào)密碼的機(jī)會(huì)!
----------------------------------------
請(qǐng)重新輸入您的賬號(hào):Willard
請(qǐng)重新輸入您的密碼:jd584520
賬號(hào)或密碼輸入錯(cuò)誤,登錄失?。?br />請(qǐng)重新登錄!您還有1次輸入賬號(hào)密碼的機(jī)會(huì)!
----------------------------------------
請(qǐng)重新輸入您的賬號(hào):willard
請(qǐng)重新輸入您的密碼:jd584520
賬號(hào)或密碼輸入錯(cuò)誤,登錄失??!
2.字典
# dict:字典,使用鍵-值對(duì)(key-value)存儲(chǔ);
# 實(shí)例:
studentScore = {"Willard":{"Math":100,"Chinese":98,"Eng":90},
"ChenJD":{"Math":99,"Chinese":100,"Eng":98},
"ChenBao":{"Math":100,"Chinese":99,"Eng":96}}
print("Willard的成績(jī)是:",studentScore["Willard"])
print("ChenJD的成績(jī)是:",studentScore["ChenJD"])
print("ChenBao的成績(jī)是:",studentScore["ChenBao"])
print("---------------------------------------------")
# 修改元素的值
print("Willard原來(lái)的成績(jī)是:",studentScore["Willard"])
studentScore["Willard"] = {"Math":100,"Chinese":100,"Eng":100}
print("修改后Willard的成績(jī)是:",studentScore["Willard"])
print("---------------------------------------------")
# 判斷key是否存在
# 1.通過(guò)in判斷key是否存在
print("判斷是否存在'ChenXiaoBao'這個(gè)key.\n")
if "ChenXiaoBao" in studentScore:
print("存在'ChenXiaoBao'這個(gè)key.")
else:
print("不存在'ChenXiaoBao'這個(gè)key.")
print("---------------------------------------------")
# 2.通過(guò)get()方法,如果key不存在,返回None,或指定的value
print(studentScore.get("Willard"))
print(studentScore.get("Willard"),-1)
print("---------------------------------------------")
# 刪除一個(gè)key,使用pop(key)
print("刪除前的字典:\n",studentScore,"\n")
studentScore.pop("ChenBao")
print("刪除后的字典:",studentScore)
# Tips:
# 字典的key必須是不可變對(duì)象,如:字符串、整數(shù)等,list是可變的;
# 結(jié)果輸出:
Willard的成績(jī)是: {'Math': 100, 'Chinese': 98, 'Eng': 90}
ChenJD的成績(jī)是: {'Math': 99, 'Chinese': 100, 'Eng': 98}
ChenBao的成績(jī)是: {'Math': 100, 'Chinese': 99, 'Eng': 96}
---------------------------------------------
Willard原來(lái)的成績(jī)是: {'Math': 100, 'Chinese': 98, 'Eng': 90}
修改后Willard的成績(jī)是: {'Math': 100, 'Chinese': 100, 'Eng': 100}
---------------------------------------------
判斷是否存在'ChenXiaoBao'這個(gè)key.不存在'ChenXiaoBao'這個(gè)key.
---------------------------------------------
{'Math': 100, 'Chinese': 100, 'Eng': 100}
{'Math': 100, 'Chinese': 100, 'Eng': 100} -1
---------------------------------------------
刪除前的字典:
{'Willard': {'Math': 100, 'Chinese': 100, 'Eng': 100}, 'ChenJD': {'Math': 99, 'Chinese': 100, 'Eng': 98}, 'ChenBao': {'Math': 100, 'Chinese': 99, 'Eng': 96}}刪除后的字典: {'Willard': {'Math': 100, 'Chinese': 100, 'Eng': 100}, 'ChenJD': {'Math': 99, 'Chinese': 100, 'Eng': 98}}
3.集合
# 集合:set;是一組key的集合,但不存儲(chǔ)value,且key不能重復(fù),具有唯一性;
# 1.創(chuàng)建一個(gè)set,提供一個(gè)list作為輸入集合
setEg = set([1,2,3])
print("集合setEg的內(nèi)容:",setEg)
print("------------------------------")
# 2.集合的元素唯一性
setEg2 = set([1,1,1,2,3,4,5,3,2])
print("集合setEg2的內(nèi)容:",setEg2)
print("------------------------------")
# 3.添加元素
setEg = set([1,2,3])
print("添加元素前集合的內(nèi)容:",setEg)
setEg.add(5)
print("添加元素后集合的內(nèi)容:",setEg)
print("------------------------------")
# 4.刪除元素
setEg = set([1,2,3])
print("刪除元素前集合的內(nèi)容:",setEg)
setEg.remove(1)
print("刪除元素后集合的內(nèi)容:",setEg)
print("------------------------------")
# 5.交集、并集
setOne = set([1,2,3,4,5])
setTwo = set([1,2,4,6])
print("setOne集合的內(nèi)容:",setOne)
print("setTwo集合的內(nèi)容:",setTwo)
print("setOne和setTwo的交集:",(setOne & setTwo))
print("setOne和setTwo的并集:",(setOne | setTwo))
# 結(jié)果輸出:
集合setEg的內(nèi)容: {1, 2, 3}
------------------------------
集合setEg2的內(nèi)容: {1, 2, 3, 4, 5}
------------------------------
添加元素前集合的內(nèi)容: {1, 2, 3}
添加元素后集合的內(nèi)容: {1, 2, 3, 5}
------------------------------
刪除元素前集合的內(nèi)容: {1, 2, 3}
刪除元素后集合的內(nèi)容: {2, 3}
------------------------------
setOne集合的內(nèi)容: {1, 2, 3, 4, 5}
setTwo集合的內(nèi)容: {1, 2, 4, 6}
setOne和setTwo的交集: {1, 2, 4}
setOne和setTwo的并集: {1, 2, 3, 4, 5, 6}
總結(jié)
本篇文章就到這里了,希望能夠給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
安裝python-docx后,無(wú)法在pycharm中導(dǎo)入的解決方案
這篇文章主要介紹了安裝python-docx后,無(wú)法在pycharm中導(dǎo)入的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03
Python實(shí)現(xiàn)快速計(jì)算詞頻功能示例
這篇文章主要介紹了Python實(shí)現(xiàn)快速計(jì)算詞頻功能,結(jié)合實(shí)例形式總結(jié)分析了Python使用nltk庫(kù)進(jìn)行詞頻計(jì)算功能的相關(guān)操作技巧,需要的朋友可以參考下2018-06-06
python沒(méi)有g(shù)pu,如何改用cpu跑代碼
這篇文章主要介紹了python沒(méi)有g(shù)pu,如何改用cpu跑代碼?今天小編就為大家分享一下解決方案。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05
python celery beat實(shí)現(xiàn)定時(shí)任務(wù)的示例代碼
在日常工作中,我們常常會(huì)用到需要周期性執(zhí)行的任務(wù),本文主要介紹了python celery beat實(shí)現(xiàn)定時(shí)任務(wù)的示例代碼,具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03
如何解決pycharm調(diào)試報(bào)錯(cuò)的問(wèn)題
在本篇內(nèi)容里小編給大家整理的是一篇關(guān)于如何解決pycharm調(diào)試報(bào)錯(cuò)的問(wèn)題文章,需要的朋友們可以學(xué)習(xí)參考下。2020-08-08
python 實(shí)現(xiàn)提取log文件中的關(guān)鍵句子,并進(jìn)行統(tǒng)計(jì)分析
今天小編就為大家分享一篇python 實(shí)現(xiàn)提取log文件中的關(guān)鍵句子,并進(jìn)行統(tǒng)計(jì)分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12

