I am only returning to programming after a 20 year gap. I thought Python looked pretty simple and powerful, so I did an online course and some reading.
Now I am looking through a few simple projects to familiarize myself with the language. One of the problems is that I get a head from object-oriented programming, which was not the last time I wrote a program.
My first project was to read in a data file containing information about a stock portfolio, do some calculations for each and print a report. It works for me.
So, now I look at something more advanced, reading the data and saving it, and then using the data to provide answers to interactive questions. My question is how to store data so that it can be easily retrieved.
My first thought was to make a list of lists, for example
companies = [ ['AMP', 1000, 2.50], ['ANZ', 2000, 17.00], ['BHP', 500, 54.30] ]
They can be easily accessed in cycles, but the access methods are not entirely friendly - numbers as indices instead of names:
companyqty = companies[1][1]
Or for loops:
for company in companies: if company[0] == 'BHP': companyqty = company[1]
Then I thought of a dictionary whose value is a list:
companies = {'AMP':[1000, 2.50], 'ANZ':[2000, 17.00], 'BHP':[500, 54.30] } companyqty = companies['BHP'][0]
This provides immediate access to any given company, but is still fixated on numerical indices.
So, I am wondering how to structure this in an object-oriented manner so as to be able to keep a list of companies and all the data associated with it, and be able to access the values conveniently. All my ideas so far only look like lists or dictionaries, as indicated above.
Or is it a problem that is not suitable for an object-oriented approach?
thanks