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
输出量
翻译自: https://www.includehelp.com/python/print-odd-and-even-numbers-from-the-list-of-integers.aspx
python判断奇数和偶数
|