Skip to content

Instantly share code, notes, and snippets.

@0xferit
Last active February 12, 2025 15:56
Show Gist options
  • Save 0xferit/086c232c0f73153e4da1479774969219 to your computer and use it in GitHub Desktop.
Save 0xferit/086c232c0f73153e4da1479774969219 to your computer and use it in GitHub Desktop.
portugal_income_tax.py
def calculate_tax(income):
# Calculate and deduct social security contribution first
social_security = income * 0.11
income -= social_security
# Income is revenue minus expenses.
# In simple accounting, 75% of income is taxable (with 25% assumed as expenses).
# Save the original taxable income for both progressive tax and solidarity tax calculations.
original_taxable_income = income * 0.75
# Create a copy for progressive tax calculation
taxable_income = original_taxable_income
# Calculate progressive tax
progressive_tax = 0
if taxable_income > 83696:
progressive_tax += (taxable_income - 83696) * 0.48
taxable_income = 83696
if taxable_income > 44987:
progressive_tax += (taxable_income - 44987) * 0.45
taxable_income = 44987
if taxable_income > 41629:
progressive_tax += (taxable_income - 41629) * 0.435
taxable_income = 41629
if taxable_income > 28400:
progressive_tax += (taxable_income - 28400) * 0.355
taxable_income = 28400
if taxable_income > 22306:
progressive_tax += (taxable_income - 22306) * 0.32
taxable_income = 22306
if taxable_income > 17233:
progressive_tax += (taxable_income - 17233) * 0.25
taxable_income = 17233
if taxable_income > 12160:
progressive_tax += (taxable_income - 12160) * 0.22
taxable_income = 12160
if taxable_income > 8059:
progressive_tax += (taxable_income - 8059) * 0.165
taxable_income = 8059
if taxable_income > 0:
progressive_tax += taxable_income * 0.13
# Calculate solidarity taxes separately.
solidarity_tax = 0
# For original taxable income above 80,000, apply 2.5% on the portion between 80,000 and 250,000.
if original_taxable_income > 80000:
taxable_for_2_5 = min(original_taxable_income, 250000) - 80000
solidarity_tax += taxable_for_2_5 * 0.025
# For original taxable income above 250,000, apply an additional 5% on the excess.
if original_taxable_income > 250000:
extra_tax = original_taxable_income - 250000
solidarity_tax += extra_tax * 0.05
total_tax = progressive_tax + solidarity_tax
return total_tax, solidarity_tax, original_taxable_income
if __name__ == "__main__":
try:
user_input = input("Enter your income (default 200K): ").strip()
# If no input is given, assume the income is 200K.
if user_input == "":
income = 200000.0
else:
income = float(user_input)
# Warn the user if the income is more than 200K.
if income > 200000:
print("Warning: The simplified regime only applies for incomes below 200K. Results might not be accurate for higher incomes.")
total_tax, solidarity_tax, taxable = calculate_tax(income)
effective = total_tax / income
print(f"Taxable income: {taxable:.2f}")
print(f"Tax owed: {total_tax:.2f}")
print(f"Effective rate: {effective:.2f}")
print(f"Solidarity tax paid: {solidarity_tax:.2f}")
except ValueError:
print("Please enter a valid number for income")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment