Python Progamming/Python conditional statement

Python If condition

Updated on January 14, 2026
1 min read

The if condition is used when a program needs to make a decision. It allows a program to examine data and choose what action to take. Without if, a program would execute every line in the same order, no matter what input it receives. That would make software useless for real-world problems.

When Python reaches an if statement, it evaluates a condition. This condition is an expression that results in either True or False. If the condition is True, Python executes the block of code under the if. If it is False, that block is skipped.

Example:

temperature = 30

if temperature > 25:
    print("It is hot outside")

Here, Python checks whether temperature > 25. If that is true, the message is printed.

The power of if comes from operators such as >, <, ==, !=, >=, and <=. These operators compare values and create conditions that guide program behavior.

Real-Life Meaning

Imagine a door with a security lock.

If the password is correct, the door opens.

If it is wrong, the door stays closed.

That simple rule is exactly how an if statement works.

Nested if Condition in Python

A nested if is an if statement placed inside another if. It is used when one decision depends on another. This creates a step-by-step filtering process.

Python evaluates the outer if first. If it is True, only then does Python evaluate the inner if. If the outer condition is False, Python never even looks at the inner one.

Example:

python
1age = 22
2has_id = True
3
4if age >= 18:
5    if has_id:
6        print("Entry allowed")

Here, Python checks two conditions:

  1. Is the person 18 or older?
  2. Do they have an ID?

Both must be true before the message is printed.

Why Nested if Exists

Real systems almost never rely on a single condition. A banking system might check:

  • Is the user logged in?
  • Does the account have enough balance?
  • Is the transaction within the limit?

Each step depends on the previous one. Nested if statements allow this layered decision-making.

Conclusion

The if statement gives Python the ability to think in terms of rules. The nested if extends this by allowing those rules to be applied in a sequence. Together, they form the logical backbone of almost every real-world program, from login systems to financial software to games.

Python If condition | Python Progamming | Learn Syntax