新聞中心
創(chuàng)新互聯(lián)python教程:

創(chuàng)新互聯(lián)建站是一家集網(wǎng)站建設(shè),天臺(tái)企業(yè)網(wǎng)站建設(shè),天臺(tái)品牌網(wǎng)站建設(shè),網(wǎng)站定制,天臺(tái)網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷(xiāo),網(wǎng)絡(luò)優(yōu)化,天臺(tái)網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力??沙浞譂M(mǎn)足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專(zhuān)業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶(hù)成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
編寫(xiě)一個(gè) Python 程序,使用 For 循環(huán)、While 循環(huán)和函數(shù)打印列表中的偶數(shù),并給出一個(gè)實(shí)例。
使用 For 循環(huán)打印列表中偶數(shù)的 Python 程序
在這個(gè) python 程序中,我們使用 For 循環(huán)來(lái)迭代這個(gè)列表中的每個(gè)元素。在 Python 循環(huán)中,我們使用 If 語(yǔ)句來(lái)檢查偶數(shù)。
# Python Program to Print Even Numbers in a List
NumList = []
Number = int(input("Please enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
value = int(input("Please enter the Value of %d Element : " %i))
NumList.append(value)
print("\nEven Numbers in this List are : ")
for j in range(Number):
if(NumList[j] % 2 == 0):
print(NumList[j], end = ' ')在本 python 程序中,用戶(hù)輸入了列表元素= [22,56,7,87]
對(duì)于循環(huán)–第一次迭代:對(duì)于范圍(0,4) 中的 0,循環(huán)的條件為真。于是, Python 進(jìn)入 If 語(yǔ)句
if(NumList[0]% 2 = = 0)= > if(22% 2 = = 0)–條件真 該數(shù)字已打印。
第二次迭代:對(duì)于范圍(0,4)中的 1–條件為真 如果(NumList[1] % 2 == 0) = >如果(56% 2 = = 0)–條件為真 該數(shù)字被打印。
第三次迭代:對(duì)于范圍(0,4)中的 2–條件為真 如果(NumList[2] % 2 == 0) = >如果(7% 2 = = 0)–條件為假 這個(gè)數(shù)字被跳過(guò)。
第四次迭代:對(duì)于范圍(0,4)中的 3–條件為真 如果(NumList[3] % 2 == 0) = >如果(87% 2 = = 0)–條件為假 跳過(guò)此數(shù)字。
第五次迭代:對(duì)于范圍(0,4)中的 4–條件為假 因此,它從 for 循環(huán)中退出
使用 While 循環(huán)打印列表中偶數(shù)的 Python 程序
列表程序中的這個(gè) Python 偶數(shù)和上面的一樣。我們剛剛將 For 循環(huán)替換為 While 循環(huán)。
NumList = []
j = 0
Number = int(input("Please enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
value = int(input("Please enter the Value of %d Element : " %i))
NumList.append(value)
print("\nEven Numbers in this List are : ")
while(j < Number):
if(NumList[j] % 2 == 0):
print(NumList[j], end = ' ')
j = j + 1Please enter the Total Number of List Elements: 5
Please enter the Value of 1 Element : 12
Please enter the Value of 2 Element : 13
Please enter the Value of 3 Element : 55
Please enter the Value of 4 Element : 66
Please enter the Value of 5 Element : 90
Even Numbers in this List are :
12 66 90 使用函數(shù)打印列表中偶數(shù)的 Python 程序
清單程序中的 Python 偶數(shù)與第一個(gè)示例相同。然而,我們使用功能分離邏輯。
def even_numbers(NumList):
for j in range(Number):
if(NumList[j] % 2 == 0):
print(NumList[j], end = ' ')
NumList = []
Number = int(input("Please enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
value = int(input("Please enter the Value of %d Element : " %i))
NumList.append(value)
print("\nEven Numbers in this List are : ")
even_numbers(NumList)Please enter the Total Number of List Elements: 6
Please enter the Value of 1 Element : 22
Please enter the Value of 2 Element : 33
Please enter the Value of 3 Element : 44
Please enter the Value of 4 Element : 55
Please enter the Value of 5 Element : 66
Please enter the Value of 6 Element : 88
Even Numbers in this List are :
22 44 66 88 文章名稱(chēng):Python程序:打印列表中偶數(shù)
鏈接分享:http://www.5511xx.com/article/cocccsc.html


咨詢(xún)
建站咨詢(xún)
