Indentation and conditional statements#
(Click here for the German version of this page)
One unique trait, which sets Python apart, are the curly braces, or more fittingly, the lack thereof.
Most programming languages use curly braces to define code blocks.
Option 1:
if (Condition) {
/* This code block is only executed in case condition holds */
}
/* This code will be executed regardless */
but it is also possible to reformat the code accordingly:
if (Condition) {
/* This code block is only executed in case condition holds */
}
/* This code will be executed regardless */
or Option 3:
if (Condition) {/* This code block is only executed in case condition holds*/}
/*This code will be executed regardless*/
Question:
Which of those three options is the most readable?
Answer:
Option 1 is the most readable, since the code blocks the condition and those independent of the condition are separated more visually, without having to pay attention to the curly braces.
The C Style Guides also agree with me, that indentation is important.
It is almost as if the braces can be omitted if proper indentation is present. Python: Yes. Curly braces in Python are not used to define code blocks. Instead, they are only used for dictionaries and sets. Dictionaries and sets will be discussed later in this introduction.
How does Python differentiate code blocks exactly? By indentation (spaces or tabs).
Option 1 in a Python-like syntax would look something like this:
if (Condition)
<This code block is only executed in case condition holds>
<This code will be executed regardless>
To differentiate the condition from the code block of the condition, an intuitive solution would be a colon. That is a relatively natural way to indicate a condition.
if (this is true): # then do
<that>
<Proceed here afterwards, no matter if "that" has been done or not>
Almost like a sticky note.
if this is true:
do that
and this
and that
In contrast to other programming languages, round brackets are also optional for if statements.
if (x is true):
<do something>
if x is true:
<do something>
Round brackets are only needed if a condition spans across multiple lines.
# Correct:
if (x is true
and y is true):
<do something>
# Incorrect:
if x is true
and y is true:
<do something>
Now we have already discussed how if statements work in Python.
if CONDITION:
<CODE BLOCK OF CONDITION>
<REST OF PROGRAM>
One must be consistent with correct indentation. Every space and every tab counts. Every part of the same code black must have the same amount of indentation.
# Incorrect:
if x is true:
do this
and this
and that
# Correct:
if x is true:
do this
and this
and that
For the code block to be executed in case the condition is not fulfilled, the keyword else is used. If one wants to create an else-if, then one can use the Python keyword elif.
This works like this:
if x == 1:
print("x is 1")
elif x == 2:
print("x is 2")
else:
print("x is neither 1 nor 2")
Exercise#
A slightly more complex example first in C:
if (x is true) {
if (y is true) {
if (z is true) {
<Code_block_1>
}
}
else {
<Code_block_2>
}
}
else {
<Code_block_3>
}
<Rest of program...>
Now it is your turn. Try to rewrite the above C program in Python. Instead of if (x is true) you can use if x is true. The same also applies for y and z.
You can use the space below to write your solution, but you will not be able to execute it, since it is not valid Python code.
# Here you can solve the problem...
Solution
if x is true:
if y is true:
if z is true:
<Code_block_1>
else:
<Code_block_2>
else:
<Code_block_3>
<Rest of program...>
Here it is important that <Rest of program…> is not indented. Otherwise with indentation, it could indirectly be grouped together with another code block or result in an indentation error.