码迷,mamicode.com
首页 > 编程语言 > 详细

python运算符

时间:2018-07-13 15:06:13      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:比较   情况下   包括   列表   技术   字符   pre   没有   png   

python中的运算符包括多种多样的,可以分为以下几个大类

第一种:算数运算

技术分享图片

第二种:比较运算

技术分享图片

第三种:赋值运算

技术分享图片

第四种:逻辑运算

技术分享图片

1,优先级

在没有()的情况下not 优先级高于 and,and优先级高于or,即优先级关系为( )>not>and>or,同一优先级从左往右计算。
例题:
判断下列逻辑语句的True,False。
or中有一个为真,则为True 
and中有一个为假,则为false
3>4 or 4<3 and 1==1        True
1 < 2 and 3 < 4 or 1>2        True
2 > 1 and 3 < 4 or 4 > 5 and 2 < 1        True
1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8        False
1 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6        False
not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 <6    False
print(1>2 and 3 or 4 and 3 < 2) == 0

2 ,or 和and

x or y , x为真,值就是x,x为假,值是y;非0转换为布尔值为True,0转换为布尔值为False,除了0都显示前一位
x and y, x为真,值是y,x为假,值是x。除了0,显示都是后一位
print(int(True))  == 1
print(int(False))    ==0
8 or 4 8
0 and 3 3
0 or 4 and 3 or 7 or 9 and 6 3
print(2 or 1 < 3) == 2
print(2 or 1 < 3 and 2) == 2

第五种:成员运算

 技术分享图片

in,not in :
判断子元素是否在原字符串(字典,列表,集合)中:
例如:

#print(‘喜欢‘ in ‘dkfljadklf喜欢hfjdkas‘)
#print(‘a‘ in ‘bcvd‘)
#print(‘y‘ not in ‘ofkjdslaf‘)

第六种:身份运算

技术分享图片

第七种:位运算

 技术分享图片

a = 60            # 60 = 0011 1100
b = 13            # 13 = 0000 1101
c = 0
  
c = a & b;        # 12 = 0000 1100
print "Line 1 - Value of c is ", c
  
c = a | b;        # 61 = 0011 1101
print "Line 2 - Value of c is ", c
  
c = a ^ b;        # 49 = 0011 0001 #相同为0,不同为1
print "Line 3 - Value of c is ", c
  
c = ~a;           # -61 = 1100 0011
print "Line 4 - Value of c is ", c
  
c = a << 2;       # 240 = 1111 0000
print "Line 5 - Value of c is ", c
  
c = a >> 2;       # 15 = 0000 1111
print "Line 6 - Value of c is ", c

总结运算符的优先级

技术分享图片

 

python运算符

标签:比较   情况下   包括   列表   技术   字符   pre   没有   png   

原文地址:https://www.cnblogs.com/Arthur7/p/9304627.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!