#基本运算符

#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 逻辑运算的更多相关文章

  1. jave 逻辑运算 vs 位运算 + Python 逻辑运算 vs 位运算

    JAVA中&&和&.||和|(短路与和逻辑与.短路或和逻辑或)的区别 博客分类: 面试题目 Java.netBlog  转自 :http://blog.csdn.net/web ...

  2. 01.python基础知识_01

    一.编译型语言和解释型语言的区别是什么? 1.编译型语言将源程序全部编译成机器码,并把结果保存为二进制文件.运行时,直接使用编译好的文件即可 2.解释型语言只在执行程序时,才一条一条的解释成机器语言给 ...

  3. python 逻辑运算 ‘and’ ,'or' 在实战中的作用,代替if语句。

    彩票程序:课上方法:import random # 生成一个随机两位数 作为一个中奖号码luck_num = random.randint(10,99)print(luck_num)luck_num_ ...

  4. day 01 python基础

    1.计算机历史 2.python历史 宏观: python2和python3的区别: python2  源码不标准,混乱,重复代码过多 python3  统一标准,去除重复代码 3.python环境 ...

  5. 01 Python初识

    基础: 1.后缀名是py       ATT: 单个文件执行,后缀无所谓 2.两种执行方式 终端 python+文件路径 解释器内部: 直接执行 3.解释器路径: #/usr/bin/env pyth ...

  6. 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 ...

  7. Python逻辑运算

    一.运算符种类 1.比较运算符 > ,< , >= ,<= , != , == 2.赋值运算符 =, +=,-=,*=,/=,**=,%= 3.成员运算符 in not in ...

  8. python逻辑运算(not,and,or)总结

    逻辑运算 1.在没有()的情况下not优先级高于and,and优先级高于or,即优先级关系为()>not>and>or,同一优先级从左往右计算 总结:a or b : 如果a = 0 ...

  9. 01.Python基础-5.函数

    1 函数的介绍 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 内置函数 自定义函数 2 函数的定义和调用 2.1 函数的定义和调用 定义 def 函数名([参数]): 代码块 [ ...

随机推荐

  1. 使用mybatis generator插件,自动生成dao、dto、mapper等文件

    mybatis generator 介绍 mybatis generator中文文档http://mbg.cndocs.tk/ MyBatis Generator (MBG) 是一个Mybatis的代 ...

  2. ubuntu16.04下python2、python3环境选择与python升级(pip版本切换)

    参考链接:https://www.jianshu.com/p/63c1f22e1fed Ubuntu中python版本问题: 添加PPA: sudo add-apt-repository ppa:jo ...

  3. 机器视觉:SSD Single Shot MultiBox Detector

    今天介绍目标检测中非常著名的一个框架 SSD,与之前的 R-CNN 系列的不同,而且速度比 YOLO 更快. SSD 的核心思想是将不同尺度的 feature map 分成很多固定大小的 box,然后 ...

  4. ORA-00847: MEMORY_TARGET/MEMORY_MAX_TARGET and LOCK_SGA cannot be set together

    群里有位兄弟,测试系统修改sga_lock=true参数后,重启库报错 ORA-00847: MEMORY_TARGET/MEMORY_MAX_TARGET and LOCK_SGA cannot b ...

  5. golang sync.Cond条件变量的使用

    cond.Wait()的操作实际上是对与cond绑定的锁先进行解锁,在等待通知:接收到通知后,会尝试加锁,加锁成功则唤醒否则继续等待通知: cond.Waite()前必须对关连锁加锁,否则panic ...

  6. Gym - 100783G:Playing With Geometry (几何 离散化 )

    pro:给定规则的多边形,规则是指顶点都在整点上,而且是相互垂直的边的交点. 现在给定两个多边形A,B,问A,B缩小,旋转后是否可以变为同一个图形. sol:缩小的话,直接离散化即可,就可以去掉没用的 ...

  7. yii2 获取模块名、控制器名、方法名

    在视图中: 模块名  $this->context->module->id控制器名 $this->context->id方法名 $this->context-> ...

  8. 为WebService添加身份验证的两种方法

    方法一:SoapHeader 辅助类:MySoapHeader //SoapHeader 添加引用 using System.Web.Services.Protocols; #region 配置登录标 ...

  9. Docker之 默认桥接网络与自定义桥接网卡

    docker引擎会默认创建一个docker0网桥,它在内核层连通了其他的物理或虚拟网卡,这就将所有容器和宿主机都放到同一个二层网络. 1. docker如何使用网桥 1.1 Linux虚拟网桥的特点 ...

  10. Nginx反向代理tomcat返回400 bad request

    Nginx反向代理tomcat返回400 bad request nginx 版本1.12, tomcat版本 9.06 最近用Nginx做反向代理tomcat,实现前后端分离,nginx 将请求代理 ...