Conditional Statements in Python

Premium

In programming, we often want to check a conditional statement and then do something if the condition is met and do something else if the condition is not met. This is done using the If conditions.

There are two general forms of decision making structures found in most of the programming languages:

Conditional Statements in Python
Conditional Statements in Python

if statement

An if statement consists of a Boolean expression followed by one or more statements.

1    if(BooleanExpression) :
2        statement
3    if(BooleanExpression) :
4        statement1
5        statement2
6         ...
7

If the BoolenExpression is true, then the statements will be executed. Note that for the statement to be a part of the If condition, it must be indented by 4 spaces.

if...else statement

An if statement can be followed by an optional else statement, which executes when the Boolean expression is false.

1    if(BooleanExpression) :
2        statement or block 1
3    else :
4        statement or block 2
5

Examples

Let's look at a few examples of the conditional statements.

1# Imaginary stock prices
2stock_A = 100
3stock_B = 120
4#Check if Stock B's price is above 100.
5if(stock_B>100) :
6    print("We recommend Stock B")
7

Save the above code in a file labeled 'pycontrols.py'. Then execute the file from your command prompt / terminal using python pycontrols.py. When the above code is compiled and executed, it produces the following result:

We recommend Stock B

Let's take another example to evaluate an If...Else statement.

1#Imaginary stock prices
2stock_A = 100
3stock_B = 120
4#Check if Stock A's price is greater than Stock B's price.
5if(stock_A>stock_B) :
6    print("We recommend Stock A")
7else :
8    print("We recommend Stock B")
9

The above code will produce the following results:

We recommend Stock B

As you can see, the Boolean Express (stock_A>stock_B) is false. Therefore, the statements in the 'else' block are evaluated.

The if...elif...else Statement

We can use elif to further customize our conditional statement. With the elif we can extend the if statement as much as you want. In the entire control structure, as soon as a condition is met (TRUE) the corresponding block of statements will be executed, and the rest of the structure will be ignored.

1if (BooleanExpression1) :
2  expression1
3elif (BooleanExpression2) :
4  expression2
5elif (BooleanExpression3) :
6  expression3
7else :
8  expression4
9

If BooleanExpression1 is TRUE, then expression1 will be executed. If FALSE, then it will check for BooleanExpression2. If BooleanExpression2 is TRUE, then expression2 will be execued, otherwise, it will evaluate BooleanExpression3. If BooleanExpression3 is TRUE, expression3 will be executed, otherwise, expression 4 will be executed.

Let's look at an example:

1#Imaginary stock prices
2stock_A = 100
3stock_B = 120
4#Compare Stock A and Stock B
5if (stock_A>stock_B) :
6    print("We recommend Stock A")
7elif (stock_A==stock_B) :
8    print("We are neutral to both stocks")
9else :
10    print("We recommend Stock B")
11

The above code will produce the following results:

We recommend Stock B

In our if conditions, we can use logical operators to check for multiple conditions, as shown below:

1#Imaginary stock prices
2stock_A = 100
3stock_B = 120
4if (stock_A>=100 & stock_B>=100) :
5  print("Sell both stocks")
6else :
7  print("No recommendation")
8

What do you think will print? Add this code to your script and execute to find out.