![](https://coding74.wordpress.com/wp-content/uploads/2023/03/conditional-statements-in-python_watermarked.webp?w=1024)
Conditional Statement:
Conditional Statement allows you to execute a block of code
only when a specific condition is True.
if True:
print("If Block")
print("Inside If")
# Output is:
If Block
Inside If
If – Else Statement: When the If – Else conditional statement is used, the Else
block of code executes if the condition is False.
a = int(input()) # -1
if a > 0:
print("Positive")
else:
print("Not Positive")
# Output is:
Not Positive
![](https://coding74.wordpress.com/wp-content/uploads/2023/03/python-elif.png?w=1024)
Nested Conditions: The conditional block inside another if/else conditional block is called as a nested conditional block.
if Condition A:
if Condition B:
block of code
else:
block of code
if Condition A:
block of code
else:
if Condition B:
block of code
Elif Statement: Use the elif statement to have multiple conditional statements
between if and else. The elif statement is optional.
if Condition A:
block of code
elif Condition B:
block of code
else:
block of code
Identation:
- Space(s) in front of the conditional block is called indentation.
- Indentation(spacing) is used to identify the Conditional Blocks.
- Standard practice is to use four spaces for indentation.
Flow Chat
![](https://coding74.wordpress.com/wp-content/uploads/2023/03/python_if_elif_else_statement_flow_chart.png?w=511)
Leave a comment