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