个税模拟器,纯Python计算部分代码,仅供学习参考
权力游戏王者
2026年01月18日 02:06

文件获取地址:https://wenshushu.vip/pan/index.php?id=36 提取7bf9

哈喽各位小伙伴大家好!今天给大家带来一个超实用的Python项目——个人所得税模拟器教学演示版!

很多同学每次看到工资条上的“个人所得税”就一脸懵逼,今天我们就用Python把它彻底搞懂!不仅能算出你要交多少税,还能了解国家个税政策的精妙之处哦~

🔥 先上效果展示

输入你的月收入,一键计算出:

· 应缴个人所得税

· 税后实际到手工资

· 各个税率阶梯的明细

是不是很实用?下面就把核心代码分享给大家!

💻 核心计算代码全解析

```python

# -*- coding: utf-8 -*-

"""

个人所得税计算器 - 教学演示版

作者:你的技术宅小伙伴

适用:综合所得年度计税(工资薪金)

"""

def calculate_annual_tax(annual_income, special_deduction=0,

additional_deduction=0, has_housing_loan=False,

has_housing_rent=False, has_elderly_support=False,

has_children_education=False):

"""

计算年度个人所得税

参数说明:

annual_income: 年度总收入(元)

special_deduction: 专项扣除(三险一金等)

additional_deduction: 专项附加扣除

has_xxx: 各类附加扣除标志

"""

# 1. 基本减除费用(年度6万元)

basic_deduction = 60000

# 2. 计算专项附加扣除

if has_housing_loan:

additional_deduction += 12000 # 住房贷款利息

if has_housing_rent:

additional_deduction += 18000 # 住房租金(按大城市标准)

if has_elderly_support:

additional_deduction += 24000 # 赡养老人

if has_children_education:

additional_deduction += 12000 # 子女教育

# 3. 计算应纳税所得额

taxable_income = annual_income - basic_deduction - special_deduction - additional_deduction

# 如果应纳税所得额 <= 0,不需要缴税

if taxable_income <= 0:

return {

'annual_income': annual_income,

'taxable_income': 0,

'annual_tax': 0,

'after_tax_income': annual_income,

'tax_rate': '0%',

'details': '无需缴纳个人所得税'

}

# 4. 个人所得税税率表(年度综合所得)

tax_brackets = [

(0, 36000, 0.03, 0),

(36000, 144000, 0.10, 2520),

(144000, 300000, 0.20, 16920),

(300000, 420000, 0.25, 31920),

(420000, 660000, 0.30, 52920),

(660000, 960000, 0.35, 85920),

(960000, float('inf'), 0.45, 181920)

]

# 5. 计算应纳税额

annual_tax = 0

tax_details = []

for i, (lower, upper, rate, quick_deduction) in enumerate(tax_brackets):

if taxable_income > lower:

# 当前区间的应税金额

bracket_income = min(taxable_income, upper) - lower

if bracket_income > 0:

bracket_tax = bracket_income * rate

annual_tax += bracket_tax

tax_details.append({

'level': i + 1,

'income_range': f"{lower/10000:.1f}万-{upper/10000:.1f}万" if upper != float('inf') else f"{lower/10000:.1f}万以上",

'taxable_amount': bracket_income,

'tax_rate': f"{rate*100:.0f}%",

'tax_amount': bracket_tax

})

# 6. 速算扣除数法(更简洁的计算方式)

# 找到对应的税率等级

for lower, upper, rate, quick_deduction in tax_brackets:

if lower <= taxable_income < upper:

annual_tax_quick = taxable_income * rate - quick_deduction

break

# 7. 返回计算结果

return {

'annual_income': annual_income,

'basic_deduction': basic_deduction,

'special_deduction': special_deduction,

'additional_deduction': additional_deduction,

'taxable_income': taxable_income,

'annual_tax': annual_tax,

'annual_tax_quick': annual_tax_quick, # 速算扣除法结果

'after_tax_income': annual_income - annual_tax,

'effective_tax_rate': annual_tax / annual_income * 100,

'monthly_tax': annual_tax / 12,

'monthly_income_after_tax': (annual_income - annual_tax) / 12,

'tax_details': tax_details

}

def calculate_monthly_tax_with_accumulation(monthly_income, month,

special_deduction_monthly=0,

additional_deduction_monthly=0):

"""

按月累计预扣法计算个人所得税(实际发工资时的算法)

这个算法更接近实际情况!

"""

# 累计收入

accumulated_income = monthly_income * month

# 累计减除费用

accumulated_deduction = 5000 * month # 每月5000元

# 累计专项扣除和附加扣除

accumulated_special = special_deduction_monthly * month

accumulated_additional = additional_deduction_monthly * month

# 累计应纳税所得额

accumulated_taxable = accumulated_income - accumulated_deduction - accumulated_special - accumulated_additional

if accumulated_taxable <= 0:

return {

'month': month,

'monthly_income': monthly_income,

'current_month_tax': 0,

'accumulated_tax': 0,

'income_after_tax': monthly_income

}

# 使用年度税率表计算累计应缴税额

tax_brackets = [

(0, 36000, 0.03, 0),

(36000, 144000, 0.10, 2520),

(144000, 300000, 0.20, 16920),

(300000, 420000, 0.25, 31920),

(420000, 660000, 0.30, 52920),

(660000, 960000, 0.35, 85920),

(960000, float('inf'), 0.45, 181920)

]

# 计算累计应缴税额

accumulated_tax = 0

for lower, upper, rate, quick_deduction in tax_brackets:

if lower <= accumulated_taxable < upper:

accumulated_tax = accumulated_taxable * rate - quick_deduction

break

# 计算本月应缴税额(累计应缴 - 已缴)

previous_accumulated_taxable = max(0, accumulated_taxable - monthly_income)

previous_tax = 0

for lower, upper, rate, quick_deduction in tax_brackets:

if lower <= previous_accumulated_taxable < upper:

previous_tax = previous_accumulated_taxable * rate - quick_deduction

break

current_month_tax = accumulated_tax - previous_tax

return {

'month': month,

'monthly_income': monthly_income,

'accumulated_income': accumulated_income,

'accumulated_taxable': accumulated_taxable,

'current_month_tax': current_month_tax,

'accumulated_tax': accumulated_tax,

'income_after_tax': monthly_income - current_month_tax,

'notice': f"第{month}月,累计收入{accumulated_income:.2f}元,本月缴税{current_month_tax:.2f}元"

}

def print_tax_report(result):

"""

漂亮地打印个税计算结果

"""

print("=" * 50)

print("📊 个人所得税计算报告")

print("=" * 50)

print(f"💰 年度总收入: {result['annual_income']:,.2f} 元")

print(f"📝 应纳税所得额: {result['taxable_income']:,.2f} 元")

print(f"🧾 年度应缴个税: {result['annual_tax']:,.2f} 元")

print(f"💵 税后年收入: {result['after_tax_income']:,.2f} 元")

print(f"📈 实际税率: {result['effective_tax_rate']:.2f}%")

print(f"🗓️ 平均每月缴税: {result['monthly_tax']:,.2f} 元")

print(f"💳 平均每月到手: {result['monthly_income_after_tax']:,.2f} 元")

if 'tax_details' in result and result['tax_details']:

print("\n📋 税率阶梯明细:")

print("-" * 50)

for detail in result['tax_details']:

print(f"等级{detail['level']}: {detail['income_range']}")

print(f" 应税金额: {detail['taxable_amount']:,.2f} 元")

print(f" 税率: {detail['tax_rate']}")

print(f" 税额: {detail['tax_amount']:,.2f} 元")

print()

# 示例使用

if __name__ == "__main__":

print("🎯 个人所得税计算器 - 教学演示版")

print("🌟 作者:你的技术宅小伙伴")

print()

# 示例1:计算年度个税

print("示例1:计算年度个税")

result = calculate_annual_tax(

annual_income=250000, # 年收入25万

special_deduction=30000, # 三险一金等

has_housing_loan=True, # 有房贷

has_children_education=True # 有子女教育

)

print_tax_report(result)

# 示例2:模拟月度预扣预缴

print("\n示例2:月度累计预扣法模拟")

print("假设月薪20,000元,三险一金每月3,000元")

monthly_income = 20000

special_monthly = 3000

additional_monthly = 2000 # 假设每月专项附加扣除

for month in range(1, 13):

month_result = calculate_monthly_tax_with_accumulation(

monthly_income, month, special_monthly, additional_monthly

)

print(f"{month_result['notice']}")

print("\n✅ 计算完成!是不是很简单呢?")

```

🎯 代码亮点解析

1. 两种计税算法

· calculate_annual_tax():年度汇算清缴算法

· calculate_monthly_tax_with_accumulation():月度累计预扣法(实际工资算法)

2. 完整的扣除项

· 基本减除费用(每月5000元)

· 专项扣除(三险一金)

· 专项附加扣除(房贷、房租、子女教育、赡养老人)

3. 清晰的税率阶梯

```python

tax_brackets = [

(0, 36000, 0.03, 0), # 不超过3.6万元,税率3%

(36000, 144000, 0.10, 2520), # 超过3.6万至14.4万,税率10%

# ... 更多税率等级

]

```

💡 学习要点

1. Python字典的灵活运用:用字典组织复杂数据

2. 税率阶梯算法:两种实现方式(分段计算和速算扣除)

3. 函数封装思想:每个函数只做一件事

4. 实际业务逻辑:理解真实的个税计算规则

🚀 如何扩展这个项目?

1. 添加GUI界面:用tkinter或PyQt做可视化

2. 做成Web应用:用Flask或Django

3. 添加更多扣除项:继续教育、大病医疗等

4. 数据可视化:用matplotlib画税率变化图

📦 完整项目获取

需要完整项目代码(包括GUI版本)的小伙伴,可以在评论区留言,或者关注我的GitHub仓库~

🎉 最后说两句

这个个税计算器虽然代码不长,但包含了实际业务中的核心逻辑。通过学习这个项目,你不仅能掌握Python编程技巧,还能真正理解个人所得税的计算原理。

记住: 技术学习的最终目的是解决实际问题!当你用自己写的代码算出个人所得税时,那种成就感是无与伦比的!

---

求三连!求关注! 🎉

如果你觉得这个教程有帮助,请务必:

· 👍 点赞

· 💬 评论

· ➕ 关注

下期预告:《用Python分析你的工资条——数据可视化实战》

有任何问题欢迎在评论区讨论,我都会回复的!

#Python #​编程教程 #个人所得税 #​税务计算 #编程学习