python基础教程

Python基础python行与缩进  

  python 行与缩进,本篇python基础教程介绍python 的行、缩进。Python 3 最具特色的是用缩进来标明成块的代码,不需要使用大括号 {} ,同一个代码块的语句必须包含相同的缩进空格数。

python行与缩进

#!/usr/bin/python3


if True:
    print ("True")
else:
    print ("False")

  Python代码最后一行语句缩进数的空格数不一致,会导致运行错误:

#!/usr/bin/python3

 
if True:
    print ("Answer")
    print ("True")
else:
    print ("Answer")
  print ("False")    # 缩进不一致,会导致运行错误#


运行结果:
File "test.py", line 6
    print ("False")   
                                      ^
IndentationError: unindent does not match any outer indentation level

  Python 代码四个空格的缩进来表示隶属关系的书写方式,强制缩进增强了程序的可读性。

  推荐:常用的50个Python模块列表

python多行语句

  Python 通常是一行写完一条语句,但如果语句很长,我们可以使用反斜杠(\)来实现多行语句,例如:

keywords = wordpress_theme + \
        wordpress_plugin + \
        pythonthree.com

  在 列表[], 字典{}, 或元组 () 中的多行语句,不需要使用反斜杠(\),例如:

keywords = ['wordpress_theme', 'wordpress_plugin', 'pythonthree.com',
        'www.pythonthree.com', '晓得博客']

  推荐阅读:零基础如何开始学习Python

给文章评分

晓得博客,版权所有丨如未注明,均为原创
晓得博客 » Python基础python行与缩进  

转载请保留链接:https://www.pythonthree.com/python-basic-line-and-indent/

Claude、Netflix、Midjourney、ChatGPT Plus、PS、Disney、Youtube、Office 365、多邻国Plus账号购买,ChatGPT API购买,优惠码XDBK,用户购买的时候输入优惠码可以打95折

Chatgpt-Plus注册购买共享账号
滚动至顶部