以前起征点是3500,2018年10月1日起起征点正式修改为5000。
下面我们用 Python
来分别计算新旧个人所得税分别为多少?
import sysimport 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.0old_tax(15600) # 2020.0old_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.0old_tax_2(15600) # 2020.0old_tax_2(95000) # 27670.0
三、新个人所得税import sysimport 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 taxnew_tax(6500) # 45.0new_tax(10500) # 340.0new_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 taxnew_tax_2(6500) # 45.0new_tax_2(10500) # 340.0new_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.pyUsage:tax.py <salary> C:\>python tax.py 15000old tax:1870.0, new tax:790.0C:\>python tax.py 95000old 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)
有错误退出 总结 以上是内存溢出为你收集整理的Python学习笔记:个税起征点上调至5000,算一算少交多少税?全部内容,希望文章能够帮你解决Python学习笔记:个税起征点上调至5000,算一算少交多少税?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)