Skip to Content

Python for Fintech Professionals: Conditionals

Conditionals allow code execution based on whether a specific condition is true or false, using "if", "elif", and "else" statements.
This is the tenth blog on Python for fintch professionals.

Conditional statements allow us to execute different actions based on specific results or criteria. This is especially useful when we want to apply different logic based on numerical inputs, such as calculating sales commissions. In this blog post, we'll explore how to use "if", "elif", and "else" statements in Python through a practical example involving sales commission.

The Basics of "if" Statements

Let’s start with the simplest form of a conditional statement using "if". Suppose we have a sales commission structure where the commission is 5% for sales under one million and 10% for sales exceeding one million. We can implement this logic in Python as follows:

sales = int(input('Provide sales amount to calculate commission: '))
​commission_percentage = 0.05  # Default commission percentage
if sales >= 1000000:
​commission_percentage = 0.10  # Update to 10% for sales over a million
print('Commission amount is', int(commission_percentage * sales), 'at', commission_percentage * 100, 'percent')

In the code above:

  • We prompt the user to input the sales amount.
  • The default commission percentage is set to 5%.
  • We use an "if" statement to check if the sales are one million or more. If true, we update the commission percentage to 10%.
  • Finally, we calculate and print the commission amount based on the applicable percentage.

Key Points

  • The colon ":" after the "if" statement is essential in Python.
  • The block of code that follows must be indented, as indentation indicates which statements belong to the `if` condition.

Using "elif" for Multiple Conditions

Sometimes, we may need to check for more than one condition. This is where the "elif" (short for "else if") statement comes in handy. Let's modify our previous example to clarify its use:

sales = int(input('Provide sales amount to calculate commission: '))
if sales < 1000000:
commission_percentage = 0.05
elif sales >= 1000000:
commission_percentage = 0.10
print('Commission amount is', int(commission_percentage * sales), 'at', commission_percentage * 100, 'percent')

In this code:

  • We check if the sales amount is less than a million. If it is, the commission remains at 5%.
  • If the sales are one million or more, the commission changes to 10%.
  • You can chain multiple "elif" statements if needed, allowing for complex logic.

The Role of "else"

The "else" statement is used to define what should happen when none of the preceding conditions are met. Here's how it can be incorporated into our commission calculation:

sales = int(input('Provide sales amount to calculate commission: '))
if sales < 1000000:
commission_percentage = 0.05
else:
commission_percentage = 0.10
print('Commission amount is', int(commission_percentage * sales), 'at', commission_percentage * 100, 'percent')

In this version:

  • If the sales are below one million, the commission percentage is set to 5%.
  • The "else" clause captures all other scenarios (in this case, sales of one million or more), assigning a commission of 10%.

Nesting Conditional Statements

Python allows you to nest "if" and "else" statements within each other. This means you can place one conditional statement inside another to create more complex logic. Here’s a simple example:

sales = int(input('Provide sales amount to calculate commission: '))
if sales < 500000:
commission_percentage = 0.03  # 3% for sales less than 500,000
elif sales < 1000000:
  commission_percentage = 0.05  # 5% for sales between 500,000 and 1,000,000
else:
  commission_percentage = 0.10  # 10% for sales over 1,000,000
print('Commission amount is', int(commission_percentage * sales), 'at', commission_percentage * 100, 'percent')

Conclusion

Understanding how to use conditional statements allow us to can handle various scenarios and execute the appropriate actions based on user inputs.


Share this post
Tags
Sign in to leave a comment
Python for Fintech Professionals: Boolean Logic
Boolean logic in Python allows the use operators like "and", "or", and "not" to combine conditional statements.