Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Formula
#1
Dear Collaborates,

Firs of all thank you in advance for replying to my inquiry:

Problem: I have written a python program that works fine, but I would like to reduce the amount of code and use a looping mechanism. I'm new to python so please excuse my inexperience.
from tabulate import tabulate
mydata = [("GOOGL", 125, 772.88, 941.53),
          ("MSFT", 85, 56.60, 73.04),
          ("RDS-A", 400, 49.58, 55.74),
          ("AIG", 235, 54.21, 65.27),
          ("FB", 150, 124.31, 172.45)]


headers = ["Stock Symbol", "No Shares", "Purchase Price", "Current Value"]
headers1 = ["Stock Symbol", "No Shares", "Earnings/Loss"]


x =  (mydata[0][2])
y =  (mydata[0][3])
s =(mydata[0][1])
a = (y-x) * s

x1 =  (mydata[1][2])
y1 =  (mydata[1][3])
s1 =(mydata[1][1])
b = (y1-x1) * s1


x2 =  (mydata[2][2])
y2 =  (mydata[2][3])
s2 =(mydata[2][1])
c = (y2-x2) * s2

x3 =  (mydata[3][2])
y3 =  (mydata[3][3])
s3 =(mydata[3][1])
d = (y3-x3) * s3

x4 =  (mydata[4][2])
y4 =  (mydata[4][3])
s4 =(mydata[4][1])
e = (y4-x4) * s4

results = [(mydata[0][0], mydata[0][1],a),

(mydata[1][0], mydata[1][1],b),

(mydata[2][0], mydata[2][1],c),

(mydata[3][0], mydata[3][1],d),

(mydata[4][0], mydata[4][1],e)]


print("\nStock Ownership")

print("..............................................................")

print(tabulate(mydata, headers=headers, tablefmt="grid"))


print("\nStock Earnings/Losses ")
print("..............................................................")

print(tabulate(results, headers=headers1, tablefmt="grid"))
Reply
#2
mydata = [("GOOGL", 125, 772.88, 941.53),
          ("MSFT", 85, 56.60, 73.04),
          ("RDS-A", 400, 49.58, 55.74),
          ("AIG", 235, 54.21, 65.27),
          ("FB", 150, 124.31, 172.45)]
 
 
headers = ["Stock Symbol", "No Shares", "Purchase Price", "Current Value"]
headers1 = ["Stock Symbol", "No Shares", "Earnings/Loss"]

separator1 = '+----------------+-------------+------------------+-----------------+'
separator2 = '+================+=============+==================+=================+'
separator3 = '+----------------+-------------+-----------------+'
separator4 = '+================+=============+=================+'

print("\nStock Ownership")
print(separator1)
print('| {:14} | {:11} | {:16} | {:15} |'.format(*headers))
print(separator2)
for element in mydata:
    print('| {:14} | {:11} | {:16} | {:15} |'.format(*element))
    print(separator1)

print("\nStock Earnings/Losses ")
print(separator3)
print('| {:14} | {:11} | {:15} |'.format(*headers1))
print(separator4)
for element in mydata:
    s = (element[3] - element[2]) * element[1]
    print('| {:14} | {:11} | ${:14,.2f} |'.format(element[0], element[1], s))
print(separator3)
output:
Output:
Stock Ownership +----------------+-------------+------------------+-----------------+ | Stock Symbol | No Shares | Purchase Price | Current Value | +================+=============+==================+=================+ | GOOGL | 125 | 772.88 | 941.53 | +----------------+-------------+------------------+-----------------+ | MSFT | 85 | 56.6 | 73.04 | +----------------+-------------+------------------+-----------------+ | RDS-A | 400 | 49.58 | 55.74 | +----------------+-------------+------------------+-----------------+ | AIG | 235 | 54.21 | 65.27 | +----------------+-------------+------------------+-----------------+ | FB | 150 | 124.31 | 172.45 | +----------------+-------------+------------------+-----------------+ Stock Earnings/Losses +----------------+-------------+-----------------+ | Stock Symbol | No Shares | Earnings/Loss | +================+=============+=================+ | GOOGL | 125 | $ 21,081.25 | | MSFT | 85 | $ 1,397.40 | | RDS-A | 400 | $ 2,464.00 | | AIG | 235 | $ 2,599.10 | | FB | 150 | $ 7,221.00 | +----------------+-------------+-----------------+
Reply
#3
I really appreciate it!!! Thank you so much for taking time out of your day to respond. Have a wonderful rest of your week.
Reply
#4
this make me think it would be nice to have a "table" module. it could take all the contents at one time in a list of lists or work an abstract data type letting you put data in cells one at a time. one version would output text like Larz60+'s code does. another could output or return HTML code. yet another could display the table graphically for a GUI app.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
Very good idea Skaperen. The other part of your idea could be to allow the program to receive a csv file upload, and then do the calculation. Let me know if you come up with a concept. Thank you
Reply
#6
(Apr-17-2019, 04:03 AM)Skaperen Wrote: another could output or return HTML code

tabulate has this feature, from their help:

Quote:"html" produces HTML markup:

>>> print(tabulate([["strings", "numbers"], ["spam", 41.9999], ["eggs", "451.0"]],
... headers="firstrow", tablefmt="html"))
<table>
<thead>
<tr><th>strings </th><th style="text-align: right;"> numbers</th></tr>
</thead>
<tbody>
<tr><td>spam </td><td style="text-align: right;"> 41.9999</td></tr>
<tr><td>eggs </td><td style="text-align: right;"> 451 </td></tr>
</tbody>
</table>
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python “Formula” Package: How do I parse Excel formula with a range of cells? JaneTan 1 2,676 Jul-12-2021, 11:09 AM
Last Post: jefsummers
  How do I read in a Formula in Excel and convert it to do the computation in Python? JaneTan 2 2,638 Jul-07-2021, 02:06 PM
Last Post: Marbelous
  Perfect Number formula in Python Question an Mersenne Numbers Pleiades 5 6,049 May-16-2018, 04:56 PM
Last Post: Pleiades

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020