1、 在二元运算符两边各空一格[=,-,+=,==,>,in,is not, and]:
# 正确的写法
i = i + 1
submitted += 1
x = x * 2 - 1
hypot2 = x * x + y * y
c = (a + b) * (a - b)
# 不推荐的写法
i=i+1
submitted +=1
x = x*2 - 1
hypot2 = x*x + y*y
c = (a+b) * (a-b)
2、 函数的参数列表中,,之后要有空格
# 正确的写法
def complex(real, imag):
pass
# 不推荐的写法
def complex(real,imag):
pass
3、 函数的参数列表中,默认值等号两边不要添加空格
# 正确的写法
spam(ham[1], {eggs: 2})
# 不推荐的写法
spam( ham[1], { eggs : 2 } )
4、 左括号之后,右括号之前不要加多余的空格
# 正确的写法
spam(ham[1], {eggs: 2})
# 不推荐的写法
spam( ham[1], { eggs : 2 } )
5、 字典对象的左括号之前不要多余的空格
# 正确的写法
dict['key'] = list[index]
# 不推荐的写法
dict ['key'] = list [index]
6、 不要为对齐赋值语句而使用的额外空格
# 正确的写法
x = 1
y = 2
long_variable = 3
# 不推荐的写法
x = 1
y = 2
long_variable = 3
7、 不要在逗号, 分号, 冒号前面加空格, 但应该在它们后面加(除了在行尾)
Yes: if x == 4:
print x, y
x, y = y, x
No: if x == 4 :
print x , y
x , y = y , x