一.运算符种类

1.比较运算符 > ,< ,   >=  ,<=  , != , ==
2.赋值运算符 =, +=,-=,*=,/=,**=,%=
3.成员运算符 in not in
4.算数运算符 + ,-,*,/,**,%
5.逻辑运算符 not and or

二.运算符运算顺序

括号>算数运算符(+,-,*,/,%,//,**)>比较操作符(>,<,!=,=)>not>and>or

三.and,or,not逻辑运算结果

1.在and关系中,两者都为真,选择后者

print(1 and 2)
print(2 and 1)
print(True and 1)
print(1 and True)
运算结果:
2
1
1
True

2.在and关系中,有一个为假,那么结果就是假

print(1 and 0)
print(0 and 1)
print(1 and False)
print(False and 1)
运算结果:
0
0
False
False

3.在and关系中,多个假,那么结果是出现的第一个假,因为发现假,那么后面的结果不会影响最终结果,所以逻辑运算结束

print(0 and False)
print(False and 0) 运算结果:
0
False

4.在or关系中,多个假,没有真,那么一定是最后一个假作为结果,因为当发现第一个假,并不代表后面没有真,所以继续逻辑运算直到最后

print(False or 0)
print(0 or False)
运算结果:
0
False

5.在or关系中,有一个真,那么结果就是出现的第一个真,那么发现真,后面的结果不会影响最终结果,所以逻辑运算结束

print(1 or 2)
print(2 or 1)
print(0 or 1)
print(1 or 0) 运算结果: 1
2
1
1

Python逻辑运算的更多相关文章

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

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

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

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

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

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

  4. 测试误区《二》 python逻辑运算和关系运算优先级

    关系运算 关系运算就是对2个对象进行比较,通过比较符判断进行比较,有6种方式. x > y 大于 x >= y 大于等于 x < y 小于 x <= y 小于等于 x = y ...

  5. python 逻辑运算及奇怪的返回值(not,and,or)

    首先,'and'.'or'和'not'的优先级是not>and>or. 同一优先级从左往右计算. 先说非运算,Python的非运算与这些语言相比,并没有特别的地方.not只有两个返回值,T ...

  6. Python——逻辑运算(or,and)

    print(0 and 2 > 1) #结果0 print(0 and 2 < 1) #结果0 print(1 and 2 > 1) #结果True print(1 and 2 &l ...

  7. 01 Python 逻辑运算

    #基本运算符 #and or not #优先级 ()>not>and>or #and or not print(2>1 and 1<4 or 2<3 and 9&g ...

  8. Python数据结构与循环语句

    # Python数据结构与循环语句:   首先编程是一项技能,类似跑步,期初不必在意细节,能使用起来就行,等学的游刃有余了再回过头来关注细节问题也不迟.  关于买书: 学会python之后,才需要买书 ...

  9. python 杂项

        Python/JS/    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ...

随机推荐

  1. mac nginx+php-fpm配置(安装过后nginx后访问php文件下载,访问php文件请求200显示空白页面)

    访问php文件下载是因为没配置php-fpm 两个问题主要都是nginx.conf配置的问题: /usr/local/etc/nginx/nginx.conf server {    listen 8 ...

  2. map的key 为指针

    STL中map的key能否用char *呢?当然可以! 在程序中需要用到一个map,本来是这样写的,map<string, int> mapStr; 为了追求效率,把string改成了ch ...

  3. for-each、for-in和for-of的区别

    for-each.for-in和for-of的区别 1.forEach()方法 用于调用数组的每个元素,并将元素传递给回调函数. 注意: forEach() 对于空数组是不会执行回调函数的. arra ...

  4. day23-python操作数据库三

    创建表import MySQLdb def connect_mysql(): db_config = { 'host': '192.168.1.5', 'port': 3306, 'user': 'w ...

  5. ubuntu compile openjdk87

    0. use oracle JDK,not OpenJDK 1. 遇到错误Error:./gamma: relocation error: /usr/lib/jvm/java-7-openjdk-am ...

  6. House Robber III

    The thief has found himself a new place for his thievery again. There is only one entrance to this a ...

  7. :状态模式:GumballMachine

    #ifndef __STATE_H__ #define __STATE_H__ #include <iostream> #include<stdlib.h> using nam ...

  8. java动手动脑3

    2016-10-152016-10-15一.编写一个方法,使用以上算法生成指定数目(比如1000个)的随机整数. 生成50个1到10的随机整数. value=a+(int)(Math.Random() ...

  9. Android : 添加apk私有权限

    一.Android的系统权限: apk在安装时,Android 为每个软件包提供唯一的 Linux 用户 ID.此 ID 在软件包在该设备上的使用寿命期间保持不变.在不同设备上,相同软件包可能有不同的 ...

  10. 运算类实现 及 GNU Makefile基本结构

    1.运算类的实现,代码如下:  (1)operator.cpp #include<iostream> #include "operator.h" using names ...