Python If, Else, and For Loops with Examples

Why Python Conditional Logic Matters in Finance

Conditional logic is at the heart of almost every financial workflow. It refers to conditional rules like “if this is true, then do that.” This kind of logic appears often in financial modeling, such as flagging companies with declining margins or applying valuation multiples only when earnings growth meets a minimum threshold.

Python’s if, else statements, and for loops let you translate those rules into clean, reusable code that is easy to apply across multiple inputs. These same rule-based tasks are often handled in Excel. However, as workflows become more complex, spreadsheets can quickly become cluttered and harder to manage. Python handles larger volumes of logic more efficiently. It’s also easier to read, test, and maintain over time, especially when working with structured data.

This guide walks through Python’s core control flow, the order of execution for statements and loops, using simple examples from real-world financial analysis tasks.

Python If, Else, and For Loops - Conditional Statements & For Loops
Source: CFI’s Getting Started with Python course

Key Highlights

  • Python’s if, elif, and else statements let you apply rule-based logic to financial data.
  • For loops allow you to automate repetitive tasks like customer or transaction classification.
  • Combining control flow tools with data structures like lists and dictionaries helps scale financial analysis efficiently.

How If, Else, and Elif Work in Python

In Python, if, elif, and else are used to create conditional branches in your code. They help control which actions should be taken depending on whether certain conditions are true or false.

The structure works like this:

  • if checks the first condition. If it’s true, that block runs.
  • elif stands for “else if.” It lets you check another condition if the previous one was false.
  • else runs only if none of the above conditions are true.

Here’s a simple example using a revenue variable:

Python If, Else, and For Loops - Revenue Variable
Source: CFI’s Getting Started with Python course

In this example:

  • If revenue is greater than 15, Python prints “High revenue.”
  • If that’s not true, it checks whether revenue is less than 10.
  • If neither condition is true, the final else block runs.

Python reads these blocks in order and stops as soon as it finds a condition that evaluates to true. Only one of the three blocks will execute.

This pattern is useful when you’re working with thresholds, rules, or tiered outputs. This type of structure is exactly the kind you’ll use in financial modeling, reporting logic, or performance analysis.

Using Python If Else Logic to Sort Customers by Revenue

Let’s say you want to categorize customers based on their annual revenue, a common step in segmentation and opportunity sizing. For example, a company might classify customers who generate over $15,000 in yearly revenue as “High,” customers under $10,000 as “Low,” and everyone else as “Medium.”

The following variables represent the annual revenue for three different customers:

  • customer_a = 20
  • customer_b = 5
  • customer_c = 10

These values could represent thousands of dollars, such as $20,000 for customer A. With conditional statements, you can assign each customer to the appropriate segment. 

Here’s how you can do it with if, elif, and else:

Python If, Else, and For Loops - If, Elif, and Else
Source: CFI’s Getting Started with Python course

This setup checks the revenue amount for a single customer. You can repeat the logic for customer_b and customer_c to get a quick sense of how different clients are classified.

If you’re looking to target customers with lower spending levels for upsell campaigns, adding a check for those under 10, as shown here, can be a useful step in your analysis pipeline.

Automating Tasks Using For Loops

When you’re working with only a few values, writing separate if statements might be fine. However, repeating the same code isn’t practical if you need to classify hundreds of customers or transactions.

With a for loop, you can apply the same logic to every item in a list without writing repetitive code.

Here’s how you could classify multiple customers with just a few lines:

Python If, Else, and For Loops - Classifying Multiple Customers
Source: CFI’s Getting Started with Python course

Each revenue value in the list is processed one by one. The loop checks the conditions for each and prints the corresponding label. This approach allows you to scale your analysis across an entire dataset of customers, with logic that’s easy to adjust or extend.

Classifying Sales Transactions by Value

This same approach can be used to classify individual sales transactions by dollar amount. For example, you might categorize sales into high, medium, or low value tiers to support reporting or performance analysis.

Python If, Else, and For Loops - Classifying Sales Transactions by Value
Source: CFI’s Getting Started with Python course

You could use this logic to set alerts, trigger pricing tiers, or track performance against thresholds. Changing the values inside the conditions is all it takes to update the classification system.

Calculating Net Sales from a Tax Dictionary

In many financial processes, gross sales need to be adjusted for tax. Dictionaries, another common data structure in Python, are ideal for mapping transactions to their respective values. 

Here’s how to subtract tax from each transaction to calculate net sales:

Calculating Net Sales from a Tax Dictionary
Source: CFI’s Getting Started with Python course

This structure mirrors how you might build a margin or net revenue calculation across multiple transactions. It’s clean, auditable, and easy to update if values change.

Looping Through Keys and Values in a Dictionary

Sometimes, it’s helpful to access both the transaction name (the key) and amount (the value) at the same time. Python’s .items() method lets you retrieve both at once:

Looping Through Keys and Values in a Dictionary
Source: CFI’s Getting Started with Python course

This is especially useful in building a finance dashboard, summary table, or audit log from raw data. 

Build Smarter Workflows with Python If Else and For Loops

Python’s control flow tools — if, else, elif, and for loops — help structure decisions and automate tasks in financial workflows. Whether you’re segmenting customer revenue, flagging high-value transactions, or calculating net figures across datasets, this logic makes your code more adaptable and easier to manage.

These tools are foundational, but they’re powerful. Once you’re comfortable writing logic like this, you’re ready to build scripts that can process large datasets efficiently without repeating your code.

Ready to build smarter financial workflows with Python?

Explore CFI’s Python courses to take your next step in data-driven finance.

Additional Resources

Transitioning from Excel to Python 

Python Data Structures 

Python Variables 

See all Data Science resources

0 search results for ‘