算数运算
res=1 + 3
print(res)
print(1 + 3)
print(10 / 3) #结果保留小数部分
print(10 // 3) # 只保留整数部分
print(10 % 3) # 取余数
print(2**3)
比较运算:== != > < >= <=
了解知识:
数字之间可以互相比较大小
print(10 > 3)
print(10 > 3.1)
而字符串只能与字符串比较大小(按照对应位置的字符参考ASCII表去比较的)
msg1='hello'
msg2='z'
print(msg1 > msg2)
A-Za-z
print('a' > 'Z')
print('Z' > 'Y')=
print(len('hello') > 3)
print('a' > 3)
列表只能与列表比较大小(按照对应位置的值依次比较,对应位置的值必须是相同的类型)
l1=[1,2,3]
l2=[10,]
print(l2 > l1)
l3=[10,2,'b',3]
l4=[10,2,'b','c']
print(l3 > l4)
赋值运算
age=18
增量赋值
age+=1 #age=age+1
print(age)
链式赋值
x=100
y=x
z=x
x=z=y=100
print(id(x),id(y),id(z))
交叉赋值
m=1000
n=2000
temp=m
m=n
n=temp
n,m=m,n
print(m,n)
解压赋值
salaries=[11,22,33,44,55]
mon1=salaries[0]
mon2=salaries[1]
mon3=salaries[2]
mon4=salaries[3]
mon5=salaries[4]
mon1,mon2,mon3,mon4,mon5=salaries
print(mon1,mon2,mon3,mon4,mon5)
等号右面包含的值的个数必须与等号左边变量名的个数一致
mon1,mon2,mon3,mon4,mon5,mon6=salaries
mon1,mon2,mon3,mon4,=salaries
_=3333
print(_)
mon1,mon2,_,_,_=salaries
mon1,mon2,*_=salaries
print(mon1)
print(mon2)
salaries=[11,22,33,44,55,]
first=salaries[0]
last=salaries[4]
first,_,_,_,last=salaries
first,*_,last=salaries
print(first)
print(last)
逻辑运算
and:连接左右两个条件,只有在两个条件同时成立的情况下最终结果才为True
print(age > 18 and age < 26 and sex == 'female' and 1 > 3)
or:连接左右两个条件,但凡有一个条件成立最终结果就为True
print(1 > 3 or 2 > 4 or 'x' == 'y' or 1==1)
not
print(not 1 > 3)
print(not (1 > 3 or 2 > 4 or 'x' == 'y' or 1==1))
res=(3>4 and 4>3) or (1==3 and ('x' == 'x' or 3 >3))
print(res)