#基本运算符

#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. ADO.NET之SqlConnection、sqlcommand的应用(学习笔记)

    一.知识描述点 1.SqlConnection (1)使用SqlConnection类可以连接到SQL Server数据库.SqlConnection对象的主要属性和方法如下: ——属性:Connec ...

  2. linux之安装软件,压缩解压文件

  3. REST easy with kbmMW #24 使用kbmMW实现JSON/XML/YAML转换成对象

    你想过没有,把一个给定的xml或json生成一个Delphi类,并通过这个类完成对xml或json的读写操作吗? 不管有没有,现在kbmMW为我们实现了,看下面这行代码: var s:string; ...

  4. JS的小判断

    // 0 if(undefined) { console.log('1'); } else { console.log('0'); } // 0 if(null) { console.log('1') ...

  5. 我的第一个爬虫程序:利用Python抓取网页上的信息

    题外话 我第一次听说Python是在大二的时候,那个时候C语言都没有学好,于是就没有心思学其他的编程语言.现在,我的毕业设计要用到爬虫技术,在网上搜索了一下,Python语言在爬虫技术这方面获得一致好 ...

  6. ElasticSearch(四):关于es的一些基础知识讲解

    上一篇博客更新完之后,我发现一个问题:在我创建索引的时候依旧无法准确的理解每个字段的意义,所以就有了这个. 1. 关于索引 1.1 关于索引的一些基础知识 在创建标准化索引的时候,我们传入的请求体如下 ...

  7. 基于tensorflow的简单线性回归模型

    #!/usr/local/bin/python3 ##ljj [1] ##linear regression model import tensorflow as tf import matplotl ...

  8. fixed 相对于父容器定位

    当一个元素设置为 fixed 或 absolute,不设置 top, left 则会在原位置,而脱离文档流,别的元素可以存在于它之后. 而当使用 fixed 后还想相对于父容器进行定位,或者说在当前位 ...

  9. 函数和对象 及 prototype和__proto__

    对象有  __proto__ 函数有 prototype 对象的__proto__指向构造自己的函数的prototype 但有一例外 var Obj = {v:99}var pObj = Object ...

  10. ASP.NET 4.0 :MasterPage母版页的ClientIDMode属性

    在ASP.NET 4.0之前我们总是要为控件的ClientID头疼,比如明明一个叫lblName的Label放在一个叫做grd的GridView里面后,在页面上改Label的ID就变成了诸如grd_c ...