Photo by Denys Nevozhai on Unsplash
Taking control of your code: an introduction to Python's control flow tools
Control flow
Until now, when running a program, statements were executed in sequential order, which means they are being executed one by one. However, programmers often modify this sequential order, skipping, repeating or choosing a statement to execute. Control flow refers to the order in which statements are executed in a program. Conditionals, loops and functions are commonly used for control flow.
if statements
The first control structure we are going to explore is the if
statement. The if
statement is used to execute code only if a certain condition is true. The basic syntax of an if
statement in Python looks like this:
if condition:
# execute this code if condition is true
Notice how the code that will be executed if the condition evaluates to true is indented. In Python, indentation is used to define blocks of code within control structures. Unlike other programming languages that use braces or other punctuation marks to delimit the beginning and end of a block of code, Python uses whitespace alone to indicate how code should be grouped. Indentation is important to the point that the program returns an error if the indentation is wrong.
Returning to if
statements, the condition is a boolean expression that evaluates either to True
or False
. If the condition evaluates to True
, the code in the block will be executed. If the condition evaluates to False
, the code in the block will be skipped. Here is an example:
age = 24
if age > 18:
print("You are an adult") # Output => You are an adult
temperature = 87
if temperature > 100:
print("The weather is hot") # Output => None
In the code above, we are comparing the value stored inside the age
variable with the number 18
. If this value is greater than 18
, the print function will run. In this case, the age
variable evaluates to 24
, which is indeed greater than 18
, therefore the code in the block will execute.
If we proceed through the program, we encounter another variable, the temperature
variable, holding the value 87
. The if statement below checks whether the temperature
variable is holding a value greater than 100
. Since 87
is not greater than 100
, therefore the condition evaluates to False, and the code in the block will not be executed.
else and elif statements
In addition to the if
statement, the else
and elif
clauses are crucial components of conditionals. In Python, the else
statement is used as a default action to be executed if none of the preceding conditions evaluate to True
.
Here is the syntax of an if...else
statement:
if condition:
# execute this code if the condition is true
else:
# execute this code if the condition is false
We can delineate the behavior of an if...else
statement:
If the condition evaluates to True
:
the code inside
if
is executedthe code inside
else
is skipped
If the condition evaluates to False
:
the code inside
if
is skippedthe code inside
else
is executed
To better explain this, here is an example:
x = 3
if x > 5:
print("x is greater than 5")
else:
print("x is less or equal to 5")
# Output => x is less or equal to 5
In the example above, the variable x
is holding the value 3
. Proceeding with the program, we encounter the first condition. The condition checks whether the value of the variable x
is greater than 5
. Since it is not, the value is indeed 3
, therefore the default condition contained in the else
statement will be executed.
The if...else
statement is used to execute a block among two alternatives. Oftentimes we want to execute a block among three or more alternatives, therefore the if...else
statement loses its usage. To this purpose, we can use the if...elif...else
statement. The elif
statement in Python stands for else if. It is used to combine multiple conditions in one if
statement.
The syntax of an if...elif...else
statement is:
if condition1:
# execute this code if condition1 is true
elif condition2:
# execute this code if condition1 is false and condition2 is true
else:
# execute this code if both condition1 and condition2 are false
We can delineate the behavior of an if...elif...else
statement:
If condition1 evaluates to True
:
- the code inside
if
is executed
If condition1 evaluates to False
, condition2 is evaluated:
If condition2 evaluates to True
:
- the code inside
elif
is executed
If condition2 evaluates to False
:
- the code inside
else
is executed
Here is an example of an if...elif...else
statement:
x = -5
if x > 0:
print("x is positive")
elif x < 0:
print("x is negative")
else:
print("x is 0")
# Output => x is negative
In the example above, the variable x
is holding the value -5
. Proceeding with the program, we encounter the first condition. The condition checks whether the value of the variable x
is greater than 0
. Since it is not, the default condition of the elif
block is evaluated. This condition checks whether the value of the x
is less than 0
. The value of x
is indeed less than 0
, therefore the code in the elif
block is executed.
It is important to note that only one block of code is executed in an if...elif...else
statement. Once a condition is met, all subsequent conditions are skipped, even if their condition might also be true.
Conclusion
In conclusion, conditional statements are a fundamental feature of Python programming that allows for the logical control of program flow.
It is important to be careful with indentation when writing code with conditional statements, as even minor errors can cause unexpected behavior. However, once you have mastered the use of conditionals, you will be well on your way to developing robust and powerful Python programs.