Editor

Convert Temperatures

Advanced · Write the code · from NumPy Broadcasting: Operate on Arrays of Different Shapes

From the lesson: NumPy Broadcasting: Operate on Arrays of Different Shapes

You've already seen that np.array([1,2,3]) 2 multiplies every element by 2. But have you wondered how* NumPy knows to stretch that single number 2 across the entire array? That mechanism is called broadcasting.

Read the lesson

Given an array of temperatures in Celsius celsius = np.array([0, 20, 37, 100]), convert them all to Fahrenheit using the formula F = C * 9/5 + 32. Print the result.

import numpy as np
celsius = np.array([0, 20, 37, 100])
# Convert to Fahrenheit and print

More NumPy Broadcasting exercises

All Python exercises · 50 practice problems · Data Science & Machine Learning quiz