# 个税起征点上调至5000，用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 = [.03, .1, .2, .25, .30, .35, .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

def new_tax(salary):
if salary < 5000:
return 0.0

above = salary - 5000

ranges = [0, 1500, 4500, 9000, 35000, 55000, 80000]
rate = [.03, .1, .2, .25, .30, .35, .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

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: {}, new: {}".format(old_tax, new_tax))

python3 tax.py 5000
old: 45.0, new: 0.0

python3 tax.py 10000
old: 745.0, new: 445.0

python3 tax.py 20000
old: 3120.0, new: 2745.0

python3 tax.py 50000
old: 11195.0, new: 10745.0

python3 tax.py 100000
old: 29920.0, new: 29245.0

Python爱好者

+ 订阅