Python Conditions and If statements:
- Python supports the usual logical conditions from mathematics:
- Equals: a == b
- Not Equals: a != b
- Less than: a < b
- Less than or equal to: a <= b
- Greater than: a > b
- Greater than or equal to: a >= b
These conditions can be used in several ways, most commonly in "if statements" and loops.
An "if statement" is written by using the if keyword.
The if statement evaluates a condition (an expression that results in True or False).
If the condition is true, the code block inside the if statement is executed.
If the condition is false, the code block is skipped.
1.IF STATEMENT:
a = 33
b = 200
if b > a:
print("b is greater than a")
OUTPUT:
b is greater than a
2.Multiple Statements in If Block
- Multiple statements inside an if block. All statements must be indented at the same level.
Example
- Multiple statements in an if block:
age = 20
if age >= 18:
print("You are an adult")
print("You can vote")
print("You have full legal rights")
OUTPUT:
You are an adult
You can vote
You have full legal rights
3.Python Elif Statement
- The elif keyword is Python's way of saying "if the previous conditions were not true, then try this condition".
- The elif keyword allows you to check multiple expressions for True and execute a block of code as soon as one of the conditions evaluates to True.
EXAMPLE:
a = 33
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
OUTPUT:
a and b are equal
4.Multiple Elif Statements:
Many elif statements as you need. Python will check each condition in order and execute the first one that is true.
Use elif when you have multiple mutually exclusive conditions to check.
This is more efficient than using multiple separate if statements because Python stops checking once it finds a true condition.
Example
Testing multiple conditions:
score = 75
if score >= 90:
print("Grade: A")
elif score >= 80:
print("Grade: B")
elif score >= 70:
print("Grade: C")
elif score >= 60:
print("Grade: D")
OUTPUT:
Grade: C
5. Python Else Statement
- The else keyword catches anything which isn't caught by the preceding conditions.
- The else statement is executed when the if condition (and any elif conditions) evaluate to False.
a = 200
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")
OUTPUT:
a is greater than b
6.Else Without Elif
How Else Works
- The else statement provides a default action when none of the previous conditions are true.
- Think of it as a "catch-all" for any scenario not covered by your if and elif statements.
Note: The else statement must come last. You cannot have an elif after an else.
- An else without the elif: Example
a = 200
b = 33
if b > a:
print("b is greater than a")
else:
print("b is not greater than a")
OUTPUT:
b is not greater than a
Complete If-Elif-Else Chain
- Combine if, elif, and else to create a comprehensive decision-making structure.
Example
7. Temperature classifier:
temperature = 22
if temperature > 30:
print("It's hot outside!")
elif temperature > 20:
print("It's warm outside")
elif temperature > 10:
print("It's cool outside")
else:
print("It's cold outside!")
OUTPUT:
It's warm outside
Top comments (0)