#基本运算符

#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. Charles抓包基本用法

    Charles抓包 浏览器发送和接受的所有请求都可以抓到 1.可以定位问题(如果看不出来是服务端问题还是前端问题) 2.可以设置弱网模式 清空请求按钮如图: 抓包: 1 打开charles,在浏览器中 ...

  2. 结构体变量的 extern 使用方法,转--

    要求如下,在.h文件中这样定义: typedef struct typFNT_GB16     // 汉字字模数据结构 {     signed ];        // 汉字内码索引     ]; ...

  3. 剑指Offer 2. 替换空格 (字符串)

    题目描述 请实现一个函数,将一个字符串中的每个空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. 题目地址 https://ww ...

  4. Oracle(二)在 Mysql 的基础上学习 Oracle

    毕竟我是先学的mysql,对数据库的一切认知都会有一个先入为主的思想在里面,如果不搞清楚其中的异同,我感觉Oracle我是学不会 了,甚至会把它们混淆.那么,不会mysql的没必要往下看了. 下边第一 ...

  5. python int str

    1. int 类型转换 a = "123" b = int(a) b = b+10 print(type(a),a) print(type(b),b) 2. int(num,bas ...

  6. C语言中->是什么意思啊?比如说 p=p->next 到底表达了什么意思,请说清楚点,还有->这个符号是一个整体吗,什么意思??

    ->是一个整体,它是用于指向结构体.C++中的class等含有子数据的指针用来取子数据.换种说法,如果我们在C语言中定义了一个结构体,然后申明一个指针指向这个结构体,那么我们要用指针取出结构体中 ...

  7. oracle-pl/sql之三

    集合与记录 set serveroutput on create or replace package my_types authid definer is type my_rec is record ...

  8. 数字证书的理解以及自建CA机构颁发证书

    一.理解什么是数字证书   http://www.cnblogs.com/JeffreySun/archive/2010/06/24/1627247.html    理解数字证书等概念,无数次想好好看 ...

  9. [zz]LyX 入门教程

    http://blog.sina.com.cn/s/blog_630e5dec0100w3jl.html The LyX Tutorial by the LyX Team 1 目录 Chapter 1 ...

  10. MDX函数

    MDX重点函数 成员函数 1..CurrentMember 获取运行时当前的成员,用法:<Dimension>.CurrentMember . 2..Parent 获取运行时当前的成员的父 ...