If-else statements in Python we see in the section of conditional statements, that decide the controls of code, As you can tell from the name. There are many situations in real life when we have to make some decisions and perform a specific task depending upon the taken decision. In Python language, conditional statements direct the flow of program execution.
Types of Control Flow in Python:
- if statement
- if-else statement
- nested-if statement
- if-elif-else ladder
Python if statement:
The if statement is the most simple decision-making statement. It is used to decide whether a certain statement or block of statements will be executed or not.
Syntax:
if condition:
# Statements to execute if
# condition is true
Example:
a = 15
if a > 10 :
print("a is gretter than 10 ")
The Output Of This Code Will Be:
a is gretter than 10
Python If-Else Statement
If-else statement in Python is used to evaluate a condition and execute some code accordingly. If the condition is true then ‘if’ block is executed. and if the condition is false then the else block will be executed.
Syntax:
if (condition):
# Executes this block if
# condition is true
else:
# Executes this block if
# condition is false
Example:
age = 18
if age >= 18:
print("You can vote")
else:
print("You can't vote")
The Output Of This Code Will Be:
You can vote
In this program, if the age is greater than and equal to 18 ” then if the block is executed “You can vote” will be printed. If the condition is false then else block will be executed. In this program age equals 18 which means if the block is executed and printed “You can vote”. Let’s see another example for a better understanding.
i = 20
if (i < 15):
print("i is smaller than 15")
print("i'm in if Block")
else:
print("i is greater than 15")
print("i'm in else Block")
print("i'm not in if and not in else Block")
The Output Of This Code Will Be:
i is greater than 15
i'm in else Block
i'm not in if and not in else Block
Python If-Elif-Else Statement:
In Python programming language if-elif-else statement is used to take decisions. But if-elif-else statement can check multiple conditions. It helps you execute different blocks of code according to different conditions. Let’s understand with a simple example:
Syntax:
Syntax:
if condition1:
# Code jo chalega agar condition1 true hai
elif condition2:
# Code jo chalega agar condition1 false hai aur condition2 true hai
else:
# Code jo chalega agar condition1 aur condition2 dono false hain
Example:
num = 20
if (num < 0):
print("Number is negative.")
elif (num == 0):
print("Number is Zero.")
elif (num == 999):
print("Number is Special.")
else:
print("Number is positive.")
print("I am outside the block.")
In this example, if the name is less than 0 then the first condition will be satisfied and print “Number is negative”. If is equal to 0 then the second block will be executed and print “Number is Zero.” If all conditions are false then always else block will be executed.
The Output Of This Code Will Be:
Number is positive.
I am outside the block.
Nested-If Statement in Python
Nested if statements are used when you need another if statement inside another if statement. We can check multiple conditions in detail. Let’s see an example:
Syntax:
if condition1:
# Code to execute if condition1 is True
if condition2:
# Code to execute if condition2 is True
else:
# Code to execute if condition2 is False
else:
# Code to execute if condition1 is False
Example:
age = 25
gender = "male"
if age >= 18:
print("You are an adult.")
if gender == "male":
print("You are a male adult.")
else:
print("You are a female adult.")
else:
print("You are a minor.")
- If the age is 18 or older, it prints “You are an adult.”
- If the gender is “male” within the adult age group, it prints “You are a male adult.”
- If the gender is not “male,” it prints “You are a female adult.”
- If the age is less than 18, it prints “You are a minor.”
The Output Of This Code Will Be:
You are an adult.
You are a male adult.
Short Hand Of If-Else:
In Python, there is a shorthand for expressing an if-else
statement in a single line, known as the ternary conditional operator. It has the following syntax:
result = value_if_true if condition else value_if_false
Here, condition
is evaluated first. If it is true, value_if_true
is returned; otherwise, value_if_false
is returned.
age = 20
status = "Adult" if age >= 18 else "Minor"
print(status)
In this example, if age
is 18 or older, status
will be set to “Adult”; otherwise, it will be set to “Minor”. This is equivalent to writing:
if age >= 18:
status = "Adult"
else:
status = "Minor"
Note:
Python programs evaluate the conditions in order. As soon as a condition is true, it executes its corresponding block and ignores the rest of the if-else-else ladder. If you need to check more conditions, you can use multiple if statements.
If you don’t know “String Methods In Python” just click on read more.
I hope you understand if Else Conditional Statements in Python. If you do not understand this topic just comment below with your email ID, and I will mail back to you.