python-基本运算符(解压缩-必考)
程序员文章站
2022-05-29 11:46:35
基本运算符 算术运算符 30 10 200 0.5 10 2 10240000000000 比较运算符 True False True True python a=300 print(a) python a +=100 print(a) python a =10 print(a) python a ......
基本运算符
算术运算符
x =10 y =20 print(x+y)
30
print(x-y)
-10
print(x*y)
200
print(x/y)
0.5
print(x%y)#取余
10
print(y//x) #取整
2
print(y**x) #这标识的是20的10次方
10240000000000
比较运算符
a=10 print(a==10)
true
print(a!=10) #!=标识不等于
false
print(a>9)
true
print(a<9)
false
print(a>=9)
true
print(type(a<=9))
<class 'bool'>
赋值运算符
a=300 print(a)
300
a +=100 print(a)
400
a *=10 print(a)
4000
a /=10 print(a)
400.0
a //=10 print(a)
40.0
逻辑运算符
and 、or、not
and:两个都是true才是true,否则为false
print(2>3 and 3>2)
false
print(4>3 and 3>2)
true
or 是两个之中有一个是true,就是true
print(2>3 or 2>1)
true
print(2>3 or 2>4)
false
not 是不是的意思(很少用)
print(not 2>3 or 1>2)
true
身份运算符(了解)
x = 258 y = 258 print(id(x),id(y)) print(id(x)==id(y))
2956579849680 2956579849136 false
print(x is y)
false
链式赋值(周考必考)
x=y=z=10 print(x,y,z)
10 10 10
交叉赋值 (周考必考)
x=10 y=20 x,y = y,x print(x,y)
20 10
解压缩 (周考必考)
快速且能一次性拿到多个列表里的值
duan_hobby_list = ['read','run'] hobby1 = duan_hobby_list[0] hobby2 = duan_hobby_list[1] print(hobby1,hobby2)
read run
hobby1,hobby2 = duan_hobby_list print(hobby1,hobby2)
read run
duan_hobby_list1 = ['read','run','sing','football','basketball'] hobby1,hobby2,_,_,_= duan_hobby_list1 print(hobby1,hobby2)
read run
hobby1,hobby2,*_ = duan_hobby_list1 # “*”代表后面所有对应赋值的位置 print(hobby1,hobby2)
read run
hobby1,*_,hobby5 = duan_hobby_list1 print(hobby1,hobby5)
read basketball
上一篇: C语言----选择结构(基础篇三)
下一篇: 前端之CSS创建的样式
推荐阅读