A program's control flow is the order in which the program's code executes. The control flow of a Python program is regulated by conditional statements. In this article, we are going to explore these conditional statements to learn how to create and use them in Python.
Topics covered in this post:
We will start by looking at if statement in its most basic form:
if <expr>:
<statement>
So the first line above is going to evaluate an expression which most return an expression of type Boolean ( True or False ) and the statement below is what ever you want to happen when the if expression returned True.
Another point you should note is the indentation of the statement, this is required by Python, there are no closing tags like you might be used to from other programming languages, as far as python is concerned all statements indented is part of the if expression.
Below we are going to look at some real if statement evaluations:
x = 5
y = 7
'''
nothing gets printed because x is not
greater than y so the print statement
was never executed.
'''
if x > y:
print('Yes')
# output => 'No'
if x < y:
print('No')
# output => 'Yes'
if 'Hello' in 'Hello World!':
print('Yes')
'''
nothing is printed because the if expression
returned a Falsy, it must be Truthy for that
statement to execute
'''
if 'wow' in 'Hello World!':
print('yes')
'''
check if wow not found in the string
'''
if 'wow' not in 'Hello World!':
print('yes')
So far we have seen that when the if statement evaluates to True then the expression is executed, but what if we also want to execute an expression when the if statement returns False? This can be achieved with an else clause.
x = 5
y = 7
# output => No
if x > y:
print('Yes')
else:
print('No')
# output => Yes
if x < y:
print('Yes')
else:
print('No')
Perfect! now our code will always print out a result, in the examples above, the else clause will always print out when the if statement returns False.
It is also possible to have an if statement within another if statement, below is an example of this in action.
'''
This program checks if a given number is positive, negative or zero
within the if statement we have a nested if statement. The first
if expr checks if num is greater than or equal to 0, the second
if expr checks if the num is equal to 0
'''
num = 3
if num >= 0:
if num == 0:
print("Zero")
else:
print("Positive number")
else:
print("Negative number")
It is also possible in Python to write an if statement in one line:
# using one line
if <expr>: <statement>
x = 5
y = 7
# output => Yes
if 7 > 5: print('Yes')
So the one-line works fine when there is no else clause required, but there is another way of writing with an else clause in one line which we are going to explore below.
If you want to get if else
in one line it is possible to do so with Ternary Operator or Conditional expression, the syntax is written as follows:
<expr1> if <conditional_expr> else <expr2>
In the above example, <conditional_expr>
will be evaluated first, if that returns True then the <expr1>
is evaluated if it returned False then <expr2>
is evaluated.
Let's give this a go with the same example as above.
# ouput => 'Yes'
'Yes' if 7 > 5 else 'No'
# ouput => 'No'
'Yes' if 5 > 7 else 'No'
If statement just like methods/functions in Python cannot be empty, but if you leave it empty for some reason, then you can use the pass
statement as below.
# ouput => IndentationError: expected an indented block
if 100 > 50:
# returns nothing and no error is raised
if 100 > 50:
pass
Control flow plays a big part in writing a computer program, Python provides really nice and easy ways of controlling the flow of logic using the if statement. We have covered different ways of using the if statement, from writing one-line to using ternary operators.
These control flow methods you have learnt will really help you as you continue to learn Python, so before you proceed further make sure you feel comfortable with the conditional statement in Python.
Keep learning and keep growing.
Happy coding!