Python Exercises
Python Exercises
Submit your Python file (Jupyter notebook) with your answers to the below questions.
In the following questions, we assume that “1d” means 1-dimension and “2d” means 2-
dimensions.
You may use a separator (using the markdown tool) between each question in your Jupyter
Notebook. Below is an example:
Use ### to emphasize the question number then execute the cell.
Good luck!
Question 1
- Create a python list with integers from 1 to 10
- Write a code that will display a new list with the square of each number inside the original
list
Question 2
- Create a 1d numpy array of size 100 (named “A”) with random numbers generated from
a standard normal distribution
- Write a code that will return a numpy array (named “B”) with values 1 (respectively -1) if
the corresponding number of the array A is positive (negative)
Question 3
Write a program that iterates the integers from 1 to 100. If the number is divisible by 3 print the
string "Fizz". If the number is divisible by 5 print the string "Buzz". If more than one condition
applies, print the associated strings separated by a space on the same line, in ascending order or
divisors. If none of the conditions apply print the number.
Question 4
Add the following conditions to the program of Question 3. If the number is divisible by 6 print
the string "Six". If the number is divisible by 11 print the string "Foo".
Question 5
- Write a Python code that extracts data from the associated excel workbook (spreadsheet
Q5). Name your DataFrame df.
- Use the column Date as your index
- Calculate the monthly returns of each assets a, b, and c
- Calculate the annualized volatility of assets a, b, and c
Question 6
Consider four assets. Their expected returns are equal to 5%, 6%, 8% and 6%. Their volatilities are
equal to 15%, 20%, 25% and 20%. The correlation matrix of asset returns is given by the following
matrix:
Assuming an equally weighted portfolio between the four assets, write a code that calculates the
volatility and the expected return of the portfolio.
Question 7
Write a python function that takes a positive integer as parameter and that returns True if the
integer is a perfect square or False if not.
Question 8
We call a Fibonacci sequence, a sequence of numbers such that 𝐹𝑛 = 𝐹𝑛−1 + 𝐹𝑛−2.
For example, the first numbers of a Fibonacci sequence are 0, 1, 1 (0+1), 2 (1+1), 3 (1+2), 5 (2+3),
8 (5+3) and so on. Note that by convention the first two numbers of the sequence are 0 and 1.
Write a python function that takes a positive integer N as argument and that returns the N-th
Fibonacci number in the sequence. Test your function with few values.
Question 9
What is the difference between .loc and .iloc in pandas ? Illustrate your answer with an example
by creating a pandas dataframe of your choice.
Question 10
- Write a Python code that extracts data from the associated excel workbook (spreadsheet
Q10). Name your DataFrame df.
- Write a code that will calculate the Sharpe Ratio of the following portfolios:
✓ Equally-weighted between Assets a, b and c
✓ Equally-weighted between Assets a and b
✓ Equally-weighted between Assets b and c
Question 11
Write a python function that takes as inputs:
The function should return the NPV of the corresponding list of cash flows. Use python lists and
for-loops and test your function with different lists of cash flows of your choice.
Question 12
Write the same function as question 11 but this time use NumPy and its vectorization.
Question 13
- Create a 2d NumPy array named arr with random values following a standard normal
distribution, with 1000 rows and 5 columns
- Write a Python code that will compute the mean and the standard deviation of each
column of the array
Question 14
- Write a Python code that extracts data from the associated excel workbook (spreadsheet
Q14). Name your DataFrame df.
- Write a code that adds a column to df (column name will be “portfolio”) with the equal
weighted portfolio between assets a, b and c.
- Create a line chart with the cumulative performance of assets a, b, c and “portfolio”
Question 15
- Create a NumPy array with integers from 0 to 32
- Reshape the array with 4 rows and 8 columns. Convert it into a pandas DataFrame named
df.
Challenge
Brainteaser often asked in interview.
Monty Hall Problem: Suppose you're on a game show, and you're given the choice of three doors:
Behind one door is a car; behind the others, goats. You pick a door, say No. 1, and the host, who
knows what's behind the doors, opens another door, say No. 3, which has a goat. He then says to
you, "Do you want to pick door No. 2?"