Learning Guides
If-Else Statements in Python: Complete Guide
Quick answer: Learn Python if, elif and else statements including comparison operators, nested conditions, and the ternary conditional expression, with examples.
Basic If Statement
age = 20
if age >= 18:
print("You are an adult")
If-Else
marks = 45
if marks >= 40:
print("Pass")
else:
print("Fail")
Elif for Multiple Conditions
marks = 82
if marks >= 90:
grade = "A+"
elif marks >= 75:
grade = "A"
elif marks >= 60:
grade = "B"
else:
grade = "C"
print(grade) # A
Python checks conditions from top to bottom and stops at the first one that is true, so order matters when conditions could overlap.
Comparison Operators Used in Conditions
| Operator | Meaning |
|---|---|
| == | Equal to |
| != | Not equal to |
| >, < | Greater than, less than |
| >=, <= | Greater than or equal to, less than or equal to |
Combining Conditions with and, or, not
age = 25
has_id = True
if age >= 18 and has_id:
print("Entry allowed")
if age < 18 or not has_id:
print("Entry denied")
Nested If Statements
income = 60000
has_loan = True
if income > 50000:
if has_loan:
print("Eligible with existing loan review")
else:
print("Eligible")
else:
print("Not eligible")
Ternary Conditional Expression
Python supports a compact one-line form for simple if-else assignments.
age = 16
status = "Adult" if age >= 18 else "Minor"
print(status) # Minor
A Common Mistake
# Using = instead of == is a SyntaxError in a condition, catching this early
if age = 18: # wrong: assignment, not comparison
pass
if age == 18: # correct
pass
Common Interview Questions
What is the difference between elif and multiple separate if statements?
With elif, Python stops checking once one condition is true. With separate if statements, every single one is evaluated independently, even if an earlier one was already true.
What does the ternary expression evaluate to if the condition is false?
It evaluates to whatever follows the else keyword in the expression.
FAQ
Frequently Asked Questions
What is the difference between elif and a new if statement?
With elif, Python stops checking further conditions once one is true. With separate if statements, every one is evaluated independently regardless of earlier results.
What is a ternary conditional expression in Python?
A compact one-line form of if-else used for simple assignments, written as value_if_true if condition else value_if_false.
Can you combine multiple conditions in a single if statement?
Yes, using the logical operators and, or and not to combine comparisons.
Keep Reading
Related Articles
Learning Guides
INNER JOIN in SQL with Examples: Complete Beginner's Guide
Learn INNER JOIN in SQL with practical examples, syntax, interview questions, and real-world use cases. A complete beginner-friendly SQL JOI
Learning Guides
OUTER JOIN in SQL: Complete Guide with Examples for Beginners
Master OUTER JOIN in SQL with practical examples and real-world scenarios. Learn LEFT JOIN, RIGHT JOIN, FULL OUTER JOIN, syntax, use cases,
Learning Guides
Multi-Row Functions in SQL: Complete Guide with Examples
Learn multi-row (aggregate) functions in SQL including SUM, AVG, COUNT, MIN, MAX, GROUP BY and HAVING, with practical examples, NULL handlin