运算符
算术运算符
代码:
a=21; b=10; c=0; c=a+b; print("a+b的值为: ",c); c=a-b; print("a-b的值为: ",c); c=a*b; print("a*b的值为: ",c); c=a/b; print("a/b的值为: ",c); #取余 c=a%b; print("a%b的值为: ",c); #幂次方 a=2; b=3; c=a**b; print("a的b次幂为: ",c); #取整 a=10; b=5; c=a//b; print("a除b,取整: ",c);
运行结果:
runfile('D:/新建文件夹/WinPython-64bit-3.3.5.9/settings/.spyder2-py3/My Python Code/key.py', wdir='D:/新建文件夹/WinPython-64bit-3.3.5.9/settings/.spyder2-py3/My Python Code') a+b的值为: 31 a-b的值为: 11 a*b的值为: 210 a/b的值为: 2.1 a+b的值为: 1 a的b次幂为: 8 a除b,取整: 2
关系运算符
代码:
a=21 b=10 c=0 if(a==b): print("a==b"); else: print("a!=b"); if(a!=b): print("a!=b"); else: print("a==b"); if(a>b): print("a>b"); else: print("a<=b"); if(a<b): print("a<b"); else: print("a>=b"); if(a>=b): print("a>=b"); else: print("a<b"); if(a<=b): print("a<=b"); else: print("a>b");
代码结果:
runfile('D:/新建文件夹/WinPython-64bit-3.3.5.9/settings/.spyder2-py3/My Python Code/key.py', wdir='D:/新建文件夹/WinPython-64bit-3.3.5.9/settings/.spyder2-py3/My Python Code') a!=b a!=b a>b a>=b a>=b a>b
赋值运算符
代码
a=21; b=10; c=0; c=a+b; print("a+b的值为:",c); c+=a; print("c+a的值为:",c); c*=a; print("c*b的值为:",c); c/=a; print("c/a的值为:",c); c%=a; print("c%a的值为:",c); c**=a; print("c**a的值为:",c); c//=a; print("c//a的值为:",c);
运算结果:
runfile('D:/新建文件夹/WinPython-64bit-3.3.5.9/settings/.spyder2-py3/My Python Code/key.py', wdir='D:/新建文件夹/WinPython-64bit-3.3.5.9/settings/.spyder2-py3/My Python Code') a+b的值为: 31 c+a的值为: 52 c*b的值为: 1092 c/a的值为: 52.0 c%a的值为: 10.0 c**a的值为: 1e+21 c//a的值为: 4.761904761904762e+19
位运算符
代码:
a=60 # 60 = 0011 1100 b=13 # 13 = 0000 1101 c=0 c=a&b; # 12 = 0000 1100 print ("a&b:",c); c=a|b; # 61 = 0011 1101 print ("a|b:",c); c=a^b; # 49 =0011 0001 print("~a",c); c=a<<2; #-61 = 1100 0011 print ("a<<2:",c); c=a>>2; # 15 = 0000 1111 print ("a>>2:",c);
代码结果:
runfile('D:/新建文件夹/WinPython-64bit-3.3.5.9/settings/.spyder2-py3/My Python Code/key.py', wdir='D:/新建文件夹/WinPython-64bit-3.3.5.9/settings/.spyder2-py3/My Python Code') a&b: 12 a|b: 61 ~a 49 a<<2: 240 a>>2: 15
逻辑运算符
代码:
a=True b=False if(a and b): print("a、b都为True"); else: print("a和b中至少有一个为False"); if(a or b): print("a和b中至少有一个为True"); else: print("a和b都为False"); if(not a): print("a为False"); else: print("a为True");
代码结果:
runfile('D:/新建文件夹/WinPython-64bit-3.3.5.9/settings/.spyder2-py3/My Python Code/key.py', wdir='D:/新建文件夹/WinPython-64bit-3.3.5.9/settings/.spyder2-py3/My Python Code') a和b中至少有一个为False a和b中至少有一个为True a为True
成员运算符
a=10 b=20 list=[10,58,65,25,47] if(a in list): print("a在列表list中"); else: print("a不在列表list中"); if(b in list): print("b在列表list中"); else: print("b不在列表list中"); if(a not in list): print("a不在列表list中"); else: print("a在列表list中"); if(b not in list): print("b不在列表list中"); else: print("b在列表list中");
代码结果:
runfile('D:/新建文件夹/WinPython-64bit-3.3.5.9/settings/.spyder2-py3/My Python Code/key.py', wdir='D:/新建文件夹/WinPython-64bit-3.3.5.9/settings/.spyder2-py3/My Python Code') a在列表list中 b不在列表list中 a在列表list中 b不在列表list中
身份运算符
a=20 b=20 if(a is b): print("a和b指向同一个对象"); else: print("a和b指向不同的对象"); if(a is not b): print("a和b指向不同的对象"); else: print("a和b指向同一个对象"); b=30 if(a is b): print("a和b指向同一个对象"); else: print("a和b指向不同的对象"); if(a is not b): print("a和b指向不同的对象"); else: print("a和b指向同一个对象");
代码结果:
runfile('D:/新建文件夹/WinPython-64bit-3.3.5.9/settings/.spyder2-py3/My Python Code/key.py', wdir='D:/新建文件夹/WinPython-64bit-3.3.5.9/settings/.spyder2-py3/My Python Code') a和b指向同一个对象 a和b指向同一个对象 a和b指向不同的对象 a和b指向不同的对象
运算符优先级