Question
I'm using python. Part II: Stock Portfolio (2 points) In addition to lists and dictionaries, Python offers another type of collection called a tuple (pronounced
I'm using python.
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. Example 1; CAT/ 25 /43.50 /67.75. example 2: MSFT/ 100/ 87.65/ 82.50/. example 3: IBM/ 35/ 90.15/ 92.97/. ex 4: 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. Both of these examples are included in the bare bones file to help you test your code.
def stocks_value(portfolio):
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started