Sitemap
Mrunal Shrikumar

Mrunal Shrikumar

Conditional Statements in Python

7 min readOct 7, 2026

--

Press enter or click to view image in full size

Programs often need to make decisions based on different situations. For example, a program may need to check whether a number is positive or negative, whether a student has achieved a particular grade, or whether the information entered by a user is valid.

In Python, conditional statements allow a program to evaluate a condition and decide which block of code should be executed.

Conditional statements are an important part of flow control, which determines the order in which statements and instructions are executed in a program.

The main conditional structures in Python are:

  • if
  • if-else
  • if-elif-else
  • Nested if-else

What Is Flow Control?

Flow control refers to the mechanisms that determine the order in which statements and instructions are executed in a program.

A program does not always need to execute every instruction in exactly the same way. Sometimes it needs to make a decision based on a particular condition.

For example:

If the weather is sunny, go play cricket. Otherwise, stay indoors.

This type of decision can be represented in Python using a conditional statement.

Flow control can involve different types of statements, including conditional statements, loop statements, and control statements. This article focuses on conditional statements.

What Is a Condition?

A condition is an expression that can produce either True or False.

For example:

3 > 4

Output:

False

Whereas:

1 < 2

Output:

True

Comparison operators such as >, <, >=, <=, ==, and != are commonly used to create conditions.

These conditions can then be used to control which part of a program should run.

The if Statement

The if statement is used when a particular block of code should be executed only when a condition is true.

The general syntax is:

if condition:
# code to be executed

If the condition is True, the code inside the if block is executed.

If the condition is False, the block is not executed.

Example

temperature = 29

if temperature > 25:
print("The room is warm")

Output:

The room is warm

The condition temperature > 25 is true because the temperature is 29. Therefore, the print() statement is executed.

Indentation in an if Statement

Python uses indentation to identify the statements that belong to a particular block.

For example:

score = 85

if score >= 80:
print("Good performance")

The print() statement is indented, which tells Python that it belongs to the if block.

Indentation is therefore an important part of writing conditional statements in Python.

Using a Boolean Variable as a Condition

A Boolean variable can also be used directly as a condition.

For example:

is_enrolled = True

if is_enrolled:
print("The student is enrolled")

Output:

The student is enrolled

Since is_enrolled contains True, the if block is executed.

This can be useful when a variable already represents a condition.

The if-else Statement

Sometimes a program needs to choose between two possible outcomes.

The if-else statement is used for this purpose.

The general syntax is:

if condition:
# code when condition is True
else:
# code when condition is False

If the condition is True, the if block is executed.

If the condition is False, the else block is executed.

Example

weather = "cloudy"

if weather == "sunny":
print("Go for a walk")
else:
print("Stay indoors")

Output:

Stay indoors

The condition weather == "sunny" is false because the value of weather is "cloudy". Therefore, Python executes the else block.

Checking Whether a Number Is Even or Odd

Conditional statements can be combined with operators to solve practical problems.

For example, a number can be checked to determine whether it is even or odd.

When a number is divided by 2, an even number leaves a remainder of 0.

The % operator can be used to find the remainder.

number = 27

if number % 2 == 0:
print("The number is even")
else:
print("The number is odd")

Output:

The number is odd

Here:

number % 2

calculates the remainder after dividing the number by 2.

If the remainder is 0, the number is even. Otherwise, it is odd.

The if-elif-else Statement

Some situations involve more than two possible outcomes.

For example, a student’s score may fall into different grade ranges.

Python provides the elif statement for handling multiple conditions.

The general structure is:

if condition:
# first block
elif condition:
# second block
else:
# final block

Python checks the conditions in order. When a condition is true, its corresponding block is executed. If none of the conditions is true, the else block is executed.

Example: Grading

score = 76

if score >= 90:
print("Grade A")
elif 80 <= score < 90:
print("Grade B")
elif 70 <= score < 80:
print("Grade C")
else:
print("Grade D")

Output:

Grade C

The score is 76.

Get Mrunal Shrikumar’s stories in your inbox

Join Medium for free to get updates from this writer.

It does not satisfy the first condition because it is less than 90.

It also does not satisfy the second condition because it is less than 80.

The third condition is true because 76 falls between 70 and 80.

Therefore, the program prints:

Grade C

Chained Comparisons

The grading example also demonstrates a useful form of comparison:

70 <= score < 80

This checks whether score is greater than or equal to 70 and less than 80.

For example:

score = 75

print(70 <= score < 80)

Output:

True

Chained comparisons can make range-based conditions concise and readable.

Using Multiple Conditions

Sometimes a decision depends on more than one condition.

For example, suppose a student needs to achieve a particular score and complete a project.

The and operator can be used to combine the conditions:

score = 88
has_completed_project = True

if score >= 80 and has_completed_project:
print("The student meets both requirements")

Output:

The student meets both requirements

The and operator requires both conditions to be true.

and vs &

It is important to distinguish between and and &.

The and operator is used for combining logical conditions:

if score >= 80 and has_completed_project:
print("Requirements met")

The & operator is a bitwise operator and is used for bitwise operations on integers.

Therefore, when combining conditions inside an if statement, and should be used for logical conditions.

Nested if-else

Sometimes one decision depends on another decision.

In such cases, an if or else block can contain another if-else statement. This is called a nested if-else.

The basic structure is:

if condition:
if another_condition:
# code
else:
# code
else:
# code

Example

x = 8
y = 3

if x > 5:
if y > 5:
print("Both numbers are greater than 5")
else:
print("x is greater than 5, but y is not")
else:
print("x is not greater than 5")

Output:

x is greater than 5, but y is not

First, Python checks whether x > 5.

Since the condition is true, Python enters the first block and checks the next condition, y > 5.

Since y is 3, that condition is false, so the inner else block is executed.

Validating User Input

Conditional statements can also be used to perform simple validation.

For example, consider a registration process where the user enters a name, email address, and password:

name = input("Please enter your name: ")
email = input("Please enter your email: ")
password = input("Please enter your password: ")

if name == "":
print("Please enter a valid name")
else:
if "@" not in email:
print("Please enter a valid email")
else:
print("Login/registration is successful")

The program first checks whether a name has been entered.

If the name is valid, it checks the email address.

The membership operator not in is used to check whether "@" is absent from the email.

This example combines several concepts:

  • input()
  • if-else
  • nested conditions
  • string values
  • membership operators

Age-Based Decisions

Another common use of conditional statements is classifying values into different categories.

For example:

age = 42

if age < 18:
print("You are a minor")
elif 18 <= age < 65:
print("You are an adult")
else:
print("You are a senior citizen")

Output:

You are an adult

Here, the if-elif-else structure allows the program to handle three different age ranges.

Conditional Statements in a Real-Life Situation

The logic behind conditional statements is similar to everyday decision-making.

Consider this situation:

If the weather is sunny and the cricket kit is available, go play cricket. Otherwise, stay indoors.

This can be represented in Python as:

weather = "sunny"
has_cricket_kit = True

if weather == "sunny" and has_cricket_kit:
print("Go play cricket")
else:
print("Stay indoors")

Output:

Go play cricket

The program makes a decision based on two conditions:

  1. Whether the weather is sunny.
  2. Whether the cricket kit is available.

Both conditions must be true for the first block to execute.

Key Takeaways

Conditional statements allow Python programs to make decisions based on different conditions.

The main concepts covered are:

  • if executes a block when a condition is true.
  • if-else provides two possible paths.
  • if-elif-else handles multiple possible conditions.
  • Nested if-else allows one conditional decision to be placed inside another.
  • Conditions can be created using comparison operators such as >, <, >=, <=, ==, and !=.
  • The and operator can be used when multiple conditions must be satisfied.
  • Boolean variables can be used directly as conditions.
  • input() can be combined with conditional statements to make decisions based on user input.
  • The % operator can be combined with conditions to solve problems such as checking whether a number is even or odd.
  • Chained comparisons can be used to check whether a value falls within a particular range.
  • Indentation defines the blocks of code associated with conditional statements.

Conditional statements are an important part of programming because they allow a program to respond differently depending on the situation. Instead of following exactly the same path every time, a Python program can evaluate conditions and choose the appropriate block of code to execute.

--

--