Python’s conditional statements carry out various calculations or operations according to whether a particular Boolean constraint evaluates to true or false. In Python, if statements deal with conditional statements.
The conditional statements that Python offers are listed below.
To make decisions, use the if statement in Python. It has a body of code that only executes when the if statement’s condition is met. The optional else statement, which includes some code for the else condition, runs if the condition is false.
Python’s if-else statement is used when you want to justify one condition while the other is false.
Syntax:
if condition:
# Statements to execute if
# condition is true
Example:
# if statement example
if 1 > 5:
print("1 greater than 5")
print("Program ended")
Output:
Program ended
When the if condition is false, the additional block of code is combined as an else statement, which is executed.
Syntax:
if (condition):
# Executes this block if
# condition is true
else:
# Executes this block if
# condition is false
Example:
price = 69
quantity = 5
if price*quantity < 500:
print("Total is less than 500")
else:
print("Total is more than 500")
Output:
Total is less than 500
If statements can be checked inside other if statements. This type of conditional statement is known as a nested if statement. This means that the inner if condition will be checked only if the outer if condition is true, and we can see that multiple conditions must be satisfied as a result.
Syntax:
if (condition1):
# Executes when condition1 is true
if (condition2):
# Executes when condition2 is true
# if Block is end here
# if Block is end here
Example:
# Nested if statement example
a = 11
if a > 5:
print("Bigger than 5")
if a <= 25:
print("Between 5 and 15")
Output:
Bigger than 5
Between 5 and 15
The if-elif statement is a shorthand for the if…else chain. When using an if-elif statement, an else block is added at the end that is executed if none of the preceding if-elif statements are true.
Syntax:
if (condition):
#statement
elif (condition):
#statement
.
.
else:
#statement
Example:
price = 109
if price > 100:
print("price is greater than 100")
elif price == 100:
print("price is 100")
elif price < 100:
print("price is less than 100")
Output:
price is greater than 100
Note: also read about Relational & Logical Operators in Python
Please follow me to read my latest post on programming and technology if you like my post.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Find the length of the longest absolute path to a file within the abstracted file…
You manage an e-commerce website and need to keep track of the last N order…
You are given a stream of elements that is too large to fit into memory.…
The formula for the area of a circle is given by πr². Use the Monte…
Given an integer k and a string s, write a function to determine the length…
There is a staircase with N steps, and you can ascend either 1 step or…