MyInternships.in

Python Control Flow

Python If Else Statements

if, elif and else let your program make decisions and run different code depending on a condition. They are the heart of control flow in Python.


The Basic if Statement

An if statement checks a condition. If the condition is True, the indented block under it runs; otherwise it is skipped. Python uses indentation (usually 4 spaces) instead of curly braces to mark a block, so consistent indentation is required.

A simple if
Python
age = 20
if age >= 18:
    print("You can vote")

if, elif and else Together

Use elif (short for "else if") to check additional conditions, and else to handle everything that did not match. Python checks each condition top to bottom and runs only the first block that is True.

Grading example
Python
marks = 72
if marks >= 90:
    grade = "A"
elif marks >= 75:
    grade = "B"
elif marks >= 60:
    grade = "C"
else:
    grade = "F"
print(grade)

Comparison and Logical Operators

Conditions are usually built using comparison operators like ==, !=, >, <, >= and <=, and combined with logical operators and, or and not. These operators evaluate to a boolean value of True or False.

OperatorMeaningExample
==equal tox == 5
!=not equal tox != 5
andboth must be Truex > 0 and x < 10
orat least one Truex < 0 or x > 100
notreverses a booleannot is_valid
Combining conditions
Python
age = 25
has_id = True
if age >= 18 and has_id:
    print("Entry allowed")

Nested if Statements

You can place an if statement inside another if statement when a decision depends on another decision. Keep nesting shallow — two levels is usually enough — so your code stays readable.

Nested if
Python
num = 15
if num > 0:
    if num % 2 == 0:
        print("Positive even")
    else:
        print("Positive odd")

The Ternary (Conditional) Operator

For simple either/or assignments, Python offers a one-line ternary expression: value_if_true if condition else value_if_false. It is handy inside variables, function calls or f-strings.

Ternary operator
Python
age = 16
status = "adult" if age >= 18 else "minor"
print(status)
💡

Avoid == True or == False in conditions. Just write if is_valid: instead of if is_valid == True: — it is cleaner and more Pythonic.

⚠️

A common freshers’ mistake is using = (assignment) instead of == (comparison) inside an if. Python actually raises a SyntaxError for if x = 5:, which helps catch this early.

Frequently Asked Questions

What is the difference between elif and multiple if statements?+

With elif, Python stops checking as soon as one condition is True and skips the rest. With separate if statements, every condition is checked independently, so more than one block could run.

Can I write an if statement without an else?+

Yes. else and elif are both optional. A standalone if simply does nothing when its condition is False.

Does Python have a switch statement like other languages?+

Not exactly — Python traditionally uses if/elif chains for this. Since Python 3.10, the match-case statement offers similar structured pattern matching.

Related Python Topics

Keep learning with these closely related lessons.

Ready to use your Python skills?

Find Python, data science and software internships and fresher jobs across India.

Browse Python Internships