Decision-Making Statements in Python with Examples

Python has several types of decision-making statements: if, if-else, if-elif-else, and nested statements.

These statements are based on conditions that the program checks.

If the condition is true, a set of statements are executed. If it is false, a different set of statements is executed.

Here is an example of a typical structure of decision-making statements in Python.

Structure of decision-making statements in Python

Any value that is not zero or null is TRUE in the Python programming language. If the value is either zero or null, it is FALSE.

If you want to learn more about Python or are just starting out, our Python Tutorial for Beginners is a great resource.

Types of Decision-Making Statements in Python

Below are the four types of Decision-Making Statements in Python:

  1. If statements
  2. If-else Statement
  3. If-Elif-Else Statement
  4. Nested If Statement

Python If Statement

The Python If Statement is the same as in other languages.

The if statement has a logical expression that compares two sets of data and makes a decision based on the result.

If Statement Syntax:

The following code below is the Python If Statement Syntax:

if expression:
    #execute your code

The block of statements contained inside the if statement is executed if the boolean expression evaluates to TRUE.

The first set of instructions following the end of the if statement(s) is executed if the boolean expression evaluates to FALSE.

If Statement Flow Diagram

Here is the If Statement Flow Diagram:

Decision Making Statements in Python

Example:
a = 100
if a:
   print ("1 - Got a true expression value")
   print (a)

b = 0
if b:
   print ("2 - Got a true expression value")
   print (b)
print ("Done!")

To test your Python code from this lesson, use your code editor like PyCharm.

You can test the above example here! ➡Python Online Compiler

Output:
1 - Got a true expression value
100
Done!

Python If-else Statement

The Python if-else statement is used to run both the true and false parts of a condition.

If the condition is true, the code in the if block is executed. If it is false, the code in the else block is executed.

If-else Statement Syntax

The following code below is the Python If-else Statement Syntax:

if expression:
    #execute your code
else:
    #execute your code

When the test condition is True, the if-else statement analyzes the test expression and executes the body of the if.

The else clause’s body is executed if the condition is False. The blocks are separated by indentation.

If-else Statement Flow Diagram

Here is the If-else Statement Flow Diagram:

Decision Making Statements in Python

Example:
a = 15
b = 20

if a > b:
    #False block
    print("a is greater")
else:
    #True block
    print("b is greater")

You can test the above example here! ➡Python Online Compiler

Output:

b is greater

Python If-elif-else Statement

Python If-elif-else statement checks the condition of the if statement.

If the condition is False, then the elif statement is evaluated.

If the elif condition is False, then the else statement is evaluated.

If-elif-else Statement Syntax

The following code below is the If-elif-else Statement Syntax:

if expression:
    #execute your code
elif expression:
    #execute your code
else:
    #execute your code

With the elif statement, you can check if more than one expression evaluates to TRUE and run a block of code when one of them does.

The elif statement is a Python keyword that can be used instead of else if to add another condition to a program. We call this “chained conditional.”

If-elif-else Statement Flow Diagram

Here is the Python If-elif-else Statement Flow Diagram:

Decision Making Statements in Python

Example:
a = 15
b = 15

if a > b:
    #if statement
    print("a is greater")
elif a == b:
    #if the if statement is False, else if condition is executed
    print("both are equal")
else:
    #else statement is executed, if both if and elif statement if False
    print("b is greater")

You can test the above example here! ➡Python Online Compiler

Output:
both are equal

Python Nested If Statement

Another Decision-Making Statement is the Python Nested If statement is one that has an if statement inside of another if statement.

This happens when you need to filter a variable more than once.

Nested If Statement Syntax

The following code below is the Python Nested If Statement Syntax:

if expression:
    if nested expression:
        #execute your code
    else:
        #execute your code
else:
    #execute your code

When you have If statements inside of each other, you should always pay attention to the indentation to show what each statement is about.

You can have as many levels of nesting as you want, but that makes the program less efficient and harder to read and understand.

So, you should always try to use as few nested IF statements as possible.

Nested If Statement Flow Diagram

Here is the Nested If Statement Flow Diagram:

Decision Making Statements in Python

Example:
num = 5
if num >= 0:
    if num == 0:
        print("Zero")
    else:
        print("Positive number")
else:
    print("Negative number")

Output:
Positive number

Single Statement Suites

In Single Statement Suites, programmers can place the block of an executable statement of an if-clause on the same line as a header statement if the block only has one line.

Example:

Below is an example of a Single line if-clause:

a = 20

if (a == 20): print("The value of a is 20")

You can test the above example here! ➡Python Online Compiler

Output
The value of a is 20

Best Practices for Using Decision-Making Statements

When using decision-making statements in Python, it is essential to follow some best practices to ensure clean and readable code:

  • Use meaningful variable and function names.
  • Keep the code within the decision-making statements concise and focused.
  • Avoid deeply nested decision-making statements; refactor complex conditions into separate functions.
  • Comment your code to explain the logic behind the conditions and decisions.

Summary

In summary, you learned about if statements, if-else statements, if-elif-else statements, and nested if statements.

I hope that this tutorial helped you understand how to use decision-making statements in Python.

Check out our list of Python Tutorial Topics if you missed any of our previous lessons.

You are one step closer to creating stronger, more effective codes now that you are aware of what decision-making statements in Python are.

In your code, start implementing all the if-else statements you have studied today.

In the next post, “Python Loops,” you’ll learn about the different kinds of Python loops and how to use them.

Related Python Tutorials


Common use cases for Decision-Making Statements

  • Data pipelines. Python is the standard for ETL, data analysis, and ML workflows.
  • Web development. Django and FastAPI power modern web backends and APIs.
  • Automation and scripting. System administration, file processing, web scraping, and cron jobs.
  • Machine learning. scikit-learn, PyTorch, TensorFlow, Hugging Face for AI/ML projects.
  • Educational tools. Python’s readability makes it the go-to teaching language.

Working code example

from typing import Optional

def process_data(items: list[dict]) -> Optional[dict]:
    """Process a list of items and return summary stats."""
    if not items:
        return None
    return {
        "count": len(items),
        "total": sum(item.get("value", 0) for item in items),
        "avg": sum(item.get("value", 0) for item in items) / len(items),
    }

# Usage
data = [{"value": 10}, {"value": 20}, {"value": 30}]
summary = process_data(data)
print(summary)  # {'count': 3, 'total': 60, 'avg': 20.0}

Best practices

  • Use type hints. list[dict], Optional[str], and TypedDict make code self-documenting and enable static analysis.
  • Follow PEP 8. Consistent style improves readability. Use black or ruff to auto-format.
  • Prefer f-strings. f”{value}” is cleaner than str.format() or % formatting.
  • Write tests with pytest. Aim for 70%+ coverage on business-critical modules.
  • Use ruff or pylint. Static analysis catches many bugs before code runs.

Common pitfalls

  • Mutable default arguments. def f(x=[]) reuses the same list across calls. Use x=None then check.
  • Integer division. 5/2 gives 2.5 in Python 3. Use // for floor division.
  • Missing self on methods. Class methods need self as first parameter.
  • Late binding closures. Loops that create lambdas can capture variables late.

Frequently Asked Questions

What Python version does this tutorial target?
This tutorial targets Python 3.10 or higher. Most examples work on 3.8+, but newer features (match statements, pipe union types, structural pattern matching) need 3.10+. For deep learning content, Python 3.11 is recommended for best performance.
How do I install Python for this tutorial?
Download Python 3.11 or higher from python.org. On Windows, tick ‘Add to PATH’ during install. On Mac use Homebrew (brew install python). On Linux use your package manager or pyenv for version management.
Do I need pip and virtual environments?
Yes. pip comes with Python. For any project beyond a single script, create a virtual environment: python -m venv venv, then activate and pip install dependencies. This keeps project libraries isolated.
Can I use this in a Jupyter notebook or Google Colab?
Most examples run in both. Colab is great for ML tutorials since it provides free GPU access. Jupyter is better for local iterative development. Just paste the code into a cell and run.
Where can I find more Python practice projects?
Browse itsourcecode.com Python Projects for 250+ free capstone-ready systems (sentiment analysis, image classification, chatbots, LangChain apps). Each includes full source code, dataset links, and installation instructions.

Leave a Comment