Python If-Else Condition
In this tutorial, You’ll learn about Python Conditional Statements such as IF, IF-ELSE, IF-ELIF, Nested IF, and Switch Statements.
The Conditional Statements take up the responsibility for logic flow across the programming and these take such decisions based on conditions specified by the programmer.
The logical operators such as OR, AND, EqualTo are used to arrive at a discussion on whether to continue program flow or not.
Table of Contents
- How to write if a statement in Python?
- How handle else condition?
- How to write Ladder If Condition using ELIF Statement?
- Write Nested If Condition
- How to use Switch Statement?
- Short-Hand If-Else Condition
- Multiple Conditioning use AND, OR, NOT Operator
- Conclusion
Post Update Timeline
Updated on: 22-07-2020
Added new topics such as Switch Statement and Logical Operators
How to write if a statement in Python?
Use keyword if
followed by expression and end that expression using colon ( :
) symbol. And in between their goes the logical comparison as shown below where variable a
is compared to variable b
to check which is greater ( >
).
a = 10 b = 9 if a > b: print("Yes the variable a is greater than b") # PYTHON OUTPUT Yes the variable a is greater than b
Logical Operators with Symbols and meaning.
- >: This is greater than the operator.
- <: This is lesser than the operator.
- ==: This is equal to the operator.
- !: This is not an operator and is used with equal to the operator such as
6 != 5
.
How handle else condition?
The If Condition executes only when the expression return True
else the program logic is not allowed to access statements inside the If condition. But there is a provision to handle execution flow i.e using else statement.
a = 3 b = 4 if a > b: print("Yes the variable a is greater than b") else: print("No the variable a is lesser than b") # PYTHON OUTPUT No the variable a is lesser than b
Here the logical expression a > b returns False
and the data inside the else statement is executed.
How to write Ladder If Condition using ELIF Statement?
You can also chain multiple If Conditions one below another and this is called ladder if Condition.
Note
while using Ladder If Statements, Instead of using else use elif statement followed by the expression.
a = 7 b = 10 c = 9 if a > b: print("Yes the variable a is greater than b") elif b > c : print("Yes the variable b is greater than a and c") else: print("Yes the variable c is greater than b and a") # PYTHON OUTPUT Yes the variable b is greater than a and c
Write Nested If Condition
If Conditions written inside one another is called Nested If Conditions. They are mostly used for nested level conditional checking where you need to check many conditions one after the other.
if <condition>: if <condition>: if <condition>: # statement else: if <condition>: # statement
Consider an example here:
user_role = 'STAFF' is_active = False if user_role == 'ADMIN': if is_active == True: print("Welcome Administrator") else: print("Your account is locked.") elif user_role == 'STAFF': print("Welcome Staff") #PYTHON OUTPUT Your account is locked.
The condition if is_active == True:
is called as nested if condition.
How to use Switch Statement?
In Python, Switch Statement is not available but here is a way by which you can implement switch functionality in python and i.e by using a dictionary.
def switch_method( case ): return { "zero" : 'Hello World', "one" : 'to Python Programming', "two" : 'Welcome', }.get( case, None ) print( switch_method( "zero" ) ) print( switch_method( "two" ) ) print( switch_method( "one" ) ) print( switch_method( "four" ) ) # Note the "four" doesn't exists # PYTHON OUTPUT Hello World Welcome to Python Programming None
Note
You can also return functions from the above example. Which will provide more functionality while working with complex logic.
Short-Hand If-Else Condition
Python has a simple one-liner If-Else condition where first is the output if condition returns True
and last is the else part.
Syntax
<logic-when-condition-is-true> if <condition> else <else-part-logic>
Example:
a=15 b=16 print(a) if a > b else print(b) #PYTHON OUTPUT 16
Multiple Conditioning use AND, OR, NOT Operator
a=35 b=25 c=33 if a > b and a > c: print('A is greater than B and C') #PYTHON OUTPUT A is greater than B and C
a=30 b=25 c=33 if a > b or a > c: print('Because A is greater than B') #if any one condition is True #PYTHON OUTPUT Because A is greater than B
name_1 = "John" name_2 = "Jake" if name_1 != name_2: print("Yes! both the names are different.") else: print("No! both the names are same.") # PYTHON OUTPUT Yes! both the names are different.
The above program can also be rewritten using a special operator.
name_1 = "John" name_2 = "Jake" if name_1 is not name_2: print("Yes! both the names are different.") else: print("No! both the names are same.") # PYTHON OUTPUT Yes! both the names are different.
Conclusion
In Conclusion, Conditional Statements in Python have their own importance and as a fundamental part, they are used every time and hence programmers must have the basic knowledge of Python If-Else Conditional Statements.
Recent Posts




