python判断奇数和偶数_Python | 程序从列表中创建两个带有偶数和奇数的列表

本文介绍了如何使用Python从给定列表中创建两个列表,分别包含偶数和奇数。通过遍历列表元素并根据条件判断,将偶数和奇数追加到对应的列表中。
摘要由CSDN通过智能技术生成

python判断奇数和偶数

Given a list, and we have to create two lists 1) list with EVEN numbers and 2) list with ODD numbers from given list in Python.

给定一个列表,我们必须创建两个列表:1)带有偶数的列表和2)带有Python中给定列表的奇数的列表。

Example:

例:

    Input:
    List1 = [11, 22, 33, 44, 55]

    Output:
    List with EVEN numbers: [22, 44]
    List with ODD NUMBERS: [11, 33, 55]

Logic:

逻辑:

To create lists with EVEN and ODD numbers, we will traverse each element of list1 and append EVEN and ODD numbers in two lists by checking the conditions for EVEN and ODD.

要创建具有偶数和奇数编号的列表,我们将遍历list1的每个元素,并通过检查偶数和奇数的条件在两个列表中追加偶数和奇数编号。

Program:

程序:

# declare and assign list1
list1 = [11, 22, 33, 44, 55]

# declare listOdd - to store odd numbers
# declare listEven - to store even numbers
listOdd = []
listEven = []

# check and append odd numbers in listOdd
# and even numbers in listEven
for num in list1:
	if num%2 == 0:
		listEven.append(num)
	else:
		listOdd.append(num) 

# print lists
print "list1:    ", list1 
print "listEven: ", listEven
print "listOdd:  ", listOdd

Output

输出量

    list1:     [11, 22, 33, 44, 55]
    listEven:  [22, 44]
    listOdd:   [11, 33, 55]


翻译自: https://www.includehelp.com/python/create-two-lists-with-even-numbers-and-odd-numbers-from-a-list.aspx

python判断奇数和偶数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值