Labs ICT
โญ Pro Login

If...Else

Python if-else

Decision-making in Python uses if, elif, and else. Python relies on indentation (spaces or tabs) to group statements in a block. The standard is 4 spaces.

Basic if Statement

A simple condition that runs code when True.

age = 18
if age >= 18:
    print("You can vote.")
Try it Yourself โ†’

if / elif / else

Use elif for additional checks and else as a fallback.

score = 85
if score >= 90:
    grade = "A"
elif score >= 80:
    grade = "B"
elif score >= 70:
    grade = "C"
else:
    grade = "F"
print(f"Grade: {grade}")
Try it Yourself โ†’

Nested if

You can place an if inside another if for more complex logic.

num = 15
if num > 0:
    print("Positive")
    if num % 2 == 0:
        print("Even")
    else:
        print("Odd")
Try it Yourself โ†’

Ternary Operator

A shorthand if-else in a single line.

age = 20
status = "Adult" if age >= 18 else "Minor"
print(status)
Try it Yourself โ†’

๐Ÿงช Quick Quiz

What keyword does Python use for 'else if'?