Question: I'm using the Python language and the Pycharm program. Here is the question: Part II: Stock Portfolio (2 points) In addition to lists and dictionaries,

I'm using the Python language and the Pycharm program.

Here is the question:

Part II: Stock Portfolio (2 points)

In addition to lists and dictionaries, Python offers another type of collection called a tuple (pronounced tupple or toople). A tuple is a container for a fixed, immutable collection of values. This means that elements inside of a tuple cannot be changed or deleted. It also means that new items cannot be added to an existing tuple. In Python, a tuple is denoted using parentheses. Here are some examples of tuples:

sizes = (small, medium, large, extra large) ages = (22, 29, 25, 30, 19, 24) info = (Jane Smith, 21, 3.91) 

Note that we can mix data types within a single tuple. To access the elements of a tuple we can use bracket notation. For instance, the following code would print the string large on the screen:

print(sizes[2]) 

Imagine we had a portfolio of stocks we purchased and then later sold. Here is an example:

Symbol

# of Shares

Purchase Price

Selling Price

CAT

25

43.50

67.75

MSFT

100

87.65

82.50

IBM

35

90.15

92.97

APPL

200

175.00

255.65

We could represent these four stock transactions using a list of tuples:

portfolio = [ (CAT, 25, 43.50, 67.75), (MSFT, 100, 87.65, 82.50), 
(IBM, 35, 90.15, 92.97), (APPL, 200, 175.00, 255.65) ] 

Write a function stocks value() that takes a list of tuples structured in this manner and computes the total profit or loss that was realized from buying and selling the stocks. The profit or loss realized for a single stock is computed using the formula:

profit or loss = # of shares (selling price purchase price) Note that the list could have as few as one tuple or as many as dozens of tuples in it. The example above has four

tuples, but this is just one example. For the example above, the total profit or loss would be calculated as follows:

25 (67.75 43.50) + 100 (82.50 87.65) + 35 (92.97 90.15) + 200 (255.65 175.00)

This mathematical expression is equal to 16319.95. So, the function would return 16319.95 for this input.

Here is another example. If the argument to stocks value() were the following list:

portfolio = [ (GOOG, 250, 325.86, 278.90), (ABC, 115, 25.60, 29.85) ] 

then the function would return 11251.25.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!