How to calculate profit and loss from investing in a single asset and multiple assets? How to calculate investment returns from profit and loss? How to write a reusable Python module on different operating systems?
Content
Write Reusable Python Functions by Putting Them in A Python Module
Make a Python Module on Windows






Choices of Python Editors
Notepad++
Download from https://notepad-plus-plus.org/

Visual Studio Code
Download from https://code.visualstudio.com/

Sublime Text
Download from https://www.sublimetext.com/

Quizzes and Answers
Quiz 1: In the examples in this chapter, we saw investing $1,000 in five assets AMZN, TSLA, DIS, MCD, and NKE between 2020 and 2021 delivers $2,628.21 in profit, which is less than the profit of investing in just two assets AMZN and TSLA, $6,018.74. Explain why.
Among the five stocks, the price of TSLA increased the most. When splitting $1000 equally in AMZN and TSLA, $500 was invested in TSLA, and when spitting $1000 equally among AMZN, TSLA, DIS, MCD, and NKE, only $200 was invested in TSLA. Since most of the profit of both investments comes from TSLA, less investment in TSLA at the beginning would result in less profit. As a result, even though more assets are added to the investment, the total profit is less because the allocation to TSLA is less. Having said that, TSLA is not always a better investment than other asset because it depends on which investment period is being looked at, hence investing more in TSLA does not always improves total profit.
Quiz 2: In function “equal_split_assets_invest_PnL”, the money is split equally among all assets. To make the function more general, write another function that allows splitting money differently for different assets, and then calculate the total profit or loss of the investments.
The link to function definition can be found in Github: https://github.com/qtvest/kidsinvest1/blob/main/Chapter-5-Simple-Investments/quiz-2.py.

To test the function, put the function definition to MyModule.py, and the type “reload(MyModule)” in your Python shell to update MyModule in the Python shell. First we can test the function by using equal splits and check if the results are the same as the output from another function “equal_split_assets_invest_PnL“.

