Conditionals in Python

himank bhalla
3 min readOct 12, 2018

Python supports conditional execution constructs like every other language does. There are some differences from some popular languages like c++ or Java.
Conditional execution is achieved through If, elif (short for else If) and else statements in python.
Let us see how it is done.

#This is the syntax for an If statement in pythonif boolean_expression :
body_statements

If statement
The if statement is used for conditional execution. An If statement consists of a series of If, elif, else clauses. Every clause except else consists of a condition (a Boolean expression) and a sequence of statements. Based on the evaluation of the condition, the sequence of statements are executed.

An If statement always has exactly one leading if clause, zero or more elif clauses that must follow it, and zero or one else clause at the end. elif and else clauses cannot be there standalone.

The sequence of if, elif and else provides an equivalent effect as the switch statement, found in other popular languages.

Example:

# Example 1if True:
print('True')

# Example 2
if 2 > 1 : #The condition 2 > 1 evaluates to true.
print('Yes 2 is greater than 1.')

# Example 3
if False:
print('False')
elif 5 < 10 : #The condition in elif evaluates to true.
print('yes 5 < 10')
elif 1 / 0: #The condition won't be evaluated.
print('division by zero')
else:
print('else block')

How Python evaluates an if, elif else clause?

The evaluation of the condition of the clauses are done in the order they are encountered until one of them evaluates to True, and then executes only the statements in that clause. It will not execute any later clause even if a later clause’s condition is also True. It will not even evaluate the later conditions (refer to example 3 above).
If none of them evaluates to true, nothing happens.

Evaluation steps:

  • An else clause if exists is replaced by an elif True.
  • The current clause’s condition is evaluated to a boolean value (True or False) and the condition is replaced by that value. If the condition evaluates to a non-boolean value, It is again converted to a boolean value.
    (you can refer to my previous article for evaluation of expressions in python.)
  • If the condition evaluates to True. It will execute the statements inside the clause and ignore all subsequent clauses. This is the end of the execution of the If statement.
  • If the condition evaluates to False. It will ignore this clause and move to the next clause. If there is no remaining clause, it will end the execution of the If statement.

There is another variation available in python. We can use if else inline to produce a similar effect as a C language ternary operator (?) has.

[on_true] if [expression] else [on_false]

Example:

Example 1print('yes') if 2 > 10 else print('no')       #This prints no.Example 2x, y = 10, 5
bigger = x if x > y else y #bigger binds to x which is 10.

--

--

himank bhalla

A software engineer. Passionate about using technology to solve real-world problems. Part-time mentor. Connect with me to learn more!