Skip to main content

Flow control

if-else condition

age = 33

if (age >= 18):
print("You are adult person.")
else:
print("You are not an adult yet.")

You can have multiple conditions with elif statements:

age = 70

if (age >=18 and age <65):
print("You are adult person.")
elif (age >=65):
print("You are a senior.")
else:
print("You are not an adult yet.")

Now try with different age to see the results yourself. Note the use of and to combine conditions. In case of if/else condition check, once a if condition is satisfied, the code exits there, it does not check for next conditions.

Tax calculator

Another practical example: calculate personal income tax for Singapore residents.

src/sg_tax_calculator.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Program: Calculate individual tax in Singapore
Version: 20220107
@author: Pranab Das (GitHub: @pranabdas)
"""

income = input("Please enter your taxable yearly income (in SGD): ")

# check input is not null
if income:
pass
else:
print("Input not recognized.")
exit()

# check input is number
try:
income = float(income)
except:
print("Please enter number (float or integer) input.")
exit()

if (income <= 0):
print("You have no positive income. There is no tax.")
exit()
elif (income <= 20000):
print("Good news. You don't have to pay any income tax.")
exit()
elif (income <= 30000):
tax = (income - 20000) * 2.0 / 100
elif (income <= 40000):
tax = 200 + (income - 30000) * 3.5 / 100
elif (income <= 80000):
tax = 550 + (income - 40000) * 7.0 / 100
elif (income <= 120000):
tax = 3350 + (income - 80000) * 11.5 / 100
elif (income <= 160000):
tax = 7950 + (income - 120000) * 15.0 / 100
elif (income <= 200000):
tax = 13950 + (income - 160000) * 18.0 / 100
elif (income <= 240000):
tax = 21150 + (income - 200000) * 19.0 / 100
elif (income <= 280000):
tax = 28750 + (income - 240000) * 19.5 / 100
elif (income <= 320000):
tax = 36550 + (income - 280000) * 20.0 / 100
else:
tax = 44550 + (income - 320000) * 22.0 / 100

print("Payable tax: SGD", round(tax, 2))
print("Your take home amount: SGD", round(income-tax, 2))

Tax rates are correct as of January 2022. Please check with IRAS website.

tip

If you want to calculate your tax right now on the browser, you can use this web app.