01 Python 逻辑运算
#基本运算符




#and or not
#优先级 ()>not>and>or
#and or not
print(2>1 and 1<4 or 2<3 and 9>6 or 2<4 and 3<2)
# True or True or False
#True
print(3>4 or 4<3 and 1==1) #False
print(1<2 and 3<4 or 1>2) #True
print(2>1 and 3<4 or 4>5 and 2<1) #True
print(1>2 and 3<4 or 4>5 and 2>1 or 9<8) #False
print(1>1 and 3<4 or 4>5 and 2>1 and 9>8 or 7<6) #False
print(not 2>1 and 3<4 or 4>5 and 2>1 and 9>8 or 7<6) #False
数字与布尔值转换
#int-->bool
print(bool(2)) #True
print(bool(-2)) #True
print(bool(0)) #False
#bool-->int
print(int(True)) #
print(int(False)) #
x or y , x为真,值就是x,x为假,值是y
print(1 or 2) #
print(3 or 2) #
print(0 or 2) #
print(0 or 100) #
x and y, x为真,值是y,x为假,值是x
print(1 and 2) #
print(0 and 2) #
in,not in :
判断子元素是否在原字符串(字典,列表,集合)中:
print('喜欢' in 'dkfljadklf喜欢hfjdkas')
print('a' in 'bcvd')
print('y' not in 'ofkjdslaf')
#and or 组合
print(2 or 100 or 3 or 4) #
print(0 or 4 and 3 or 2) #
#综合 第一个为布尔型则返回布尔型,第一个为数值型,则返回数值型
print(1>2 and 3 or 4 and 3<2) #False
print(2 or 1<3 and 2) #
print(0 or 5<4) #False
print(0 and 3>1) #
print(3>1 and 0) #
print(3>1 and 2 or 2<3 and 3 and 4 or 3>2) #
01 Python 逻辑运算的更多相关文章
- jave 逻辑运算 vs 位运算 + Python 逻辑运算 vs 位运算
JAVA中&&和&.||和|(短路与和逻辑与.短路或和逻辑或)的区别 博客分类: 面试题目 Java.netBlog 转自 :http://blog.csdn.net/web ...
- 01.python基础知识_01
一.编译型语言和解释型语言的区别是什么? 1.编译型语言将源程序全部编译成机器码,并把结果保存为二进制文件.运行时,直接使用编译好的文件即可 2.解释型语言只在执行程序时,才一条一条的解释成机器语言给 ...
- python 逻辑运算 ‘and’ ,'or' 在实战中的作用,代替if语句。
彩票程序:课上方法:import random # 生成一个随机两位数 作为一个中奖号码luck_num = random.randint(10,99)print(luck_num)luck_num_ ...
- day 01 python基础
1.计算机历史 2.python历史 宏观: python2和python3的区别: python2 源码不标准,混乱,重复代码过多 python3 统一标准,去除重复代码 3.python环境 ...
- 01 Python初识
基础: 1.后缀名是py ATT: 单个文件执行,后缀无所谓 2.两种执行方式 终端 python+文件路径 解释器内部: 直接执行 3.解释器路径: #/usr/bin/env pyth ...
- 01: Python基本数据类型
目录: 1.1 列表和元组 1.2 字符串 1.3 字典 1.4 集合 1.1 列表和元组返回顶部 1.列表基本操作 1. 列表赋值 a = [1,2,3,4,5,6,7,8] a[0] = 100 ...
- Python逻辑运算
一.运算符种类 1.比较运算符 > ,< , >= ,<= , != , == 2.赋值运算符 =, +=,-=,*=,/=,**=,%= 3.成员运算符 in not in ...
- python逻辑运算(not,and,or)总结
逻辑运算 1.在没有()的情况下not优先级高于and,and优先级高于or,即优先级关系为()>not>and>or,同一优先级从左往右计算 总结:a or b : 如果a = 0 ...
- 01.Python基础-5.函数
1 函数的介绍 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 内置函数 自定义函数 2 函数的定义和调用 2.1 函数的定义和调用 定义 def 函数名([参数]): 代码块 [ ...
随机推荐
- web 页面上纯js实现按钮倒计数功能(实时计时器也可以)
需求构思:本功能想实现的是,一个按钮在页面载入就显示提醒续费,,,倒数60秒后,完成提醒功能,可以按另外一个页面跳转到主页. 参考网上的大神,实现如下:Button2倒数,Button3跳转,在页面上 ...
- jdbc “贾琏欲执事”
“贾琏欲执事” 1.加载驱动2.获取连接3.SQL语句4.执行SQL5.释放资源 示例: public void test_insert() { String driver="oracle. ...
- 剑指Offer 10. 矩形覆盖 (递归)
题目描述 我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形.请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法? 题目地址 https://www.nowcoder.com/ ...
- Binary Search Tree Learning Summary
BST Definition BST is short for Binary Search Tree, by definition, the value of right node is always ...
- scrapy框架的日志等级和请求传参
日志等级 请求传参 如何提高scrapy的爬取效率 一.Scrapy的日志等级 - 在使用scrapy crawl spiderFileName运行程序时,在终端里打印输出的就是scrapy的日志信息 ...
- Chromium Settings页面修改
/********************************************************************** * Chromium Settings页面修改 * 说明 ...
- Flask+uwsgi+virtualenv环境配置
Linux系统版本: SLES12sp3 (阿里云) 1. 首先需要安装python-devel,否则后续安装会报错! rpm -qa|grep python-base 结果: python-base ...
- lua经典问题
lua是一门比较简单的脚本语言,但是有些问题经常碰到,在这里总结一下: 1 lua 传参,如果参数是table,则相当于传引用 2 lua中只有nil和false返回假 3 lua and 和 or ...
- Apache ZooKeeper 服务启动源码解释
转载:https://www.ibm.com/developerworks/cn/opensource/os-cn-zookeeper-code/ 本文首先讲解了 Apache ZooKeeper 服 ...
- js中常用的内置对象
Arguments 函数参数集合 arguments[ ] 函数参数的数组 Arguments 一个函数的参数和其他属性 Arguments.callee 当前正在运行的函数 Argument ...