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
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:
An example of executing multiple statements in if statement
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:
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:
The output of the above code is:
A demo of using multiple conditions in the if Python statement
- In this example, 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 below:
#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!")
The output of the above code is:
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:
#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:
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!")
As one condition is true, this will be the output:
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.
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:
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: