#基本运算符

#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. JS两个页面通过URL传值

    1.传递参数: window.location.href = "./list.html?id="+id; 1.接收参数: (1)接收参数函数封装 function GetReque ...

  2. 提供一个Java字符串转整型数组的方法

    package edu.yuliang.Data_Structure_Basics; import java.util.Scanner; public class new_string { publi ...

  3. 剑指Offer 45. 扑克牌顺子 (其他)

    题目描述 LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张^_^)...他随机从中抽出了5张牌,想测测自己的手气,看看能不能抽到顺子,如果抽到的话,他决 ...

  4. Ubuntu 17.10 安装Caffe(cpu)并配置Matlab接口

    (1)安装依赖: sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-ser ...

  5. tofile和fromfile数组内建函数

    numpy教程:基本输入输出和文件输入输出Input and output 使用数组的方法函数tofile可以方便地将数组中数据以二进制的格式写进文件.tofile输出的数据没有格式,因此用numpy ...

  6. 图像特征匹配,sift,surf法

    今天想把这一段时间做的一些工作做个总结,望能帮到大家,尊重原创作品,转摘请注明原创地址:http://www.cnblogs.com/ggYYa/p/7902900.html,在此感谢!

  7. crontab 相关

    修改编辑器    select-editor 查看服务状态  service cron status  (linux下为crond ,ubuntu为cron) 1-59/2 1,10,12 * * * ...

  8. 二、tcp/ip基础知识

    一.TCP/IP的标准化 1.TCP/IP的含义 一般来说,TCP/IP是利用IP进行通信时所必须用到的协议群的统称. 具体点,IP或ICMP.TCP或UDP.TELENT或FTP.以及HTTP等都属 ...

  9. 我发起了一个 操作系统 GUI 和 Tcp / IP 包 的 开源项目 DeviceOS

    操作系统 如果 不需要 处理 复杂多样 的 硬件 兼容性, 其实 并不算 大项目, 可以算 毕业设计 . 但是, GUI 和 Tcp / IP  这两个 部分 的 实现逻辑 很多 很复杂,  这  2 ...

  10. Nginx源码结构及如何处理请求

    一.源码结构   1:下载安装包后,解压,可以看到目录结构,其中src目录下放的是源码       2:src源码目录下,可以看到这几个目录     mail:mail目录中存放了实现Nginx服务器 ...