In this article, We are going to cover Python Conditional Statements with Examples, Python If statements, Python If…else statements, Python Nested if statements, Python If-elif ladder, Python, Short hand if statements, Python Short hand if else statements.
Table of Contents
Introduction
In python If else statement is also known as conditional statements to check if the condition is true or false. And it is also known as a decision making statement.
If you want to execute some line of code if a condition is true, or it is not. In python there is if, elif and else statements for this purpose.
Python Conditional Statements with Examples
#1: Python If statement
It is the simple decision making statement. In this statement when we execute some lines of code then the block of statement will be executed or not i. e If the condition is true then the block of statement will be executed otherwise not.
Syntax:
if (condition):
# if block
Example of Python if statement:
i=10
if(10 == i):
print("the condition is true")
Output:
the condition is true
In this example if statement is true that’s why the block(print) is executed.
Another Example of Python If Statement
i=10
if(9 == i):
print("the condition is true")
print("the condition is false")
Output:
the condition is false
In this example if statement is false that’s why the block(print) below the if statement is not executed.
#2: Python If…else statements
In if statement if condition is true then it will execute a block of statement and if the condition is false then it won’t. but in this statement there are two blocks . In this statement if the condition is false then execute the else condition.
Syntax:
if(condition):
# if block
else:
# else block
Example of Python if else statement:
x = 20
y = 10
if(y>x):
print(“y is greater than x”)
else:
print(“y is not greater than x”)
Output:
y is not greater than x
#3: Python Nested if statements
Nested if in python is placing an if statement inside another if statement. Nested if statement helpful if you want to check another condition inside a condition. Python provides this feature to check multiple conditions in a given program.
Syntax:
If (condition1):
#statement to execute if condition is true
If (condition2):
# statement to execute if condition is true
#end of nested if(condition2)
#end of if
#end of if (condition1)
In this syntax clearly says that in if block we can create another if block and if block contain n number of if block inside if block.
Example of Python nested if statement:
a=10
if (a > 0):
print(“number is positive”)
if(a < 20):
print(“a is less than 10”)
Output:
number is positive
a is less than 10
Another Example of Python nested if statement:
Syntax:
If(condition1):
#statement to execute if condition is true
If(condition)
#statement to execute if condition is true
else:
# statement to execute if condition is false
else:
# statement to execute if condition is false
Example:
a=10
if (a == 10):
if(a < 20):
print(“a is less than 20”)
else:
print(“a is greater than 20”)
else:
print(“a is less than 10”)
Output:
a is less than 20 a is less than 12
#4: Python If-elif ladder
The condition statement are executed from top down. In this if-elif if the if condition is true then the the if block will be executed otherwise not. Then check if the elif condition if it true then it will be executed otherwise not. If none of the condition is true then the else block will be executed.
Syntax:
if(condition):
#statement to execute if condition is true
elif(condition):
#statement to be executed if condition is false and elif is true
elif(condition):
#statement to be executed if both condition are false and this elif condition is true
.
.
.
.
else:
#statement to executed when all conditions are false
Example of Python If-elif ladder:
a=10
if(a<3):
print(“a is less than 3”)
elif(a!=10):
print(“a is not equals to 10”)
elif(a >8):
print(“a is greater than 8”)
else:
print(“a is zero”)
Output:
a is greater than 8
Another example of Python if elif:
#5: Python Short hand if statements
Short hand if is also called as one line statement. In python we can write if statement, if else statement and elif statement in one line without indentation. If there is single line statement then we can use short hand if statement.
Syntax:
If condition: statement
Example of Python short hand if statement
a = 10
if(a>0):print(“a is positive”)
Output:
a is positive
Another syntax of Python short hand if statement
if (condition):statement1; statement2;…..statement n
Example of Python short hand if statement
if('S' in 'FOSSTecNix'): print("welcome"); print("to"); print("FOSSTecNix");
Output:
welcome
to
FOSSTecNix
#6: Python Short hand if else statements
This statement used to write if else in one line.
Syntax:
if (condition): # if statement
else: #else statement
Example of Python Short hand if else statements:
a = 10
if(a==10):print(“a is equals to 10”)
else:print(“a is less than 10 ”)
Output:
a is equals to 10
Another syntax of Python Short hand if else statements:
if (condition):statement1; statement2;…..statement n
else:statement1; statement2;…..statement n
Example of Python short hand if else statement:
if('A' in 'FOSSTecNix'): print("welcome"); print("to"); print("FOSSTecNix");
else: print("There"); print("is"); print("no");print(“A”)
Output:
There
is
no
A
We have covered, Python Conditional Statements with Examples.
Conclusion:
We have covered, Python Conditional Statements with Examples , Python If statements, Python If…else statements, Python Nested if statements, Python If-elif ladder, Python, Short hand if statements, Python Short hand if else statements.
Related Articles:
7 Python Operators with Examples
Python Introduction for Programmers [Part 1]
Functions in Python with Examples
classes and objects in python with examples
Reference:
Great share dr
Good Discription dear
Nice info
Thanks.