What is the purpose of the colon before a block in Python?

后端 未结 5 1966
孤街浪徒
孤街浪徒 2020-11-28 09:26

What is the purpose of the colon before a block in Python?

Example:

if n == 0:
    print \"The end\"
5条回答
  •  情话喂你
    2020-11-28 10:22

    Three reasons:

    1. To increase readability. The colon helps the code flow into the following indented block.
    2. To help text editors/IDEs, they can automatically indent the next line if the previous line ended with a colon.
    3. To make parsing by python slightly easier.

提交回复
热议问题