python3基础学习与巩固(2) 运算符

运算符是用于对各类变量及值进行各种操作。

这里就按照W3school里面的(Python运算符)顺序和表格加强学习和巩固以下吧

运算符类型

  • 算术运算符
  • 赋值运算符
  • 比较运算符
  • 逻辑运算符
  • 身份运算符
  • 成员运算符
  • 位运算符

一、Python算数运算符

1.算数运算符有哪些?

算术运算符与数值一起使用来执行常见的数学运算

运算符名称表达式示例(x=2,y=3,z=0)
+x + yz=x+y;    结果5
-x - yz=x-y;    结果-1
*x * yz=x*y;    结果6
/x / yz=x/y;    结果0.6666666666666666
%取模x % yz=x%y;    结果2
**x ** yz=x**y    结果8
//地板除(取整除)x // yz=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 = 5x = 5
+=x += 3x = x + 3
-=x -= 3x = x - 3
*=x *= 3x = x * 3
/=x /= 3x = x / 3
%=x %= 3x = x % 3
//=x //= 3x = x // 3
**=x **= 3x = x ** 3
&=x &= 3x = x & 3
|=x |= 3x = x | 3
^=x ^= 3x = x ^ 3
>>=x >>= 3x = x >> 3
<<=x <<= 3x = 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,则返回 Falsenot(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

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

空中旋转篮球

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值