Python if, else and elif statements

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 on the 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 the expression is true, it will display a message:

The following code is executed:

ifnum = 10

if ifnum==10:

print ("The value of ifnum variable is 10, so condition is true!")

Output:

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

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

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:

The following code is used in this example:

#A demo of if statement with two conditions
var1 = 10
var2 = 20

if var1==10 and var2 == 20:

print ("Both conditions are TRUE!")

else:

print ("One or both conditions are false!")

Output:

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 Python ‘and’ operator. See the code and output:

The following Python code is used:

#A demo of if statement with two conditions

var1 = 5

var2 = 20



if var1==10 and var2 == 20:



print ("Both conditions are TRUE!")



else:



print ("One or both conditions are false!")

 

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 the 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 conditions 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:

This is how the ‘or’ operator is used:

#A demo of if statement with two conditions

var1 = 5

var2 = 20



if var1==10 or var2 == 20:



print ("One or both conditions are true!")



else:



print ("All conditions are false!")

Output:

if else OR

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.

The following code is used for elif example:

var1 = 30



if var1==10:

print ("The if statement is true!")

elif var1 == 20:

print ("The first elif is true!")

elif var1 == 30:

print ("The second elif is true!")

else:

print ("All are false!")

Output:

elif

As the value of the variable is 30, the second elif statement is 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.

This is how it is done:

import datetime



currday = datetime.date.today()

w = currday.weekday() + 1



if (w == 0): print("Its Sunday.");

elif (w == 1): print("The first working day, Monday!");

elif (w == 2): print("The second working day, Tuesday!");

elif (w == 3): print("The third working day, Wed!");

elif (w == 4): print("Weekend is getting closer, its Thu!");

elif (w == 5): print("Hurray! so close, its Fri!");

else: print("Yay, the Weekend started, as its Sat!");

Output:

elif weekdays

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!