Python_逻辑_算术_比较_赋值运算符

资料来源:

http://www.runoob.com/python/python-operators.html#ysf5

1、逻辑运算符"与、或、非"

   在Java、C中使用"&、||、!",在Python中使用"and,or,not"

     优先级 not > and = or

 1 #True和False首字母必须大写,小写不识别
 2 
 3 #输入:
 4 print (not True)
 5 print (False and True)
 6 print (True and False)
 7 print (True or False)
 8 
 9 #输出:
10 >>> 
11 False
12 False
13 False
14 True
15 >>> 
View Code

    'A and B',A为0/False,返回0/False;A为True,返回B的计算值

    'A or B',A为非0/True,返回A值/True;否则返回B的计算值

    'not A',A = 0返回Ture;A为其他值,返回False

 1 print (0 and 12)    #0
 2 print (1 and 12)    #12
 3 print (12 and 12)   #12
 4 print (12 and 0)    #0
 5 print (not 3)
 6 
 7 print (0 or 12)    #12
 8 print (1 or 12)    #1
 9 print (12 or 12)   #12
10 print (12 or 0)    #12
11 
12 print (not 3)       #False
View Code

2、算术运算符

    x/y     x除以y,不取整

    x%y     取余,返回x除以y的余数

    x**y      x的y次幂

    x//y       取整余,返回商的整数部分

1 print(11/3)    #3.6666666666666666
2 print(11%3)    #2
3 print(2**5)    #32
4 print(11//3)   #3
5 print(11.00//3.00)  #3.0
View Code

3、比较运算符

    等于==  

    不等于   !=  或 <>

    其他  >      <      >=      <=

4、赋值运算符

    num = a + b

    num ©= a    等价于    num = num © a

    ©可为:+   —   *   /   %   **   // 

 

posted @ 2016-11-30 17:35  一罐热茶  阅读(371)  评论(0编辑  收藏  举报