Percent change between two lists

homework

#1


#2

This code is left exposed so it can be indexed. Be wary about using it in your assignment or you could find yourself in trouble with your school.

def pct_change(past, present):
  return present / past * 100 - 100
top_dogs = [
  "Verizon", "IBM", "Pfizer", "Exxon", "Chevron",
  "Merck", "CocaCola", "Cisco", "Proctor&Gamble", "GE"  ]
end_2017 = [
  52.93, 153.42, 36.22, 93.64, 125.19,
  56.27, 45.88, 38.30, 91.88, 17.45  ]
latest_2018 = [
  54.90, 129.10, 44.50, 81.97, 118.14,
  72.35, 46.33, 45.34, 87.30, 12.56  ]
combined = zip(end_2017, latest_2018)
combined = zip(top_dogs, combined)
earnings = []

for x in combined:
  a, b = x
  c, d = b
  p = pct_change(c, d)
  el = 1000 * p / 100
  earnings.append(el)
  print ("{:<16s} {:>6.2f} {:>6.2f} {:>6.2f} {:>7.2f}".format(a, c, d, p, el))

print ("{}\nEarnings:           {}\nPercent Change YoY: {:.2f}".format(\
  '-'*45, sum(earnings), pct_change(10000, sum(earnings) + 10000)))
====== RESTART: D:/cc/discuss/users/ctgriff/top_dogs_percent_change.py ======
Verizon           52.93  54.90   3.72   30.00
IBM              153.42 129.10 -15.85 -150.00
Pfizer            36.22  44.50  22.86  220.00
Exxon             93.64  81.97 -12.46 -120.00
Chevron          125.19 118.14  -5.63  -50.00
Merck             56.27  72.35  28.58  280.00
CocaCola          45.88  46.33   0.98    0.00
Cisco             38.30  45.34  18.38  180.00
Proctor&Gamble    91.88  87.30  -4.98  -40.00
GE                17.45  12.56 -28.02 -280.00
---------------------------------------------
Earnings:           70.0
Percent Change YoY: 0.70
>>>