Python Note -- Day 2. 运算符、运算符比较级

本文详细介绍了Python中的各种运算符,包括算术、字符串、赋值、比较、逻辑、位运算符,以及它们的用法和实例。重点讲解了and、or、not的逻辑运算,并探讨了二进制与位运算的原理和应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

8.运算符

算 字 赋 比 逻 位 它

  • 8.1 算术运算符

+  -  *  /   %    **(幂)      //取整

  • 8.2 字符串运算

  1. 字符串若和数字使用*,就是重复前面的字符串
    print('1'+'2')
  2. 关于字符串的拼接
    l = 'love'
    i = 'i' + l + 'you'
    print(i)
    f表示将字符串中花括号当作变量
    i = f'i {l} you‘
    i = 'i {} you'.format(l)
    i = 'i {l} you'.format(l=l)
  • 8.3 赋值运算符

a = 20
a += 10; a = a + 10
a -= 10; a = a - 10
a *= 10; a = a * 10
a /= 10; a = a / 10
a %= 10; a = a % 10
a **= 10; a = a ** 10
a //= 10; a = a // 10

:=       海象运算符,可在表达式内部为变量赋值。3.8 新增运算符

if (n := len(a)) > 10: pass

  • 8.4 比较运算符

a = 10
b = 20
print(a == b)
print(a != b)
print(a > b)
print(a < b)
print(a >= b)
print(a <= b)

  • 8.5 逻辑运算符 and or not

a=10, b=20


and,必须都为真,一假则假

print(20 and 30); 30
print(30 and 20); 20
print(20 and 0); 0
print(0 and 20); 0


or,有真则真,全假则假


print(20 or 30); 20
print(30 or 20); 30
print(20 or 0); 20
print(0 or 20); 20


not 非, 取反,真变假,假变真

  • 8.6 二进制与位运算

  1. bin() 可以转换为二进制
    十进制转为二进制,“除二取余,逆序排列”
    125 == 1111101
    print(bin(125))  # 0b1111101
  2. int() 二进制转换为十进制
    按权展开求和
    二进制的数写成加权系数展开式,而后根据十进制的加法规则求和
  3. 位运算
  • & 按位与运算符
    参与运算的两个值,若两个相应为都为1,则该位结果为1,否则为0
  • |  按位或运算符
    只要对应的二个二进位有一个为1,结果位就为1
  • ^ 按位异或运算符
    当两对应的二进位相异时,结果为1
  • << 左移运算符
    运算数的各二进位全部左移若干位,高位丢弃,低位补0
    print(0b0011 << 2)
    print(int(0b1100))
  • >> 右移运算符
  • ~ 按位取反运算符

        对数据的每个二进制位取反,即把1变为0,把0变为1 。~x 类似于 -x-1

  • 8.7 其它运算符

  1. 成员运算符
    in :如果在指定的序列中找到值,返回True,否则False
    s = 'iloveuandherboth'
    s = ['love', 'i', 'you']
    s = ('i', 'love', 'hate')
    s = {1:'i', 2:'love', 3:'u'}; 字典类型,只能检测 键!
    print('love' in s)
    not in : 若在指定序列中没找到,返回False
  2. 身份运算符
  • 2.1 is : 是判断两个标识符是不是引用自一个对象
    x is y,类似id(x)==id(y),若引用自一个对象,返回True
    PS:    id()函数用于获取对象内存地址
    a = 10
    b = 10
    print(a is b)   # False
    is 与 == 区别:
    is用于判断两个变量引用对象是否为同一个,==用于判断引用变量的值是否相等
  • 2.2 is not : 两个标识符是不是引用自不同对象
  • 8.8 运算符优先级

 运算符 not and or 练习题 !

x = True
y = False
z = False

if not x or y:
    print(1)
elif not x or not y and z:
    print(2)
elif not x or y or not y and x:
    print(3)
else:
    print(4)

以上代码输出结果为?
1
2
3
4

已知操作符优先级顺序,not > and > or
 a and  b  若a为False 返回a  否则返回b
 a or   b  若a为True 则返回a 否则返回b
 not a     若a为True 返回False 若a为False,则返回True

我们看上面的表达式一个一个来解
    if not x or y
1:not优先级较高,先运算not x  因为x=True 所以 not x =False
2.语句表达式解为: if False or y      y=False    
3.语句表达式解为: if  False or False
4.根据运算符规则, 该语句解为:False
5.所以if not x or y  返回值为False 当前未命中,进入下一个if判断

-----------------------------------
  if not x  or  not y  and x
1.not优先级较高, x = True  y = False    z = False
2.语句表达式解为: if False or True and False
3.语句中有or和and and优先级较高,先执行and运算
4.语句表达式解为: if False or False
6.语句表达式解为:if False
所以该语句不会执行,进入下个语句判断

------------------------------------------------
    elif not x or y or not y and x:
1.not优先级较高,先进行not运算  x = True  y = False    z = False
2.语句表达式解为: elif False or False or True and True
3.语句表达式有 or和and  根据优先级 and先预算
4.语句表达式解为:elif False or False or True
5.语句只有or 从左到右进行计算
6.语句表达式解为:elif False  or True
7.语句表达式解为:elif True
8.语句表达式解为:elif True 
9.语句为True 执行该段语句

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值