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判断奇数和偶数

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

  1. python判断是否回文_对python判断是否回文数的实例详解

    设n是一任意自然数.若将n的各位数字反向排列所得自然数n1与n相等,则称n为一回文数.例如,若n=1234321,则称n为一回文数:但若n=1234567,则n不是回文数. 上面的解释就是说回文数和逆 ...

  2. python判断题题库_《Python程序设计》判断题1-240题

    1.Python是一种跨平台.开源.免费的高级动态编程语言.(对) 2.Python 3.x完全兼容Python 2.x.(错) 3.Python 3.x和Python 2.x唯一的区别就是:prin ...

  3. python 判断括号是否匹配_使用Python实现一个栈判断括号是否平衡

    栈(Stack)在计算机领域是一个被广泛应用的集合,栈是线性集合,访问都严格地限制在一段,叫做顶(top). 举个例子,栈就想一摞洗干净的盘子,你每次取一个新盘子,都是放在这一摞盘子的最上头,当你往里 ...

  4. python判断素数的函数_使用Python判断质数(素数)的简单方法讲解

    质数又称素数.指在一个大于1的自然数中,除了1和此整数自身外,不能被其他自然数整除的数.素数在数论中有着很重要的地位.比1大但不是素数的数称为合数.1和0既非素数也非合数.质数是与合数相对立的两个概念 ...

  5. python判断邮件发送成功_(原创)python发送邮件

    这段时间一直在学习flask框架,看到flask扩展中有一个mail插件,所以今天就给大家演示如果发邮件. 首先我注册了一个163邮箱,需要开启smtp功能,因为咱们python发送邮件经过的是smt ...

  6. python判断手机号运营商_基于python的-使用正则表达式验证手机号并匹配运营商和所述地域...

    import re import json # 将语句中不符合手机号码规则的数字串剔除,保存剩余符合手机号码规则的数字到ns中 def is_phone(n): ns = re.findall('\d ...

  7. python对分组进行排序_如何按排序顺序将列表中的项目分组?

    由于您希望对输出进行排序,所以可以根据第一个元素对原始列表进行排序>>> first = lambda x: x[0] >>> one_sorted = sorte ...

  8. python判断输入数字大于0,对python 判断数字是否小于0的方法详解

    对python 判断数字是否小于0的方法详解 为了精度更准确 可以使用数字的绝对值 < 1.0e-16  或者 < 1.0e-8来对比 abs(Num) 以上这篇对python 判断数字是 ...

  9. python输出列表元素_在Python中分别打印列表中的每一个元素方法

    在Python中分别打印列表中的每一个元素方法 更新时间:2018年11月07日 15:12:03 作者:wintersshi 今天小编就为大家分享一篇在Python中分别打印列表中的每一个元素方法, ...

最新文章

  1. NDK JNI Android Studio开发与调试DEMO(三)(生成 .so 文件)
  2. 国内IT图书出版的未来之路
  3. asp.net + winform + log4net 使用示例|using log4net with asp.net/winform
  4. Ubuntu下安装pycharm遇到的一些问题解决方法
  5. 吴恩达 Deeplearning深度学习笔记v5.7 最新PDF版 免积分下载
  6. javascript 图片上传并显示
  7. opencv python 人脸识别 相似度_python3.6+opencv+keras等人脸识别匹配初探
  8. PeakDo毫米波无线投屏器
  9. C语言中数组名的使用总结
  10. 第三方支付架构设计之:自有账户支付(六)
  11. 解决Steam需要在线进行更新。请确定您的网络连接正常,然后重试。的问题
  12. 微服务的优缺点_支付宝上的好医保长期医疗险这款保险到底怎么样?保障全面吗?有哪些优缺点?值得买吗?...
  13. JavaScript中开发常用方法-总结-持续更新
  14. cms概述 。比较shopex和ecshop区别 。smarty模板引擎的入门
  15. Windows 11 企业版,安装或执行程序时有时会报错:文件系统错误(-1073740771)
  16. google学术无法访问
  17. 【PyTorch深度学习项目实战100例目录】项目详解 + 数据集 + 完整源码
  18. 【虹科白皮书】通过卫星网络测试应用程序性能的最佳做法
  19. git 设置别名 git alias
  20. IT小天个人技术博客

热门文章

  1. Shiro基础应用——角色和权限校验
  2. SpringCloud2.0
  3. Boost学习之语法解析器--Spirit
  4. 一个程序员单枪匹马,靠一个网站一年赚1个亿
  5. lazy(懒加载)模式和异步加载模式详解
  6. IE浏览器对象不支持Blob属性或方法,IE浏览器不支持canvas toBlob()方法的Polyfill
  7. 在安霸s2lm上wifi定频测试
  8. 线程函数参数(LPVOID Param)
  9. 搜索引擎 Solr 简介
  10. Nginx介绍和使用