首页 > 编程知识 正文

Python 程序:将偶数和奇数放在单独的列表中

时间:2023-11-24 15:33:42 阅读:308915 作者:PGMR

写一个 Python 程序,用 For 循环、While 循环和函数将偶数和奇数放在单独的列表中,并给出一个实例。

使用 For 循环将偶数和奇数放入单独列表的 Python 程序

在这个 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 循环

Python 程序使用 While 循环将偶数和奇数放在单独的列表中

这个把偶数放在偶数表,把奇数放在奇数表的程序同上。我们刚刚将 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 程序

这个将奇数和偶数放在单独列表中的程序与第一个示例相同。但是,我们使用函数来分离逻辑。请记住,您也可以编写单个函数,而不是为偶数和奇数编写单独的函数。

# 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]

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。