codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐍

Python Programming

14 / 68 topics
14Python if...else Statement15Python Match Statement16Python for Loop & range()17Python while Loop18Python break, continue, and pass
Tutorials/Python Programming/Python if...else Statement
🐍Python Programming

Python if...else Statement

Updated 2026-04-20
3 min read

Python if...else Statement

The if...else statement is a fundamental control flow construct in Python that allows you to execute different blocks of code based on certain conditions. It enables your programs to make decisions and perform actions accordingly, making it essential for building dynamic and interactive applications.

Overview

In Python, the if...else statement follows a simple syntax:

if condition:
    # Code block executed if condition is True
else:
    # Code block executed if condition is False
  • Condition: This is an expression that evaluates to either True or False.
  • Code Block: A block of code that gets executed only if the associated condition is met.

Basic Usage

Let's start with a basic example:

x = 10

if x > 5:
    print("x is greater than 5")
else:
    print("x is not greater than 5")

In this example, since x is indeed greater than 5, the output will be:

x is greater than 5

Multiple Conditions with elif

You can use multiple conditions by chaining them together using elif (short for "else if"):

score = 85

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

In this example, the output will be:

Grade: B

The conditions are checked sequentially, and as soon as one condition is met, the corresponding block of code is executed, and the rest of the elif and else blocks are skipped.

Nested if...else Statements

You can also nest if...else statements inside each other to handle more complex decision-making:

num = 15

if num > 0:
    print("Positive number")
    if num % 2 == 0:
        print("Even number")
    else:
        print("Odd number")
else:
    print("Non-positive number")

In this example, the output will be:

Positive number
Odd number

Best Practices

  1. Indentation: Python relies on indentation to define code blocks. Ensure that all lines in a block are indented consistently.
  2. Readability: Use descriptive variable names and keep conditions simple for better readability.
  3. Avoid Deep Nesting: Excessive nesting can make your code difficult to read and maintain. Consider using other control structures like loops or functions to simplify complex logic.

Advanced Usage

Ternary Operator

Python also supports a ternary operator, which is a concise way to write simple if...else statements:

x = 10
result = "Even" if x % 2 == 0 else "Odd"
print(result)

In this example, the output will be:

Even

Chaining Comparisons

You can chain multiple comparisons in a single condition for more compact code:

age = 18

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

In this example, the output will be:

Adult

Common Mistakes to Avoid

  1. Forgetting Colons: Ensure that each if, elif, and else statement ends with a colon (:).
  2. Incorrect Indentation: Python is sensitive to indentation. Mixing tabs and spaces or inconsistent indentation can lead to syntax errors.
  3. Neglecting Parentheses: While not required, using parentheses for complex conditions can improve readability.

Conclusion

The if...else statement is a powerful tool in Python that allows your programs to make decisions based on conditions. By understanding how to use it effectively, you can write more dynamic and responsive applications. Remember to follow best practices for code readability and maintainability, and always test your conditions thoroughly to ensure they behave as expected.

Feel free to practice these concepts by writing small scripts or integrating them into larger projects. Happy coding!


PreviousPython Operators & PrecedenceNext Python Match Statement

Recommended Gear

Python Operators & PrecedencePython Match Statement