Python逻辑运算符and or not
Python语言支持逻辑运算符,以下假设变量 a 为 10, b为 20:
运算符 逻辑表达式 描述 实例
and
x and y 布尔"与" - 如果 x 为 False,x and y 返回 False,否则它返回 y 的计算值。 (a and b) 返回 20。
or
x or y 布尔"或" - 如果 x 是非 0,它返回 x 的值,否则它返回 y 的计算值。 (a or b) 返回 10。
not
not x 布尔"非" - 如果 x 为 True,返回 False 。如果 x 为 False,它返回 True。 not(a and b) 返回 False
以上实例输出结果:
实例(Python 2.0+)
#!/usr/bin/python
#-*- coding: UTF-8 -*-a = 10
b = 20if a and b :print "1 - 变量 a 和 b 都为 true"
else:print "1 - 变量 a 和 b 有一个不为 true"if a or b :print "2 - 变量 a 和 b 都为 true,或其中一个变量为 true"
else:print "2 - 变量 a 和 b 都不为 true"
修改变量 a 的值
a = 0
if a and b :print "3 - 变量 a 和 b 都为 true"
else:print "3 - 变量 a 和 b 有一个不为 true"if a or b :print "4 - 变量 a 和 b 都为 true,或其中一个变量为 true"
else:print "4 - 变量 a 和 b 都不为 true"if not( a and b ):print "5 - 变量 a 和 b 都为 false,或其中一个变量为 false"
else:print "5 - 变量 a 和 b 都为 true"
以上实例输出结果:
1 - 变量 a 和 b 都为 true
2 - 变量 a 和 b 都为 true,或其中一个变量为 true
3 - 变量 a 和 b 有一个不为 true
4 - 变量 a 和 b 都为 true,或其中一个变量为 true
5 - 变量 a 和 b 都为 false,或其中一个变量为 false
Python逻辑运算符and or not相关推荐
- Python逻辑运算符 and ,or not 的理解
要理解Python逻辑运算符 and ,or not 这三个 ,你需要知道 在 python里面,0.''.[].().{}.None为 false,其它任何东西都为true 1 and(这个会j ...
- Python 逻辑运算符
Python 逻辑运算符可以用来操作任何类型的表达式,不管表达式是不是 bool 类型:同时,逻辑运算的结果也不一定是 bool 类型,它也可以是任意类型. 这点是和C/C++等语言不同的,C/C++ ...
- python逻辑运算符的优先级_Python逻辑运算符
python逻辑运算符 1.成员 and or not 优先级:() > not > and > or 2.and 逻辑运算符and,a andb,如果a和b都为True,则返回Tr ...
- python 逻辑运算符 and or
https://www.cnblogs.com/white-small/p/6260740.html 1 Python逻辑运算符 Python语言支持逻辑运算符,以下假设变量 a 为 10, b为 2 ...
- python 逻辑运算符_Python逻辑运算符
python 逻辑运算符 Python logical operators allow us to perform logical AND, OR, and NOT operation between ...
- Python逻辑运算符
逻辑运算符用来表示日常生活中"并且"."或者"和"除非"等逻辑关系的运算符. 运算符 含义 说明 and 逻辑与运算,等价于数学中的&quo ...
- Python逻辑运算符有哪些?
Python逻辑运算符中有三种 and . or .not [1]and 逻辑: " 两真为真才是真 "--truetrue and true ---true ---两真为真才是真 ...
- python 逻辑运算符_Python关系和逻辑运算符
python 逻辑运算符 Relation and Logic are the fundamental bricks of a program that defines its functionali ...
- Python逻辑运算符优先级小记
Python逻辑运算符优先级小记
- Python逻辑运算符之与或非
Python逻辑运算符之与或非 一. 短路与(逻辑与)和非短路与 二. python逻辑运算符之与或非 在看python的时候逻辑运算符没看懂. x and y 布尔"与" - 如 ...
最新文章
- 揭秘PPT设计中的逻辑真相
- 五个数字从小到大排序java,五个数冒泡排序 用c语言数组定义5个数使用冒泡排序 从小到大...
- ZF2系列 – Zend Framework 2 MVC實作 (Part 3)
- 面对10ms的延迟,这群人就是一个字“改”
- REDGATE又一好用的脚本工具ScriptsManager1.3
- I2C总线协议详解(特点、通信过程、典型I2C时序)
- 鼠标连点器同时点多个位置_大神的强力鼠标清单,小白用它瞬间提升实力,在游戏里呼风唤雨...
- 【NDN安全】Poseidon: Mitigating Interest Flooding DDoS Attacks in Named Data Networking 学习笔记
- 数据中心机房设备标签规范建议
- [转载]英语语音断句规则
- origin 已画好的图中添加曲线
- 动态条形图展示人均GDP,最后以gif格式输出
- 程序员才看得懂的祝福语(程序员送祝福啦)
- 【Python 测验 01编程】数值运算
- Postman接口自动化之postman脚本编写
- 《教练型管理者》读书笔记-第4篇【教练实践】
- uboot源码下载以及编译
- 2022年web前端开发值得学习的10个javascript框架
- java多态(java多态的概念)
- Codeforces 1182E Product Oriented Recurrence 矩阵快速幂