Conditional Statements

Understand Python conditional statements like if, elif, and else with syntax, examples, and key tips. Learn how to control the flow of your Python programs.

Loading...
Conditional Statements

What Is an if Statement?

An if statement lets Python run a block of code only when a condition is True.

The basic syntax looks like this:

if condition:
    # Code to run if the condition is True

For example:

age = 20
 
if age >= 18:
    print("You are an adult.")
 
# Output:
 
# You are an adult.

The condition age >= 18 is True because 20 is greater than 18.

So Python runs the code inside the if block.

What If the Condition Is False?

Let’s change the age:

age = 15
 
if age >= 18:
    print("You are an adult.")

This time, nothing is printed. Because age >= 18 is False.

When an if condition is False, Python simply skips the code inside the if block.

Indentation Matters

Python uses indentation to show which code belongs to the if statement.

For example:

age = 20
 
if age >= 18:
    print("You are an adult.")

The print() statement is indented, so Python knows it belongs to the if block.

You can have more than one line inside the block:

age = 20
 
if age >= 18:
    print("You are an adult.")
    print("You can vote.")

Both lines belong to the if block.

Be Careful With Indentation

This is not correct:

age = 20
 
if age >= 18:
print("You are an adult.")

Python will raise an IndentationError.

So remember, after the : in an if statement, the code that belongs to it needs to be indented.

Using input() With if

You can use input() function with if.

For example:

age = int(input("Enter your age: "))
 
if age >= 18:
    print("You are an adult.")

If the user enters:

Enter your age: 25

Python checks 25 >= 18.

The result is True, so it prints:

You are an adult.

Using Logical Operators With if

You can combine conditions using and and or, inside an if statement.

age = int(input("Enter your age: "))
 
if age >= 18 and age <= 65:
    print("You are eligible.")

The if block will run, when both conditions are true.

Using ==

We can also check whether two values are equal.

password = input("Enter password: ")
 
if password == "python":
    print("Correct password!")

If the user enters python, the condition is True.

If the user enters Python instead of python, the condition is False, since string comparison is case-sensitive.

Remember:

  • = assigns a value
  • == compares two values

Note: This is just for learning; real applications never store or compare passwords as plain text.

Now, let's see what happens when the condition is False.

What Is else?

The else block runs when the if condition is False.

The basic syntax is:

if condition:
    # Code if condition is True
else:
    # Code if condition is False

For example:

age = 15
 
if age >= 18:
    print("You are an adult.")
else:
    print("You are not an adult.")
 
# Output:
 
# You are not an adult.

The condition age >= 18 is False, so Python skips the if block and runs the else block.

Notice that else lines up with if, not with the indented code inside it. Getting this alignment wrong will lead to IndentationError.

if and else Give Us Two Paths

Think of it like a simple decision:

             Is age >= 18?
                /      \
              Yes       No
               |         |
           Adult     Not adult

Only one of the two blocks runs.

For example:

number = 10
 
if number > 0:
    print("Positive number")
else:
    print("Not a positive number")

Since 10 > 0 is True, Python prints Positive number.

If we change the number:

number = -5

the condition becomes False, so Python prints Not a positive number.

Using input()

We can also get the value from the user.

number = int(input("Enter a number: "))
 
if number > 0:
    print("Positive number")
else:
    print("Not a positive number")

If the user enters 10:

Positive number

If the user enters -5:

Not a positive number

A Simple Even or Odd Example

Remember the % operator from our operators lesson?

We can use it here.

If a number divided by 2 leaves a remainder of 0, it’s even.

number = int(input("Enter a number: "))
 
if number % 2 == 0:
    print("Even")
else:
    print("Odd")

For example, if the user enters 10, Python checks:

10 % 2 == 0

This is True, so it prints Even.

If the user enters 7, the condition is False, so it prints Odd.

else Doesn’t Have a Condition

Notice that we don’t write a condition after else.

This is because else simply means: If none of the if condition is true, do this.

So this is correct:

if age >= 18:
    print("Adult")
else:
    print("Not an adult")

But this is not:

else age < 18:

The else block automatically handles the other possibility.

Now, what if we have more than two possibilities?

That’s where elif comes in.

What Is elif?

elif is short for else if.

It lets us check another condition when the previous if condition was False.

The basic structure is:

if condition1:
    # Code if condition1 is True
elif condition2:
    # Code if condition2 is True
else:
    # Code if all conditions are False

For example:

number = int(input("Enter a number: "))
 
if number > 0:
    print("Positive")
elif number == 0:
    print("Zero")
else:
    print("Negative")

Now we have three possibilities:

  • Greater than 0
  • Equal to 0
  • Less than 0

If the user enters 10: Positive If the user enters 0: Zero If the user enters -5: Negative

How Does Python Check elif?

Python checks the conditions from top to bottom.

For example:

age = 25
 
if age < 18:
    print("Under 18")
elif age < 30:
    print("Between 18 and 29")
else:
    print("30 or older")

Python first checks age < 18, that’s False.

Then it checks age < 30, that’s True.

So it prints: Between 18 and 29

Once Python finds a condition that is True, it runs that block and skips the remaining conditions.

You Can Have Multiple elif Statements

You aren’t limited to one elif.

For example:

score = int(input("Enter your score: "))
 
if score >= 90:
    print("Excellent")
elif score >= 75:
    print("Good")
elif score >= 50:
    print("Pass")
else:
    print("Fail")

Python checks each condition from top to bottom.

Let’s take one more example.

score = 85
 
if score >= 50:
    print("Pass")
elif score >= 75:
    print("Good")
elif score >= 90:
    print("Excellent")

This prints Pass, even though 85 should be Good. Since Python stops at the first True condition, score >= 50 is checked first and matches, so the more specific conditions below it never run.

So always order your conditions from most specific to least specific, usually highest to lowest when using ranges like this.

One more thing to remember: else is optional. So if no condition matches and there’s no else, Python simply runs nothing.

score = 50
 
if score >= 90:
    print("Excellent")
elif score >= 75:
    print("Good")

At this point, what if we need to check another condition inside an existing condition?

That’s where nested if statements come in.

What Is a Nested if?

A nested if is simply an if statement inside another if statement.

For example:

age = 20
 
if age >= 18:
    if age <= 30:
        print("You are between 18 and 30.")

Here, Python first checks: age >= 18

Only if that condition is True does Python check the second condition: age <= 30

Since both conditions are true, it prints: You are between 18 and 30.

Why Use a Nested if?

A nested if is useful when the second condition only matters if the first condition is already true.

For example:

number = int(input("Enter a number: "))
 
if number > 0:
    if number <= 50:
        print("Number is between 1 and 50.")

The second condition only needs to be checked when the number is already greater than 0.

Nested if With else

You can also use else inside a nested if.

age = 20
 
if age >= 18:
    if age <= 30:
        print("You are between 18 and 30.")
    else:
        print("You are older than 30.")
else:
    print("You are under 18.")

There are now multiple paths:

  • Under 18
  • Between 18 and 30
  • Older than 30

The indentation tells Python which else belongs to which if.

That’s why indentation is especially important with nested conditions.

Nested if With elif

You can also use elif inside a nested if.

number = int(input("Enter a number: "))
 
if number > 0:
    if number <= 50:
        print("Between 1 and 50.")
    elif number <= 70:
        print("Between 51 and 70.")
    else:
        print("Greater than 70.")
else:
    print("Zero or negative.")

Python first checks: number > 0

If that is True, it moves inside the nested if and checks the other conditions.

Note: Nesting too many if statements inside each other makes code harder to read. If you find yourself nesting three or four levels deep, it’s often a sign the logic can be simplified.

Nested if vs and

Sometimes you can write the same logic using and.

For example:

age = 25
 
if age >= 18:
    if age <= 30:
        print("Between 18 and 30.")

can also be written as:

if age >= 18 and age <= 30:
    print("Between 18 and 30.")

Both can work.

One thing to remember: Using and works well for simple combined conditions. But when the inner check needs its own elif or else (like the number range example above), nesting is clearer than trying to combine everything into one line.

Shorthand If-Else Statements

Python also lets you write a simple if-else condition in a single line.

This is called a conditional expression or shorthand if-else.

It is useful when you have a simple condition and want to choose between two values.

Syntax

value_if_true if condition else value_if_false

For example:

a = 10
b = 20
 
result = "A is greater" if a > b else "B is greater"
 
print(result)
 
# Output:
 
# B is greater

Here, Python checks:

a > b

If the condition is True, it uses "A is greater".

If the condition is False, it uses "B is greater".

Comparison with Standard if-else

The same example using a regular if-else statement would be:

a = 10
b = 20
 
if a > b:
    result = "A is greater"
else:
    result = "B is greater"
 
print(result)

Both approaches produce the same result.

The shorthand version is simply more compact.

Multiple Conditions

You can also use more than one condition in a conditional expression.

For example:

a = 330
b = 330
 
result = "A" if a > b else "=" if a == b else "B"
 
print(result)
 
# Output:
 
# =

Python checks the conditions from left to right:

  1. Is a > b?
  2. If not, is a == b?
  3. If neither is true, use "B".

Although this works, multiple conditions can quickly make a line difficult to read.

When to Use Shorthand if-else

Use it when:

  • The condition is simple.
  • You're choosing between two simple values.
  • The resulting code is easy to read.

For example:

age = 20
 
status = "Adult" if age >= 18 else "Minor"

Avoid it when:

  • The logic is complex.
  • You have many conditions.
  • The line becomes difficult to understand.

In those situations, a regular if-elif-else statement is usually easier to read.

if age >= 18:
    status = "Adult"
else:
    status = "Minor"

The goal isn't to make your code as short as possible. Readable code is more important than fewer lines.


👉 Next tutorial: Python Match Case Statements