Python Program to Calculate Income Tax with Examples
if-elif-else statements to check income slabs and apply tax rates, for example: if income <= 250000: tax = 0 elif income <= 500000: tax = (income - 250000) * 0.05.Examples
How to Think About It
if conditions. Calculate tax only on the amount that falls within each slab and add them up to get total tax.Algorithm
Code
income = float(input("Enter your income: ")) tax = 0 if income <= 250000: tax = 0 elif income <= 500000: tax = (income - 250000) * 0.05 elif income <= 1000000: tax = 250000 * 0.05 + (income - 500000) * 0.20 else: tax = 250000 * 0.05 + 500000 * 0.20 + (income - 1000000) * 0.30 print(f"Income: {income}") print(f"Tax: {tax}")
Dry Run
Let's trace the income 400000 through the code
Input income
income = 400000
Check income <= 250000
400000 <= 250000 is False, move to next condition
Check income <= 500000
400000 <= 500000 is True, calculate tax = (400000 - 250000) * 0.05 = 7500
Print results
Income: 400000.0, Tax: 7500.0
| Step | Income | Tax Calculation | Tax |
|---|---|---|---|
| 1 | 400000 | N/A | 0 |
| 2 | 400000 | Check <=250000 | 0 |
| 3 | 400000 | (400000-250000)*0.05 | 7500 |
| 4 | 400000 | Print results | 7500 |
Why This Works
Step 1: Check income slabs
The program uses if-elif-else to check which tax slab the income falls into.
Step 2: Calculate tax for each slab
Tax is calculated only on the income amount above the lower limit of each slab using subtraction and multiplication.
Step 3: Add tax from all slabs
For incomes in higher slabs, tax from lower slabs is added to the current slab's tax to get total tax.
Alternative Approaches
def calculate_tax(income): if income <= 250000: return 0 elif income <= 500000: return (income - 250000) * 0.05 elif income <= 1000000: return 250000 * 0.05 + (income - 500000) * 0.20 else: return 250000 * 0.05 + 500000 * 0.20 + (income - 1000000) * 0.30 income = float(input("Enter your income: ")) tax = calculate_tax(income) print(f"Income: {income}") print(f"Tax: {tax}")
income = float(input("Enter your income: ")) slabs = [(250000, 0), (500000, 0.05), (1000000, 0.20), (float('inf'), 0.30)] tax = 0 prev_limit = 0 for limit, rate in slabs: if income > limit: tax += (limit - prev_limit) * rate prev_limit = limit else: tax += (income - prev_limit) * rate break print(f"Income: {income}") print(f"Tax: {tax}")
Complexity: O(1) time, O(1) space
Time Complexity
The program uses a fixed number of conditional checks without loops over input size, so it runs in constant time.
Space Complexity
Only a few variables are used to store income and tax, so space usage is constant.
Which Approach is Fastest?
All approaches run in constant time; using functions improves readability, while dictionary-based slabs add flexibility but slightly more code.
| Approach | Time | Space | Best For |
|---|---|---|---|
| If-elif-else | O(1) | O(1) | Simple and clear tax slabs |
| Function-based | O(1) | O(1) | Modular and reusable code |
| Dictionary and loop | O(1) | O(1) | Flexible slab management |