Python if, else and elif statements: Explained with 8 examples

The if statement in Python

The if..else and elif are the decision-making statements in Python.

It will execute single or more statements based at given expression.

If the given expression is True, the statement inside the if statement will execute.

If the condition is false, the statement inside the else will execute.

Syntax of if statement

Following is the general syntax of using if statement in Python:

if expression:

#Statements to be executed here if condition is true

else:

#Statements to be executed here if condition is false

Note: The else statement is optional. If you use the else statement, it can be used once only. If you want to check multiple conditions then use the elif statement, explained at the last section of this tutorial.

An example of using Python if statement

In this example, a simple if statement is used where an expression is given. If expression is true, it will display a message:

Python if statement

See online demo and code

The following code is executed:

 

In the demo page, you can see the output of the above code is:

The value of ifnum variable is 10, so condition is true!

An example of executing multiple statements in if statement

You may execute multiple statements in the if statement, provided you use the same indentation i.e. default four spaces (you may change that).

If you use the second or next statement at the same level where the if statement is used, it will be considered outside of the if statement. In that case, after executing the if statement, this statement or code will execute.

See the following example where multiple statements are executed as the condition is true:

if multiple statement

See online demo and code

An example of using the Python else statement

In this example, the else Python statement is given that will execute a line of code if the condition is false. You may also use multiple statements with the same indentation in the else block just like in the if block. See the code and output online:

Python if else

See online demo and code

The output of the above code is:

The ifnum variable value is other than 10!

A demo of using multiple conditions in the if Python statement

In this demo, two conditions are used in the if statement. For that, two variables are declared and assigned the values. In the if condition, the “and” operator is used. See the code and output by clicking the link or image below:

if else and

See online demo and code

The following code is used in this example:

 

The output of the above code is:

Both conditions are TRUE!

Note: If you are using the ‘and’ operator, all the conditions have to be true in order to execute the statements inside the if statement. If any of the conditions is false, the else part (if provided) will execute. See the example below.

What happens if a condition is false with ‘and’ operator

In this example, one condition is true while the other is false in the if statement as using the ‘and’ operator. See the code and output:

See online demo and code

The following Python code is used:

 

The output of the above code is:

One or both conditions are false!

As mentioned earlier, all the conditions have to be true in order to execute the statements inside if block while using the ‘and’ operator. Otherwise, the else part will execute.

If this is required to execute if block as any of the condition is true, then use the ‘or’ operator as shown in the following example.

Using ‘or’ operator in if statement

In this example, the ‘or’ operator is used in Python if else statement. Two conditions are given, so, if any of the conditions is true, the if part will execute:

if else OR

See online demo and code

This is how the ‘or’ operator is used:

 

As one condition is true, this will be the output:

One or both conditions are true!

The elif statement in Python

If you have multiple conditions to check and for each condition different code is required to execute, you may use the elif statement of Python.

  • The elif statement also takes an expression which is checked after the first if statement.
  • You may use multiple elif statements.

See the following example of using the Python elif statement. In this example, a variable is assigned an initial value of 30.

elif

See online demo and code

The following code is used for elif example:

 

As the value of the variable is 30, the second elif statement executed.

An example of elif with current day

In this example, a message is shown according to the current day by using the if..elif..else statement.

For that, first the datetime module is imported and current day is taken. After that, a series of if..elif statements are used to check what is the current day and according to the day, a related message is displayed.

elif weekdays

See online demo and code

This is how it is done: