Python Conditional Statements

Python 9 min min read Updated: Mar 09, 2026 Beginner
Python Conditional Statements
Beginner Topic 6 of 10

Conditional Statements in Python

Conditional statements allow a Python program to make decisions based on certain conditions. Instead of executing every line of code sequentially, conditional statements enable programs to choose different actions depending on whether a condition is true or false.

These statements are extremely useful when building real-world applications such as login systems, form validations, grading systems, or decision-based programs.

What are Conditional Statements?

A conditional statement checks a condition and executes a block of code depending on the result. If the condition evaluates to True, Python executes one block of code; otherwise, it may execute another block.

Python mainly provides the following conditional statements:

  • if statement
  • if...else statement
  • if...elif...else statement
  • Nested if statement
  • Short-hand conditional statements

The if Statement

The if statement is used to execute a block of code only if a condition is true.

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

In this example, the message will only be printed if the condition age >= 18 evaluates to true.

The if...else Statement

The if...else statement provides two possible paths of execution. If the condition is true, the if block runs; otherwise, the else block runs.

python number = 10 if number % 2 == 0: print("Even Number") else: print("Odd Number")

This program checks whether a number is even or odd.

The if...elif...else Statement

Sometimes we need to evaluate multiple conditions. Python provides the elif (else if) statement to handle multiple conditions.

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

This example determines the grade based on marks obtained.

Nested if Statements

A nested if statement means placing one if statement inside another. This is useful when multiple conditions must be checked step by step.

python age = 20 citizen = True if age >= 18: if citizen: print("You can vote")

Here, the second condition is only checked if the first condition is true.

Using Logical Operators with Conditions

Logical operators can combine multiple conditions in a single statement.

python age = 25 salary = 40000 if age > 18 and salary > 30000: print("Loan Approved")

The program checks two conditions simultaneously using the and operator.

Short-hand if Statement

Python allows writing simple conditions in a single line for cleaner code.

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

Short-hand if...else (Ternary Operator)

The ternary operator allows you to write conditional expressions in a single line.

python age = 16 result = "Adult" if age >= 18 else "Minor" print(result)

Indentation in Conditional Statements

Python uses indentation to define the block of code that belongs to a condition. Proper indentation is required, otherwise Python will generate an error.

python score = 85 if score > 80: print("Excellent Performance")

The indented line belongs to the if block.

Best Practices for Conditional Statements

  • Use clear and meaningful conditions.
  • Avoid deeply nested conditions if possible.
  • Use logical operators to simplify complex conditions.
  • Keep code readable with proper indentation.

Real-World Example

Conditional statements are widely used in real-world applications such as authentication systems.

python username = "admin" password = "1234" if username == "admin" and password == "1234": print("Login Successful") else: print("Invalid Credentials")

Conclusion

Conditional statements are one of the most important concepts in Python programming. They allow programs to make decisions and control the flow of execution based on different conditions.

By mastering conditional statements, you can build smarter and more dynamic Python programs that react differently depending on user input or data conditions.

In the next tutorial, we will learn about Loops in Python and how to repeat tasks efficiently using Python.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators