Data Science · Level 1 Arrays

NumPy Arrays

Turn a Python list into a NumPy array and convert a whole list of temperatures in one line, without a loop.

  • np.array() from a list
  • Vectorized arithmetic
  • list * 2 versus array * 2

A NumPy array applies arithmetic to every element at once, no loop needed. Trap: on a plain list, * 2 repeats the list instead of doubling the numbers.

import numpy as np

prices = [10, 20, 30]
prices * 2            # [10, 20, 30, 10, 20, 30]
np.array(prices) * 2  # array([20, 40, 60])

Write to_fahrenheit(celsius) that takes a list of temperatures and returns a NumPy array in Fahrenheit (c * 9 / 5 + 32), for example to_fahrenheit([0, 100]) gives array([32., 212.]).

solution.py
Test Suites Pending
Output