python conditional statement

Python Conditional Statement

Last Updated | Jan, 06, 2021 By Kingsley Ijomah
Typically when you write code it is executed in sequential order, but in the real world this is not always going to be what we want to do, it might be the case that you want a particular action to occur only when something particular happens. This is precisely when the conditional statement comes to our aid.

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.

The If Statement

We will start by looking at if statement in its most basic form:

code
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:

code
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')

The elif and else Clauses

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.

code
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.

Nested If Statement

It is also possible to have an if statement within another if statement, below is an example of this in action.

code
'''
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")

One-Line if Statements

It is also possible in Python to write an if statement in one line:

code
# 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.

Python’s Ternary Operator

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:

code
<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.

code
# ouput => 'Yes'
'Yes' if 7 > 5 else 'No'

# ouput => 'No'
'Yes' if 5 > 7 else 'No'

The Python pass Statement

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.

code
# ouput => IndentationError: expected an indented block
if 100 > 50:


# returns nothing and no error is raised
if 100 > 50:
    pass

Final Thoughts

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!

MY STORY

My name is Kingsley Ijomah, I am the founder of CODEHANCE, an online education platform built with you in mind, a place where I express my gratitude to a skill ( coding ) which has changed my life completely.

Learn To Code With Me.
FREE course included.

GET IN TOUCH

Enter your email address:

Delivered by FeedBurner