atom feed2 messages in[Edu-sig] Why colon?
FromSent OnAttachments
Jeff SandysMar 14, 2002 3:54 pm 
Guido van RossumMar 15, 2002 9:11 am 
Subject:[Edu-sig] Why colon?
From:Guido van Rossum (
Date:Mar 15, 2002 9:11:30 am

What is the use of the colon in def and if? It only seems to signal, indent the next line, but if the next line is indented anyway, what is the colon's purpose?

We have a semi-colon to put multiple statements on a line, why not use that for a single-line if,

if x > 0 ; print "positive" else ; print "negative"

Then when we use multiple lines we still indent,

if x > 0 print "positive" else print "negative"

It is a common error of new Python programmers to leave out the colon after def, if and else, and I can't explain why the colon is needed.

This is a FAQ (but anyway, it's too late to change):

The colon is required primarily to enhance readability (one of the results of the expirimental ABC language). Consider this:

if a==b

print a


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 the second line of this FAQ answer; it's a standard usage in English. Finally, the colon makes it easier for editors with syntax highlighting.