“Python/gloss python indentation”的版本间差异
来自菜鸟教程
小 (Pywikibot 4.4.0.dev0) |
小 (机器人:添加分类Python基础教程) |
||
第72行: | 第72行: | ||
<br /> | <br /> | ||
<br /> | <br /> | ||
+ | |||
+ | [[分类:Python基础教程]] |
2020年10月29日 (四) 08:48的最新版本
<languages />
Python缩进
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!")