运算符是用于对各类变量及值进行各种操作。
这里就按照W3school里面的(Python运算符)顺序和表格加强学习和巩固以下吧
运算符类型
- 算术运算符
- 赋值运算符
- 比较运算符
- 逻辑运算符
- 身份运算符
- 成员运算符
- 位运算符
一、Python算数运算符
1.算数运算符有哪些?
算术运算符与数值一起使用来执行常见的数学运算
运算符 | 名称 | 表达式 | 示例(x=2,y=3,z=0) |
---|---|---|---|
+ | 加 | x + y | z=x+y; 结果5 |
- | 减 | x - y | z=x-y; 结果-1 |
* | 乘 | x * y | z=x*y; 结果6 |
/ | 除 | x / y | z=x/y; 结果0.6666666666666666 |
% | 取模 | x % y | z=x%y; 结果2 |
** | 幂 | x ** y | z=x**y 结果8 |
// | 地板除(取整除) | x // y | z=x//y; 结果0 |
2.算数运算符示例及运行结果
x=2; y=3; z=0;
print("测试数据x,y,z分别为:",x,y,z)
print("加减乘除运算:")
print("x+y=",x+y);print("x-y=",x-y);print("x*y=",x*y);print("x/y=",x/y);
print("取模、幂、地板除取整运算:")
print("x%y=",x%y);print("x**y=",x**y);print("x//y=",x//y);
运行结果:
测试数据x,y,z分别为: 2 3 0
加减乘除运算:
x+y= 5
x-y= -1
x*y= 6
x/y= 0.6666666666666666
取模、幂、地板除取整运算:
x%y= 2
x**y= 8
x//y= 0
二、Python 赋值运算符
1.赋值运算符有哪些
运算符 | 实例 | 等同于 |
---|---|---|
= | x = 5 | x = 5 |
+= | x += 3 | x = x + 3 |
-= | x -= 3 | x = x - 3 |
*= | x *= 3 | x = x * 3 |
/= | x /= 3 | x = x / 3 |
%= | x %= 3 | x = x % 3 |
//= | x //= 3 | x = x // 3 |
**= | x **= 3 | x = x ** 3 |
&= | x &= 3 | x = x & 3 |
|= | x |= 3 | x = x | 3 |
^= | x ^= 3 | x = x ^ 3 |
>>= | x >>= 3 | x = x >> 3 |
<<= | x <<= 3 | x = x << 3 |
2.赋值运算符示例及运行结果
x=5;print("x=",x);
x+=5;print("x=",x);
x-=5;print("x=",x);
x*=-1;print("x=",x);
x/=-5;print("x=",x);
x%=5;print("x=",x);
x//=5;print("x=",x);
x=5;
x**=2;print("x=",x);
x&=3;print("x=",x);
x|=3;print("x=",x);
x=5
x^=3;print("x=",x);
x>>=5;print("x=",x);
x<<=5;print("x=",x);
运行结果:
x= 5
x= 10
x= 5
x= -5
x= 1.0
x= 1.0
x= 0.0
x= 25
x= 1
x= 3
x= 6
x= 0
x= 0
三、比较运算符
1.比较运算符有哪些
运算符 | 名称 | 实例 |
---|---|---|
== | 等于 | x == y |
!= | 不等于 | x != y |
> | 大于 | x > y |
< | 小于 | x < y |
>= | 大于或等于 | x >= y |
<= | 小于或等于 | x <= y |
2.比较运算符示例及运行结果
x=5; y=8;
if x==y:
print("x=y")
if x>y:
print("x>y")
if x<y:
print("x<y")
if x>=y:
print("x>y")
elif x<=y:
print("x<y")
else:
print("x!=y")
运行结果:
x<y
x<y
四、逻辑运算符
1.逻辑运算符有哪些
运算符 | 描述 | 实例 |
---|---|---|
and | 如果两个语句都为真,则返回 True。 | x > 3 and x < 10 |
or | 如果其中一个语句为真,则返回 True。 | x > 3 or x < 4 |
not | 反转结果,如果结果为 true,则返回 False | not(x > 3 and x < 10) |
2.逻辑运算符示例及运行结果
x=5; y=8;a=5;b=7;
if x==y and a==b:
print("x=y")
if x>y and a<b:
print("x>y")
if x<y and a<b:#输出结果
print("x<y and a<b")
if x<y or a>b:#输出结果
print("x<y or a>b")
if not(x>y or a>b):#输出结果
print("x<y or a>b")
if x>=y or a>=b:
print("x>y")
elif x<=y:#输出结果
print("x<y")
else:
print("x!=y")
运行结果:
x<y and a<b
x<y or a>b
x<y or a>b
x<y
五、身份运算符
1.身份运算符有哪些
运算符 | 描述 | 实例 |
---|---|---|
is | 如果两个变量是同一个对象,则返回 true。 | x is y |
is not | 如果两个变量不是同一个对象,则返回 true。 | x is not y |
2.身份运算符示例及运行结果
#数字验证
x=5; y=5;z=8;a=x;
if x is y:
print("x is y")
if x is z:
print("x is z")
elif x is not z:
print("x is not z")
if x is a:
print("x is a")
else:
print("x is not a")
#字符串验证
print("字符串比较")
x=["China","USA","Rusia"];y=["China","USA","Rusia"];z=x;
if x is y:
print("x is y")
if x is z:
print("x is z")
else:
print("x is not z")
if x is a:
print("x is a")
else:
print("x is not a")
运行结果:
x is y
x is not z
x is a
字符串比较
x is z
x is not a
六、成员运算符
1.成员运算符有哪些
运算符 | 描述 | 实例 |
---|---|---|
in | 如果对象中存在具有指定值的序列,则返回 True。 | x in y |
not in | 如果对象中不存在具有指定值的序列,则返回 True。 | x not in y |
2.成员运算符示例及运行结果
x=["China","USA","Rusia"];y="Chian" ;
if "China" in x:
print("China is in y")
if "C" in y:
print("C in y")
elif "C" in y:
print("C is not in y")
运行结果:
China is in y
C in y
七、位运算符
1.位运算符有哪些
运算符 | 描述 | 实例 |
---|---|---|
& | AND | 如果两个位均为 1,则将每个位设为 1。 |
| | OR | 如果两位中的一位为 1,则将每个位设为 1。 |
^ | XOR | 如果两个位中只有一位为 1,则将每个位设为 1。 |
~ | NOT | 反转所有位。 |
<< | Zero fill left shift | 通过从右侧推入零来向左移动,推掉最左边的位。 |
>> | Signed right shift | 通过从左侧推入最左边的位的副本向右移动,推掉最右边的位。 |
2.位运算符示例及运行结果
a=5;b=13;
print("a=",a);print("b=",b);
print(bin(a));print(bin(b));#输出二进制,下面顺便输出8进制和16进制
print(oct(a));print(oct(b));print(hex(a));print(hex(b));
print("下面是位运算结果:")
z=a&b;print("a&b:",z);
z=a|b;print("a|b:",z);
z=a^b;print("a^b:",z);
z=~a;print("z=~a:",z);
z=a<<b;print("a<<b:",z);
z=a>>b;print("a>>b:",z);
运行结果:
a= 5
b= 13
0b101
0b1101
0o5
0o15
0x5
0xd
下面是位运算结果:
a&b: 5
a|b: 13
a^b: 8
z=~a: -6
a<<b: 40960
a>>b: 0