python判断奇数和偶数_Python | 程序从整数列表中打印奇数和偶数

论坛 期权论坛 编程之家     
选择匿名的用户   2021-5-30 13:28   11   0

python判断奇数和偶数

Logic: To do this, we will simply go through the list and check whether the number is divisible by 2 or not, if it is divisible by 2, then the number is EVEN otherwise it is ODD.

逻辑:为此,我们将简单地遍历列表,检查该数字是否可被2整除,如果可被2整除,则该数字为偶数,否则为奇数。

Program:

程序:

# Give number of elements present in list
n=int(input())

# list
l= list(map(int,input().strip().split(" ")))

# the number will be odd if on diving the number by 2 
# its remainder is one otherwise number will be even
odd=[]
even=[]

for i in l:
    if(i%2!=0):
        odd.append(i)
    else:
        even.append(i)

print("list of odd number is:",odd)
print("list of even number is:",even)

Output

输出量

Python program to print EVEN and ODD

翻译自: https://www.includehelp.com/python/print-odd-and-even-numbers-from-the-list-of-integers.aspx

python判断奇数和偶数

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP