Python 缩进
缩进指的是代码行开头的空格。
在其他编程语言中,代码缩进仅出于可读性的考虑,而 Python 中的缩进非常重要。
Python 使用缩进来指示代码块。
实例
if 5 > 2:
print("Five is greater than two!")
如果省略缩进,Python 会出错:
实例
语法错误:
if 5 > 2:
print("Five is greater than two!")
空格数取决于程序员,但至少需要一个。
实例
if 5 > 2:
print("Five is greater than two!")
if 5 > 2:
print("Five is greater than two!")
您必须在同一代码块中使用相同数量的空格,否则 Python 会出错:
实例
语法错误:
if 5 > 2:
print("Five is greater than two!")
print("Five is greater than two!")