代码规范
缩进4个空格(建议使用空格,不使用Tab键)
每行最长79个字符
类和top_level函数定义之间空两行,类中的方法之间空一行,函数内逻辑无关的语句间空一行
赋值语句
基本赋值,如:x=3
序列赋值,如:a,b,c=’uke’;a,b,c=(1,2,3);[a,b,c]=(1,2,3)
扩展序列解包赋值,如:a,b,*c=’youwin’;c返回的为列表,解包
多目标赋值,如:a=b=0,a变b也变;建议:a,b=0,0,此时b不会跟随a一起变化
表达式
函数调用,如:len(‘abc’)
方法调用,如:list.append(‘a’)
打印操作,如:print(‘hello’,’world’,end=’…\n’,file=open(‘result.txt’,’w’,encoding=’utf-8’);可设置参数:sep,end,file
控制流语句
If判断语句
基本判断
if condition:
execute statement
2.二元判断
if condition:
execute statement
else:
execute statement
3.多元判断
if condition:
execute statement
elif condition:
execute statement
else:
execute statement
4.If嵌套
if condition:
if condition:
execute statement
5.三元运算符
Result=a if conditon else b
while循环语句
while condition:
execute statement:
if condition:
break
#跳出循环
if condition:
continue
#停止此次循环,跳到循环的起始位置
for循环语句
for variable in sequence:
execute statement
2.for variable in range(start,end,step):
execute statement
3.for index,item in enumerate(seq):
execute statement
4.列表推导式
Result=[x for x in range(1,5) if x>2]