一、旧税率表与新税率表比较

以前起征点是3500,2018年10月1日起起征点正式修改为5000。

python计算个人所得税 利用python计算个人所得税_传递参数

下面我们用 Python 来分别计算新旧个人所得税分别为多少?

二、旧个人所得税

import sys
import bisect

# 方法一:手动计算
def old_tax(salary):
    if salary < 3500:
        return 0.0
    above = salary - 3500
    ranges = [0, 1500, 4500, 9000, 35000, 55000, 80000]
    rate = [0.03, 0.1, 0.2, 0.25, 0.3, 0.35, 0.45]
    i = bisect.bisect_left(ranges, above)
    j = 0
    tax = 0.0
    while j < i:
        if j+1 < i:
            tax += (ranges[j+1] - ranges[j]) * rate[j]
        else:
            tax += (above - ranges[j]) * rate[j]
        j += 1
    return tax
        
# 测试        
old_tax(6500) # 195.0
old_tax(15600) # 2020.0
old_tax(95000) # 27670.0

# 方法二:速扣数
def old_tax_2(salary):
    if salary < 3500:
        return 0.0
    above = salary - 3500
    ranges = [0, 1500, 4500, 9000, 35000, 55000, 80000]
    quick_count = [0, 105, 555, 1005, 2755, 5505, 13505]
    rate = [0.03, 0.1, 0.2, 0.25, 0.3, 0.35, 0.45]
    i = bisect.bisect_left(ranges, above)
    tax = above * rate[i-1] - quick_count[i-1]
    return tax
    
old_tax_2(6500) # 195.0
old_tax_2(15600) # 2020.0
old_tax_2(95000) # 27670.0

三、新个人所得税

import sys
import bisect

# 方法一:手动计算
def new_tax(salary):
    if salary < 5000:
        return 0.0
    above = salary - 5000
    ranges = [0, 3000, 12000, 25000, 35000, 55000, 80000]
    rate = [0.03, 0.10, 0.20, 0.25, 0.30, 0.35, 0.45]
    i = bisect.bisect_left(ranges, above)
    j = 0
    tax = 0.0
    while j < i:
        if j+1 < i:
            tax += (ranges[j+1] - ranges[j]) * rate[j]
        else:
            tax += (above - ranges[j]) * rate[j]
        j += 1
    return tax

new_tax(6500) # 45.0
new_tax(10500) # 340.0
new_tax(95000) # 25340.0

# 方法二:速扣数
def new_tax_2(salary):
    if salary < 5000:
        return 0.0
    above = salary - 5000
    ranges = [0, 3000, 12000, 25000, 35000, 55000, 80000]
    rate = [0.03, 0.10, 0.20, 0.25, 0.30, 0.35, 0.45]
    quick_count = [0, 210, 1410, 2660, 4410, 7160, 15160]
    i = bisect.bisect_left(ranges, above)
    tax = above * rate[i-1] - quick_count[i-1]
    return tax

new_tax_2(6500) # 45.0
new_tax_2(10500) # 340.0
new_tax_2(95000) # 25340.0

四、对比

构造主函数:

if __name__ == "__main__":
    if(len(sys.argv)) < 2:
        print("Usage:{} <salary>".format(sys.argv[0]))
        sys.exit(1)
    
    salary = float(sys.argv[1])
    old_tax = old_tax(salary)
    new_tax = new_tax(salary)
    print("old tax:{}, new tax:{}".format(old_tax, new_tax))

将代码保存为 tax.py 文件后,利用 cmd 执行可得到:

C:\>python tax.py
Usage:tax.py <salary>
    
C:\>python tax.py 15000
old tax:1870.0, new tax:790.0

C:\>python tax.py 95000
old tax:27670.0, new tax:25340.0

五、其他解释

  • bisect.bisect_left(L, x) 函数查找返回 x 在 L 中应该插入的位置
  • sys.argv 实现外部传递参数,其中 sys.argv[0] 是脚本的名字,1,2,3... 则为传递的参数
  • sys.exit(1) 抛出 SystemExit 异常,如果没有被捕获,Python解释器将会退出,如果有捕获异常的代码存在,那么代码还会继续执行。0为正常退出,其他数值(1-127)为不正常,可抛异常事件供捕获。
  • exit(0) 无错误退出
  • exit(1) 有错误退出