What is the purpose of the colon before a block in Python?
Example:
if n == 0:
print "The end"
What is the purpose of the colon before a block in Python?
Example:
if n == 0:
print "The end"
The colon is there to declare the start of an indented block.
Technically, it's not necessary; you could just indent and de-indent when the block is done. However, based on the Python koan “explicit is better than implicit” (EIBTI), I believe that Guido deliberately made the colon obligatory, so any statement that should be followed by indented code ends in a colon. (It also allows one-liners if you continue after the colon, but this style is not in wide use.)
It also makes the work of syntax-aware auto-indenting editors easier, which also counted in the decision.
This question turns out to be a Python FAQ, and I found one of its answers by Guido here:
Why are colons required for the if/while/def/class statements?
The colon is required primarily to enhance readability (one of the results of the experimental ABC language). Consider this:
if a == b print a
versus
if a == b: print a
Notice how the second one is slightly easier to read. Notice further how a colon sets off the example in this FAQ answer; it’s a standard usage in English.
Another minor reason is that the colon makes it easier for editors with syntax highlighting; they can look for colons to decide when indentation needs to be increased instead of having to do a more elaborate parsing of the program text.
Consider the following list of things to buy from the grocery store, written in Pewprikanese.
pewkah
lalala
chunkykachoo
pewpewpew
skunkybacon
When I read that, I'm confused, Are chunkykachoo and pewpewpew a kind of lalala? Or what if chunkykachoo and pewpewpew are indented just because they are special items?
Now see what happens when my Pewprikanese friend add a colon to help me parse the list better: (<-- like this)
pewkah
lalala: (<-- see this colon)
chunkykachoo
pewpewpew
skunkybacon
Now it's clear that chunkykachoo and pewpewpew are a kind of lalala.
Let's say there is a person who's starting to learn Python, which happens to be her first programming language to learn. Without colons, there's a considerable probability that she's going to keep thinking "this lines are indented because this lines are like special items.", and it could take a while to realize that that's not the best way to think about indentation.
Three reasons:
As far as I know, it's an intentional design to make it more obvious, that the reader should expect an indentation after the colon.
It also makes constructs like this possible:
if expression: action()
code_continues()
since having the code for the if
immediately following the colon makes it possible for the compiler to understand that the next line should not be indented.
def long_words(lst): return list(filter((lambda word: len(word) > 5), lst))
works as a one-liner for people who appreciate compactness of simple functions.
Not all redundancies are bad. Python aspires to resemble a natural language. And natural languages are full of redundancies that help us better understand each other. Take, for instance, the capitalization of the first letter in a sentence. The end of a sentence is already clear from the full stop, yet capitalization helps us better grasp when a new sentence starts.
Guido Van Rossum, the creator of Python, already experimented with enhancing structural clarity with a colon for the ABC language — a precursor to Python, also designed for beginners. During initial tests, it became apparent that beginners progressed more rapidly when a colon was introduced.
In the absence of the colon, the meaning of indentation were unclear for beginners learners. The addition of the colon significantly clarified this: somehow, the colon draws attention to what follows, seamlessly tying together the phrases before and after it.
For more details, you can refer to the source: Early Language Design and Development.
While I acknowledge that mature programmers, including myself, initially find this construct unusual and somewhat annoying. Yet we tend to get accustomed to it over time. Nowadays, Python is often one of the first languages that people learn, so for most people the colon is not issue but a visual aid.
Moreover, the colon proves useful in one-liners and is less bothersome than the semicolon.
The colon a bit resembles a binary operator for a compound statement, and improves mark up when the condition of if or while takes multiple lines:
if (one_extra_long_expression !=
that_is_another_thing):
this_thing |= abs(something)
Many programming languages use colons are often used as a separator in ternary if operator, so use of colon as lightweight separator should not be that shocking.
It's worth noting that there are colon-less encodings for CPython, as well as full-fledged colon-less dialects like Cobra, though these have not gained widespread adoption.