Python If…Else
In Python, the if...else statement is used for conditional execution. This allows you to execute different blocks of code based on whether a condition is True or False.
Basic Syntax:
if condition:
# Code block executed if the condition is True
else:
# Code block executed if the condition is False
1. If Statement
The if statement checks a condition, and if it evaluates to True, the indented block of code following it is executed.
Example:
age = 20
if age >= 18:
print("You are an adult.")
Output:
You are an adult.
2. If…Else Statement
The else part runs when the condition in the if statement evaluates to False.
Example:
age = 16
if age >= 18:
print("You are an adult.")
else:
print("You are a minor.")
Output:
You are a minor.
3. Elif (Else If) Statement
The elif (short for “else if”) allows you to check multiple conditions. If the condition of the if statement is False, the elif statements are checked sequentially. If none of the if or elif conditions are True, the else block will execute.
Example:
age = 25
if age < 18:
print("You are a minor.")
elif age < 60:
print("You are an adult.")
else:
print("You are a senior.")
Output:
You are an adult.
4. Nested If Statements
You can also nest if...else statements within other if...else statements. This allows for more complex decision-making structures.
Example:
age = 25
if age >= 18:
if age < 60:
print("You are an adult.")
else:
print("You are a senior.")
else:
print("You are a minor.")
Output:
You are an adult.
5. Logical Operators in Conditions
You can combine conditions using logical operators like and, or, and not to create more complex conditions.
and: ReturnsTrueif both conditions areTrue.or: ReturnsTrueif at least one of the conditions isTrue.not: Reverses the truth value of a condition (i.e.,TruebecomesFalseand vice versa).
Example with and:
age = 25
income = 50000
if age >= 18 and income >= 30000:
print("You qualify for the loan.")
else:
print("You do not qualify for the loan.")
Output:
You qualify for the loan.
Example with or:
age = 16
has_permission = True
if age >= 18 or has_permission:
print("You are allowed to enter.")
else:
print("You are not allowed to enter.")
Output:
You are allowed to enter.
Example with not:
is_raining = False
if not is_raining:
print("You can go outside without an umbrella.")
else:
print("You should take an umbrella.")
Output:
You can go outside without an umbrella.
6. Ternary (Inline) If-Else
Python provides a shorthand for simple if...else statements using the ternary operator or conditional expression.
The syntax is:
value_if_true if condition else value_if_false
Example:
age = 20
status = "Adult" if age >= 18 else "Minor"
print(status)
Output:
Adult
Summary:
ifchecks a condition and executes a block of code if the condition isTrue.elseexecutes a block of code if the condition isFalse.elifallows checking multiple conditions in sequence.- Conditions can be combined using logical operators like
and,or, andnot. - Nested
ifstatements allow for more complex decision structures. - The ternary operator allows for concise
if...elsestatements in a single line.