Operators,Expressions
Python的优先级表,从最低优先级(最少绑定)到最高优先级(最多绑定)
lambda
:Lambda表达式if - else
:有条件的表达or
:布尔ORand
:布尔ANDnot x
:布尔NOTin, not in, is, is not, <, <=, >, >=, !=, ==
:比较,包括成员资格测试和身份测试|
:按位或^
:按位XOR&
:按位与<<, >>
: 转变+, -
: 加减*, /, //, %
:乘法,除法,楼层划分和剩余部分+x, -x, ~x
:正,负,按位不**
:求幂x[index], x[index:index], x(arguments...), x.attribute
:订阅,切片,调用,属性参考(expressions...), [expressions...], {key: value...}, {expressions...}
:绑定或元组显示,列表显示,字典显示,设置显示
a=13
b=3
print(a/b,a//b,a%b,b**a) #除,取整,求余,几次幂
4.333333333333333 4 1 1594323
对象的引用,is判断对象的内存地址是否相同,==判断对象是否相等
a=[1,2,3]
b=a #对象的引用
b.pop()
print(a==b,a is b,a) #true,true,a的值发生改变
True True [1, 2]
a=[1,2,3]
b=[1,2,3] #声明两个值相等但内存地址不等的对象
print(a==b,a is b) #true,false
b.pop()
print(a) #a的值不变
True False
[1, 2]
Control Flow
if(如果),elif(如果),else(否者)
number=23
guess=int(input('enter an inter: '))
if guess==number:print('contant')
elif guess>number:print('too high')
else:print('too lower')
print('done')
enter an inter: 26
too high
done
while只要条件成立,即执行语句,且只能有一个else再最后是使用,表示当while后的条件不成立时,执行的语句
number=23
runing=23
while runing==23:guess=int(input('enter an integer: '))if guess==number:print('contant')runing=23elif guess<number:print('too lower')runing=20else:print('too high')runing=30
else:print('done')
print(runing)
enter an integer: 26
too high
done
30
for 遍历序列的每个项目
for i in range(1, 5):print(i)
else:print('The for loop is over')
1
2
3
4
The for loop is over
break 终止本层循环,[注]:如果终止for或while循环,则else语句不执行
for i in range(1, 5):print(i)if i==3:break
else:print('The for loop is over')
1
2
3
continue 表示跳出当前循环
while True:s=input('enter something: ')if s=='quit':breakif len(s)<3:print('too small')continueprint('try again')
enter something: 2A
too small #并未出现try again,跳出当前循环,表示此次循环结束,重新开始
enter something: ADFSAF
try again
enter something: DSAFS
try again