1 个回答|234 次阅读
缩进是指代码行开头的空格。
在其他编程语言中,代码中的缩进仅用于可读性,而Python中的缩进非常重要。
Python使用缩进来指示代码块。
例:
if 5 > 2:
print("Five is greater than two!")
1、Python会给你一个错误,如果你跳过缩进:
例:
语法错误:
if 5 > 2:
print("Five is greater than two!")
2、空格的数量取决于您,最常见的是四个空格,至少也得是一个空格。例如下面的例子,没有语法错误,但是建议统一风格,采用惯例的4个空格
例:
if 5 > 2:
print("Five is greater than two!")
if 5 > 2:
print("Five is greater than two!")
3、你必须在同一代码块中使用相同数量的空格,否则Python会给你一个错误:
例:
语法错误:
if 5 > 2:
print("Five is greater than two!")
print("Five is greater than two!")
请先 登录 后评论