寫一個 Python 程序,用 For 循環、While 循環和函數將偶數和奇數放在單獨的列表中,並給出一個實例。
在這個 python 程序中,我們使用 For 循環來迭代給定列表中的每個元素。在 Python 循環中,我們使用 If 語句來檢查列表項是偶數還是奇數。根據結果,我們將該項目追加到偶數列表或奇數列表中。
# 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)
在這個 python 程序中,為了分離列表中的偶數和奇數,用戶輸入了項= [22,33,44,55,77]
對於循環–第一次迭代:對於範圍(0,5)
中的 0,條件為真。因此,它進入 If 語句
If(NumList[0]% 2 = = 0)=>If(22% 2 = = 0)–條件為真
偶數.追加(NumList[0]) = >偶數= [22]
第二次迭代:對於範圍(0,5)中的 1–條件為真
如果(NumList[1] % 2 == 0) = >如果(33% 2 = = 0)–條件為假,則進入 Else 塊。
奇數追加(NumList[1]) = >奇數=【33】
第三次迭代:對於範圍(0,5)中的 2–條件為真
如果(NumList[2] % 2 == 0) = >如果(44% 2 = = 0)–條件為真
偶數追加(44) = >偶數=【22,44】
第四次迭代:對於範圍(0,5)中的 3–如果(55% 2 = = 0)–條件為假,則條件為真
–條件進入「否則」塊。
奇數追加(55) = >奇數=【33,55】
第五次迭代:對於範圍(0,5)中的 4–條件為真
如果(77% 2 = = 0)–條件為假,則進入否則塊。
奇數追加(77) = >奇數=【33,55,77】
第六次迭代:對於範圍(5)中的 5–條件為假。因此, Python 退出 For 循環
這個把偶數放在偶數表,把奇數放在奇數表的程序同上。我們剛剛將 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]
這個將奇數和偶數放在單獨列表中的程序與第一個示例相同。但是,我們使用函數來分離邏輯。請記住,您也可以編寫單個函數,而不是為偶數和奇數編寫單獨的函數。
# 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]
原創文章,作者:KHO2I,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/126775.html