0
0
PythonProgramBeginner · 2 min read

Python Program to Calculate Income Tax with Examples

You can calculate income tax in Python by using 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

Input200000
OutputIncome: 200000 Tax: 0
Input400000
OutputIncome: 400000 Tax: 7500.0
Input1000000
OutputIncome: 1000000 Tax: 125000.0
🧠

How to Think About It

To calculate income tax, first understand the tax slabs and rates. Then check the income against each slab using if conditions. Calculate tax only on the amount that falls within each slab and add them up to get total tax.
📐

Algorithm

1
Get the income amount from the user
2
Check if income is less than or equal to 250000, then tax is 0
3
If income is between 250001 and 500000, calculate 5% tax on the amount above 250000
4
If income is between 500001 and 1000000, calculate tax for first slab plus 20% on amount above 500000
5
If income is above 1000000, calculate tax for previous slabs plus 30% on amount above 1000000
6
Print the total tax calculated
💻

Code

python
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}")
Output
Enter your income: 400000 Income: 400000.0 Tax: 7500.0
🔍

Dry Run

Let's trace the income 400000 through the code

1

Input income

income = 400000

2

Check income <= 250000

400000 <= 250000 is False, move to next condition

3

Check income <= 500000

400000 <= 500000 is True, calculate tax = (400000 - 250000) * 0.05 = 7500

4

Print results

Income: 400000.0, Tax: 7500.0

StepIncomeTax CalculationTax
1400000N/A0
2400000Check <=2500000
3400000(400000-250000)*0.057500
4400000Print results7500
💡

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

Using functions for modularity
python
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}")
This approach improves readability and reusability by separating tax calculation into a function.
Using dictionary for slabs and rates
python
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}")
This method uses a loop and data structure to handle slabs, making it easier to update slabs but slightly more complex.

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.

ApproachTimeSpaceBest For
If-elif-elseO(1)O(1)Simple and clear tax slabs
Function-basedO(1)O(1)Modular and reusable code
Dictionary and loopO(1)O(1)Flexible slab management
💡
Always test your tax calculator with incomes at slab boundaries to ensure correctness.
⚠️
Beginners often forget to subtract the lower slab limit before applying the tax rate, leading to incorrect tax amounts.