# 简单赋值运算符 if true: a=51 b=len('cto') print('a值:'+str(a)) # 加法赋值运算符 if true: a=51 b=len('cto') a+=b print('a+=b值:'+str(a)) # 减法赋值运算符 if true: a=51 b=len('cto') a-=b print('a-=b值:'+str(a)) # 除法赋值运算符 if true: a=51 b=len('cto') a/=b print('a/=b值:'+str(a)) # 取模赋值运算符 if true: a=51 b=len('cto') a%=b print('a%=b值:'+str(a)) # 幂赋值运算符 if true: a=len('51') b=len('cto') a**=b print('a**=b值:'+str(a)) # 取整赋值运算符 if true: a=51 b=len('cto') a//=b print('a//=b值:'+str(a))
效果如下:
三、位运算符
编号
位运算符
说明
1
&
按位与运算符,两者均1则为1,否则为0
2
|
按位或运算符,两者其一为1则为1,否则为0
3
^
按位异或运算符
4
~
按位取反运算符
5
<<
左移动运算符
6
>>
右移动运算符
代码如下:
# 十进制转二进制,[2:]去掉0b,int将字符串转为整型 a=int(bin(51)[2:]) b=int(bin(len('cto'))[2:]) print('a=51=二进制值:'+str(a)) print('b=3=二进制值:'+str(b)) # a=110011 # b=000011=11 # 按位与运算符 if true: print('a&b值:'+str(a&b)) # 按位或运算符 if true: print('a|b值:'+str(a|b)) # 按位异或运算符 if true: print('a^b值:'+str(a^b)) # 按位取反运算符 if true: print('~a值:'+str(~a)) # 左移动运算符 if true: print('a<<2值:'+str(a<<2)) # 右移动运算符 if true: print('a>>2值:'+str(a>>2))
效果如下:
四、逻辑运算符
编号
逻辑运算符
说明
1
and
与运算符,a和b为true,才为true
2
or
或运算符,a和b其中一个为true,则为true
3
not
非运算符,a为false,则为true
代码如下:
a=len(str(51))<0 b=len('cto')>0 print('a值:'+str(a)) print('b值:'+str(b)) # 与运算符 if true: print('a and b值:'+str(a and b)) # 或运算符 if true: print('a or b值:'+str(a or b)) # a非运算符 if true: print('not(a)值:'+str(not(a)))
效果如下:
五、成员运算符
编号
成员运算符
说明
1
in
a在b中,则返回true
2
not in
a不在b中,则返回true
代码如下:
a='c' b=[5,1,'c','t','o'] print('a值:'+str(a)) print('b值:'+str(b)) # in运算符 if true: print('a in b值:'+str(a in b)) # not in运算符 if true: print('a not in b值:'+str(a not in b))
效果如下:
成员运算符
编号
成员运算符
说明
1
is
a和b是否一样,则返回true
2
is not
a和b不是一样,则返回true
代码如下:
a='c' b=[5,1,'c','t','o'] print('a值:'+str(a)) print('b值:'+str(b)) # in运算符 if true: print('a in b值:'+str(a in b)) # not in运算符 if true: print('a not in b值:'+str(a not in b))#!/usr/bin/python3 # -*- coding: utf-8 -*- # feb 12, 2022 11:00 am a='51cto ' b=' 51cto' print('a值:'+str(a)) print('b值:'+str(b)) # is运算符 if true: print('a is b值:'+str(a is b)) # is not运算符 if true: print('a is not b值:'+str(a is not b)) # is运算符,清除空格 a=a.strip() b=b.strip() if true: print('a is b值:'+str(a is b)) # is运算符,清除空格 c='51cto' d='51cto' if true: print('c is d值:'+str(c is d))