python判断奇数和偶数_从Python中的给定列表中提取偶数和奇数

python判断奇数和偶数

In this problem, we are given a list by the user which may be the mixture of even and odd numbers and based on the concept of even and odd, we will split the list into two lists and one will contain only even numbers and another will contain only odd numbers. Before going to do this task, we will learn how to check the given number is even or odd in Python?

在这个问题中,我们为用户提供了一个可能是偶数和奇数混合的列表,根据偶数和奇数的概念,我们将列表分为两个列表,一个仅包含偶数,另一个将包含仅包含奇数。 在执行此任务之前,我们将学习如何在Python中检查给定的数字是偶数还是奇数?

What are the even and odd number?

什么是偶数和奇数?

The number that can be fully divided by 2 is known as an even number and if the number is not divisible by 2 then it is known as an odd number.

可以完全除以2的数字称为偶数,如果不能将数字除以2,则称为奇数。

Python程序检查偶数或奇数 (Python program to check even or odd number)

# taking input from the user 
n=int(input('Enter the number: '))

# checking whether it is EVEN or ODD
if n%2==0:
    print('{} is an even number.'.format(n))
else:
    print('{} is an odd number.'.format(n))

Output

输出量

RUN 1:
Enter the number: 63734
63734 is an even number.

RUN 2:
Enter the number: 9568405
9568405 is an odd number.

Algorithm to extract even and odd number from the given list

从给定列表中提取偶数和奇数的算法

  1. Take the input in the form of a list.

    采取列表形式的输入。

  2. Create two empty lists to store the even and odd number which will be extracted from the given list.

    创建两个空列表以存储将从给定列表中提取的偶数和奇数。

  3. Check each element of the given list.

    检查给定列表的每个元素。

    1. If it is an EVEN number, then add this to one of the lists from the above-created list by using the append method.
    2. If it is an ODD number, then add this to another list from the above-created list by using the append method.
  4. Print both lists which will be our required list.

    打印两个列表,这将是我们所需的列表。

Python程序检查给定列表中的偶数或奇数 (Python program to check even or odd number from the given list)

# input the list
A=list(map(int,input('Enter elements of List: ').split()))

# create two empty lists to store EVEN and ODD elements
B=[]
c=[]

for j in A:
    if j%2==0:
        B.append(j)
    else:
        c.append(j)
        
print('List of even number: ',B)
print('List of odd number: ',c)

Output

输出量

Enter elements of List: 6 4 7 45 7 6 7 9 2 1
List of even number: [6, 4, 6, 2]
List of odd number: [7, 45, 7, 7, 9, 1]

append() method:

append()方法:

The function append use to add a number to an existing list. Here, we have used the append function to add an even number to list B and odd numbers to list C.

函数append用于将数字添加到现有列表。 在这里,我们使用了append函数将偶数添加到列表B中,并将奇数添加到列表C中 。

翻译自: https://www.includehelp.com/python/extract-even-and-odd-number-from-a-given-list.aspx

python判断奇数和偶数

  • 11
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值