Get startedGet started for free

Arrays and NumPy

NumPy is a scientific computing package in Python that helps you to work with arrays. Let's use array operations to calculate price to earning ratios of the S&P 100 stocks.

The S&P 100 data is available as the lists: prices (stock prices per share) and earnings (earnings per share).

This exercise is part of the course

Introduction to Python for Finance

View Course

Exercise instructions

  • Import the numpy as np.
  • Convert the prices and earnings lists to arrays, prices_array and earnings_array, respectively.
  • Calculate the price to earnings ratio as pe.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Import numpy as np
import ____ as ____

# Convert lists to arrays
prices_array = ____(prices)
earnings_array = ____(earnings)

# Calculate P/E ratio 
pe = ____
print(pe)
Edit and Run Code