Python Conditionals

Make decisions with if, elif, and else.

In real life, we make decisions every day. If it is raining, we carry an umbrella. If we are hungry, we eat food. If a traffic light is green, we move forward.

Programs work in a similar way. A login system checks whether a username and password are correct before allowing access, while an online shop may apply a discount only when an order exceeds a certain value.

In Python, conditional statements make these decisions by executing different blocks of code depending on whether a condition is True or False.

What is a Conditional Statement?

A conditional statement checks a condition and decides which block of code should run. Python mainly uses if, elif, and else. Every condition produces a truth value that is treated as True or False.

The if Statement

The if statement is the simplest conditional statement. It executes its block only when the condition is true.

Syntax

python
if condition:
    # Code to execute
    pass

Example

python
age = 20

if age >= 18:
    print("You are eligible to vote.")

Output

text
You are eligible to vote.

Because age >= 18 is true, Python executes the print() statement.

The else Statement

The else block runs when the preceding if condition is false.

python
age = 15

if age >= 18:
    print("You can vote.")
else:
    print("You cannot vote yet.")

Output

text
You cannot vote yet.

The elif Statement

Use elif, short for else if, when you need to check multiple conditions in order. Python executes the first true branch and skips the remaining branches in that chain.

python
marks = 82

if marks >= 90:
    print("Grade A")
elif marks >= 75:
    print("Grade B")
elif marks >= 50:
    print("Grade C")
else:
    print("Fail")

Output

text
Grade B

Using Comparison Operators

Conditional statements commonly use comparison operators to compare values.

OperatorMeaning
==Equal to
!=Not equal to
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to
python
number = 10

if number == 10:
    print("Correct number")

The equality condition is true, so the message is displayed.

Using Logical Operators

Logical operators combine or reverse conditions.

Using and

With and, both conditions must be true.

python
age = 22
has_license = True

if age >= 18 and has_license:
    print("You can drive.")

Using or

With or, at least one condition must be true.

python
is_weekend = False
is_holiday = True

if is_weekend or is_holiday:
    print("Enjoy your day off!")

Using not

The not operator reverses the truth value of a condition.

python
is_logged_in = False

if not is_logged_in:
    print("Please log in.")

Nested if Statements

A nested if places one conditional statement inside another. This is useful when a second decision should be evaluated only after the first condition succeeds.

python
age = 25
citizen = True

if age >= 18:
    if citizen:
        print("You are eligible to vote.")

Output

text
You are eligible to vote.

The match Statement

Python 3.10 introduced match for structural pattern matching. A simple match statement can compare one value against several cases. The underscore acts as a catch-all case.

python
day = "Monday"

match day:
    case "Monday":
        print("Start of the week")
    case "Friday":
        print("Weekend is near")
    case _:
        print("Regular day")

Although match is powerful, beginners can comfortably start with if, elif, and else.

Indentation in Conditional Statements

Python uses indentation, usually four spaces, to define code blocks. Correct and consistent indentation is required.

Correct

python
age = 18

if age >= 18:
    print("Adult")

Incorrect

python
age = 18

if age >= 18:
print("Adult")

The incorrect example causes an IndentationError because print() is not indented inside the if block.

Real-World Example

The following program combines user input, a comparison operator, and conditional statements to check movie access by age.

python
age = int(input("Enter your age: "))

if age >= 18:
    print("You are allowed to watch the movie.")
else:
    print("Sorry, you must be at least 18 years old.")

Key Takeaways

  • Conditional statements help programs make decisions.
  • Python mainly uses if, elif, and else for decision-making.
  • Conditions evaluate to True or False.
  • Comparison operators compare values inside conditions.
  • Logical operators and, or, and not combine or modify conditions.
  • Nested if statements check conditions within conditions.
  • match provides another way to handle multiple choices in Python 3.10 and later.
  • Proper indentation is required because Python uses it to define code blocks.
  • Conditional statements are widely used in login systems, grading applications, online shops, and many other programs.
Let's learn with DevBrainBox AI