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

惠農(nóng)ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書未來(lái)市場(chǎng)廣闊!成為成都創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:13518219792(備注:SSL證書合作)期待與您的合作!
寫一個(gè) Python 程序,用 For 循環(huán)、While 循環(huán)和函數(shù)將偶數(shù)和奇數(shù)放在單獨(dú)的列表中,并給出一個(gè)實(shí)例。
使用 For 循環(huán)將偶數(shù)和奇數(shù)放入單獨(dú)列表的 Python 程序
在這個(gè) python 程序中,我們使用 For 循環(huán)來(lái)迭代給定列表中的每個(gè)元素。在 Python 循環(huán)中,我們使用 If 語(yǔ)句來(lái)檢查列表項(xiàng)是偶數(shù)還是奇數(shù)。根據(jù)結(jié)果,我們將該項(xiàng)目追加到偶數(shù)列表或奇數(shù)列表中。
# Python Program to Put Even and Odd Numbers in Separate List
NumList = []
Even = []
Odd = []
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)
for j in range(Number):
if(NumList[j] % 2 == 0):
Even.append(NumList[j])
else:
Odd.append(NumList[j])
print("Element in Even List is : ", Even)
print("Element in Odd List is : ", Odd)在這個(gè) python 程序中,為了分離列表中的偶數(shù)和奇數(shù),用戶輸入了項(xiàng)= [22,33,44,55,77]
對(duì)于循環(huán)–第一次迭代:對(duì)于范圍(0,5) 中的 0,條件為真。因此,它進(jìn)入 If 語(yǔ)句 If(NumList[0]% 2 = = 0)=>If(22% 2 = = 0)–條件為真 偶數(shù).追加(NumList[0]) = >偶數(shù)= [22]
第二次迭代:對(duì)于范圍(0,5)中的 1–條件為真 如果(NumList[1] % 2 == 0) = >如果(33% 2 = = 0)–條件為假,則進(jìn)入 Else 塊。 奇數(shù)追加(NumList[1]) = >奇數(shù)=【33】
第三次迭代:對(duì)于范圍(0,5)中的 2–條件為真 如果(NumList[2] % 2 == 0) = >如果(44% 2 = = 0)–條件為真 偶數(shù)追加(44) = >偶數(shù)=【22,44】
第四次迭代:對(duì)于范圍(0,5)中的 3–如果(55% 2 = = 0)–條件為假,則條件為真 –條件進(jìn)入“否則”塊。 奇數(shù)追加(55) = >奇數(shù)=【33,55】
第五次迭代:對(duì)于范圍(0,5)中的 4–條件為真 如果(77% 2 = = 0)–條件為假,則進(jìn)入否則塊。 奇數(shù)追加(77) = >奇數(shù)=【33,55,77】
第六次迭代:對(duì)于范圍(5)中的 5–條件為假。因此, Python 退出 For 循環(huán)
Python 程序使用 While 循環(huán)將偶數(shù)和奇數(shù)放在單獨(dú)的列表中
這個(gè)把偶數(shù)放在偶數(shù)表,把奇數(shù)放在奇數(shù)表的程序同上。我們剛剛將 For Loop 替換為 While loop 。
# Python Program to Put Even and Odd Numbers in Separate List
NumList = []
Even = []
Odd = []
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)
while(j < Number):
if(NumList[j] % 2 == 0):
Even.append(NumList[j])
else:
Odd.append(NumList[j])
j = j + 1
print("Element in Even List is : ", Even)
print("Element in Odd List is : ", Odd)Please enter the Total Number of List Elements: 4
Please enter the Value of 1 Element : 11
Please enter the Value of 2 Element : 33
Please enter the Value of 3 Element : 55
Please enter the Value of 4 Element : 4
Element in Even List is : [4]
Element in Odd List is : [11, 33, 55]用函數(shù)將偶數(shù)和奇數(shù)分開的 Python 程序
這個(gè)將奇數(shù)和偶數(shù)放在單獨(dú)列表中的程序與第一個(gè)示例相同。但是,我們使用函數(shù)來(lái)分離邏輯。請(qǐng)記住,您也可以編寫單個(gè)函數(shù),而不是為偶數(shù)和奇數(shù)編寫單獨(dú)的函數(shù)。
# Python Program to Put Even and Odd Numbers in Separate List
def even_numbers(NumList):
Even = []
for j in range(Number):
if(NumList[j] % 2 == 0):
Even.append(NumList[j])
print("Element in Even List is : ", Even)
def odd_numbers(NumList):
Odd = []
for j in range(Number):
if(NumList[j] % 2 != 0):
Odd.append(NumList[j])
print("Element in Odd List is : ", Odd)
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)
even_numbers(NumList)
odd_numbers(NumList)Please enter the Total Number of List Elements: 5
Please enter the Value of 1 Element : 45
Please enter the Value of 2 Element : 56
Please enter the Value of 3 Element : 78
Please enter the Value of 4 Element : 98
Please enter the Value of 5 Element : 22
Element in Even List is : [56, 78, 98, 22]
Element in Odd List is : [45] 網(wǎng)頁(yè)題目:Python程序:將偶數(shù)和奇數(shù)放在單獨(dú)的列表中
URL地址:http://m.fisionsoft.com.cn/article/dpicjgs.html


咨詢
建站咨詢
