0
Why colon is used in if statement
3 Réponses
+ 2
Colon tells that the following code belongs to a statement.
In:
if condition:
statement1
statement2
statement1 would only be run, when:
1: program reaches the "if condition:" line.
and:
2: condition evaluates to True.
statement2 don't have anything to do with the if statement.
+ 2
The colon separates the condition from the body, the body of the if statement does not necessarily have to start indented on a new line. For example:
if condition: doStuff(); moreStuff();
+ 1
It is the Python syntax, it allows for better readability.