python-基本运算符(解压缩-必考)

2023-01-31 00:01:43 python

算术运算符

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

相关文章