In this Python Tutorial, we are going to explore Conditional Statements.
What is a conditional statement?
Conditional statement is used to execute a block of code based on the condition.
Derived from mathematical concept, a conditional statement is a statement which is written in the below form.
If P then Q, where P is a hypothesis and Q is a conclusion.
If P is True then Q is the Result.
In the same way, in programming, P is an expression that results a Boolean value or P itself can be a Boolean value.
If P is True, then Q Block will be executed.
if Condition: <Code to be executed for true condition> else: <Code to be executed for false condition>
What will be the condition?
Lets see some conditional statements of real time scenarios.
if (passenger has valid ticket): Allow Entry else: Deny Entry
In the above example, the condition is “passenger should have a ticket”.
Lets see some example conditions for programming.
if (has ticket Boolean variable is True) then Allow Entry else Deny Entry.
How can you mention the “is True” condition?
There are conditional operators available for this case. Conditional operators are used to evaluate one or more conditions.
How the conditional Operators will be applied?
The condition can be one expression or combination of many expressions. But always the conditional will be passed only if the result is True.
The above table has all the example conditions and Truth values.
The simple Conditional Statement:
We have already seen the Syntax for conditional statement. Lets see a program now.
has_ticket = input("Do you have ticket? [Y/N]:") if (has_ticket == 'Y'): print("Allow Entry") else: print("Deny Entry")
Do you have ticket? [Y/N]:Y Allow Entry
Do you have ticket? [Y/N]:N Deny Entry
Do you have ticket? [Y/N]:not sure Deny Entry
Do you have ticket? [Y/N]:y Deny Entry
In the last result, you can see that even “y” small letter y is not passed the condition. This is because Python is case sensitive. Y and y are different values.
In many cases we had to check more than 1 conditions. For that, we do not need to write separate If statements.
We can combine it as a If-else ladder.
if Condition1: <Code block1> elif Condition2: <Code block2> else: <Code block3> <Remaining Code>
In the above syntax, you can find a condition with “elif” keyword. This is nothing but a form of else if.
We can have any number of “elif” blocks between if and else blocks.
Control flow of Elif:
In the above Elif statement syntax, the first condition, which is the if condition is validated.
If it is true, then the “Code block1” will be executed and other blocks will be skipped.
And, If Condition1 fails then the Condition2 will be validated.
If it is true, then the “Code block2” will be executed and other blocks will be skipped. And the validation goes on like this.
If all conditions failed then only the else block will be executed.
Finally the “Remaining Code” will be executed in any case.
As you can see, the condition flow will start from If and goes vertically. If any of the condition is True, then the remaining conditions will be skipped.
If there is a case where 2 conditions can be true and both the block need to be executed, then the second condition will be skipped as there is already a condition was True and execution was completed.
a = int(input("A:")) b = int(input("B:")) if a > b: print("A is greater than B.") elif a < b: print("A is less than B.") else: print("Both A & B are equal.") print("Exit")
Execution #1: A:5 B:5 Both A & B are equal. Exit Execution #2: A:4 B:5 A is less than B. Exit Execution #3: A:6 B:2 A is greater than B. Exit
The If statements can be nested together validate complex conditions.
# Grading System # < 50 - Fail # 50 - 59 - Second class # 60 - 74 - First class # 75 - 89 - Distinction # >= 90 - Exemplary mark = int(input("Please enter your mark:")) if (mark > 50): if (mark <= 59): print("Second class") elif (mark >= 60 and mark <= 74): print("First class") elif (mark >= 75 and mark <= 89): print("Distinction") else: print("Exemplary") else: print("Fail")
Please enter your mark:75 Distinction
In the above example, you can see that “and” operator is used to combine 2 conditions. Similarly or can be used.
Excited? Now you may have many such examples in your mind. Comment below your ideas and examples. That may be useful for other readers.