Python:运算符

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Zzwtyds/article/details/89068780
# 赋值运算符
a = 1
b = 2
c = 3
d = 4
e = 23
f = 32
g = 99
# 加法运算符
# a = a + 1
a+=1
print(a)

str="abc"
str+="bcd"
print(str) #拼接

b-=3 # b=b-3
print(b)
c*=3
print(c)
d/=10
print(d)

# 取模,幂运算,取整数
e %=4       # 取模
print(e)

f**=2       # 幂运算
print(f)

g//=2       # 取整数
print(g)

# 成员运算符号 in   /  not in  在指定序列中找到值
sheet=["张三","李四","王五"]
if("张三" in sheet):
    print("张三在队列中")
else:
    print("张三没有在队列中")

if("林六" not in sheet):
    print("林六未在队列中")
else:
    print("林六在队列中")

# 身份运算符 is    /   is not  判断两个变量是否用同一个对象(同一内存地址)类似java中的isequal和==
h = 43
i = h
j = 43.0

print(h is i)    # 对象(地址)等
print(h == j)    # 数值等
print(h is j)

# 二进制位运算符
"""  
& 按位与   
| 按位或   
^ 按位异或 
~ 按位取反 
<< 左移动运算符
>> 右移动运算符
"""
m = 60 # 00111100
n = 13 # 00001101

h1= m & n
h2= m | n
h3= m ^ n

h1= m & n
print(h1) # 00001100 =12

h2= m | n
print(h2) # 00111101 =61

h3= m ^ n
print(h3) # 00111101 =49

h4= ~ m
# 11000011 =195
# -(~11000011+1)
# -(00111100+1)
# -00111101 = -61
print(h4)

h5= m << 3
# 00111100
# 00111100000 =480
print(h5)

h6= m >> 3
# 00111100
# 00000111100 =7
print(h6)







 

运行结果:

 

展开阅读全文

没有更多推荐了,返回首页