基本运算符

基本运算符

【一】算数运算符

image

(1)加法

x = 9
y = 2
result = x + y
print(result) # 输出:11

(2)减法

x = 9
y = 2
result = x - y
print(result) # 输出:7

(3)乘法

x = 9
y = 2
result = x * y
print(result) # 输出:18

(4)除法

x = 9
y = 2
result = x / y
print(result) # 输出:4.5

(5)取整除

x = 9
y = 2
result = x // y
print(result) # 输出:4

(6)求余数

x = 9
y = 2
result = x % y
print(result) # 输出:1

(7)幂运算

x = 9
y = 2
result = x ** y
print(result) # 输出:81

【二】比较运算符

  • 比较运算用来对两个值进行比较,返回的值是True或False

image

【三】赋值运算符

image

(1)链式赋值

  • 链式赋值允许在单一语句中给多个变量赋值
a = b = c = 5
print(a, b, c) # 5 5 5

(2)交叉赋值

  • 交叉赋值允许在不使用临时变量的情况下,交换两个变量的值。
x = 9
y = 2
# 交叉赋值
x, y = y, x
print(x, y) # 输出:2 9

(3)解压赋值

  • 解压赋值允许将一个可迭代对象(如列表、元组等)的值解压到多个变量中
aa = (3, 5)
# 解压赋值
x, y = aa
print(x, y) # 输出:3 5

【四】逻辑运算符

  • 逻辑运算符用于连接多个条件,进行关联判断,并返回布尔值 TrueFalse
  • 常用的逻辑运算符包括与(and)、或(or)、非(not)。

image

(1)优先级

  • 在逻辑运算中,优先级为not>and>or

【五】成员运算符

image

namelist = ['heart', 'Tom']
print('heart' in namelist) # True
print('Tom' not in namelist) # False

【六】身份运算符

  • is/is not 这两个运算符主要用于检查对象的标识,即它们是否指向相同的内存地址

image

#is/is not
a = [1, 2, 3]
b = a
c = [1, 2, 3]
print(a is b) # True
print(a is c) # False
print(a is not b) # False
print(a is not c) # True

本文作者:heart

本文链接:https://www.cnblogs.com/ssrheart/p/17864150.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   ssrheart  阅读(17)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起