Conditional Statements
When we talked about operators, we talked about how manipulating logical expressions are the building block of programming. In this blog, we will explore conditional statements, which essentially are us telling the computer to do a certain computation or execute this block of code, if a particular condition is satisfied.
If Statement
The syntax of an If statement is:
if <condition>:
….statement(s)
The 4 dots depict the number of whitespaces we must use within an If statement. Python considers whitespace to identify a block of code, whereas in other languages we often see the use of curly braces ‘{}’ to represent a block of code.
Consider the following example:
a = 5 b = 3 if a > b: print("Yes that is True")
This gives the output:
We created 2 variables, each of which stores an integer value. The next line of code should automatically convey what is happening. The condition compares our 2 variables, and if and only if ‘a’ is greater than ‘b’ would it execute the print statement. Try changing the value of ‘b’ to be greater than ‘a’ and see what happens (Nothing!).
Else Statement
If we want Python to execute some code when the ‘If Statement’ is not True, we can use the ‘Else Statement’. The code stored in the ‘Else Statement’ is only executed when there are no other conditions left to be satisfied.
a = 5 b = 7 if a > b: print("Yes a is greater than b") else: print("No a is not greater than b")
The obvious output would be:
Here ‘a’ is not greater than ‘b’, hence Python skips the code stored within the ‘If Statement’, and the code stored in ‘Else Statement’ is executed. This can also be used as a way to inform us, programmers, that the previous conditions have failed.
Alternative
Python has a simpler way of using If Else statements and it is called as an InLine statement. Its syntax is:
- if <condition> : <statement> [If Statement]
- <true_statement> if <condition> else <false_statement> [If Else Statement]
Consider an example:
integer_val = 15 # If Statement if integer_val > 13: print("The value is greater than 13") # If Else Statement print("The value is greater than 11" if integer_val > 11 else "The value is less than 11")
Elif Statement
We can check multiple conditions using the ‘Elif Statement’. Consider the example given below:
a = 5 b = 5 if a > b: print("a is greater than b") elif a < b: print("a is lesser than b") else: print("a is equal to b")
We would get the output as:
Here we have ‘a’ and ‘b’ store the same integer value, and we have given Python two different conditions to check. We have only 2 conditions to check because the third would automatically be True if the first 2 conditions are False.
We first check if a > b and then we check a < b, and if both are conditions False then the only remaining possibility is that a = b, and so we don’t have to ask Python to check if that condition is True.
Applications of Conditional Statements
Now that we have an idea about the conditional statements in Python, we can combine this with concepts we have learned before (mainly operators). Beginning with logical operators let’s consider the following:
this_blog_teaches_python = True if not this_blog_teaches_python: print("You are at the correct tutorial") else: print("Oops, wrong tutorial")
Here we created a variable that stores a Boolean value, and then in the ‘If Statement’ we are checking if the value stored in “this_blog_teaches_python” is False. Notice we start with “if not”, this is telling Python to check for the opposite value of the Boolean value stored in the variable, i.e., we are asking Python to check if “this_blog_teaches_python” stores False.
We can also use the input() function with conditional statements.
input_variable = input("Enter a value greater than 5: ") print(type(input_variable)) input_variable = int(input_variable) print("") if input_variable > 5: print("Correct variable") elif input_variable == 5: print("Entered value is equal to 5") else: print("Entered value is less than 5")
Over here, after we take the input, you must remember that the type of a variable that takes an input is ‘str’ and we need to convert it to the data type we want to work with.
Finally let’s consider a more complex use of relational & logical operators with conditional statements. (Try modifying the expressions so that you can see the output in all scenarios.)
variable_1 = 5 variable_2 = 3 variable_3 = 7 variable_4 = 1 if (variable_3 > variable_2) and (variable_4 == variable_1): print("All are TRUE!") elif (variable_3 == variable_2) or (variable_4 > variable_2): print("Second attempt") else: print("All is vanity!")
When we execute the above, we should get:
As mentioned before, the ‘Else Statement’ is a useful way to inform us that all the previous conditions have not been met.
What have we learned?
•What are conditional statements?
•What is the ‘If Statement’?
•What is the ‘Else Statement’?
•What is the ‘EIif Statement’?
•What is the data type of a value we receive from the input() function?