Python If else
An if...else statement in Python is a basic control flow structure used to make decisions within your code based on some given conditions. It lets your program execute one block of code if the condition is true and another block if the condition is false.
Syntax
Here’s the basic syntax of an if.else statement in Python:
if condition:
# If the condition is True, this block of code will be executed
else:
# This block of code will be executed when the condition is False
Key Points
- Condition:
- A condition is an expression whose value is either
TrueorFalse. - Conditions frequently include the comparison operators (
==,!=,<, >,<=,>=), or the logical operatorsand,or,not.
2. Indentation:
- Python implements indentation using spaces or tabs to specify which lines of a program belong to the indentation block for
iforelse. - All the statements have to be at the same indent level.
3. Program Flow:
- The programme evaluates the condition in an
ifstatement. - If the condition is
True, then the block of code underifgets executed. - If the condition is
False, then the block of code underelsegets executed.
Example:
x = 10
if x > 5:
print("x is greater than 5")
else:
print("x is less than or equal to 5")
Output:
x is greater than 5
Adding elif (Else If)
The elif statement, short for “else if,” allows you to check multiple conditions in sequence. Only the first condition that evaluates to True will execute.
Syntax:
if condition1:
# Block of code to run if the condition1 is True
elif condition2:
# Block of code to run if condition2 is True
else:
# Block of code to execute if neither of the conditions conditions1 or condition2 are True
Example:
age = 18
if age < 18:
print("You are a minor.")
elif age == 18:
print("You just became an adult!")
else:
print("You are an adult.")
Output:
You just became an adult!
Nested if...else
You can place an if...else statement inside another if or else block, making it a nested condition
Example:
x = 10
y = 20
if x > 5:
if y > 15:
print("x is greater than 5 and y is greater than 15")
else:
print("x is greater than 5 but y is not greater than 15")
else:
print("x is not greater than 5")
Output:
x is greater than 5 and y is greater than 15
Using Logical Operators with if
You can combine conditions using logical operators like and, or, and not.
Example:
x = 10
y = 20
if x > 5 and y > 15:
print("Both conditions are True")
else:
print("At least one condition is False")
Output:
Both conditions are True
Ternary Conditional Operator
Python also supports a shorthand for if...else statements called the ternary conditional operator.
Syntax:
value_if_true if condition else value_if_false
Example:
x = 10
result = "Greater than 5" if x > 5 else "5 or less"
print(result)
Output:
Greater than 5
Summary
ifchecks a condition.elsegives you an alternative when theifcondition isFalse.eliflets you check additional conditions.- Logical operators (
and,or,not) allow you to combine multiple conditions. - Indentation is important in Python to define blocks of code.
- Ternary operators give you a neat way to write simple
if...elsestatements.