numpy Exercises
These exercises test your understanding of the NumPy concepts covered in the introduction notebook.
Exercise 1: Import and Basic Array Creation
Task: Import NumPy with the standard import command and create a NumPy array from the list [2, 4, 6, 8, 10, 12]. Print the array, its data type, and its shape.
Exercise 2: Array Indexing and Slicing
Task: Using the array you created in Exercise 1:
- Print the first element
- Print the last element
- Print elements from index 2 to 4 (inclusive of 2, exclusive of 5)
- Print every second element
Exercise 3: Element-wise Operations
Task: Create a NumPy array arr = np.array([1, 4, 9, 16, 25]). Perform the following operations and print the results:
- Multiply each element by 3
- Add 10 to each element
- Calculate the square root of each element
- Apply the sine function to each element
Exercise 4: Array Operations with Two Arrays
Task: Create two NumPy arrays:
arr1 = np.array([1, 2, 3, 4, 5])arr2 = np.array([10, 20, 30, 40, 50])
Perform element-wise operations and print the results:
- Add the two arrays
- Subtract arr1 from arr2
- Multiply the two arrays
- Divide arr2 by arr1
Exercise 5: Shape Mismatch Error
Task: Create two arrays with different lengths:
arr_a = np.array([1, 2, 3, 4])arr_b = np.array([10, 20])
Try to add these arrays together. What happens? Write the code and explain the error in a comment.
Exercise 6: Reductive Operations
Task: Create a NumPy array data = np.array([12, 8, 15, 3, 7, 20, 11, 9]). Calculate and print:
- The sum of all elements
- The mean (average) of all elements
- The maximum value
- The minimum value
- The standard deviation
Exercise 7: Array Manipulation
Task: Create a NumPy array numbers = np.array([5, 2, 8, 1, 9, 3]). Perform the following operations:
- Sort the array and print the result
- Calculate the cumulative sum and print the result
- Create a new array with duplicates:
with_duplicates = np.array([5, 2, 8, 1, 9, 3, 5, 2, 1])and find the unique elements
Exercise 8: List vs NumPy Comparison
Task: Compare the flexibility of lists vs NumPy arrays:
- Create a Python list containing mixed data types:
mixed_list = [1, 'hello', 3.14, True] - Try to create a NumPy array from this list. What happens to the data types?
- Create a list of numbers:
num_list = [1, 2, 3, 4, 5] - Try to multiply the entire list by 2 using
num_list * 2. What happens? - Create a NumPy array from the same numbers and multiply by 2. Compare the results.
Exercise 9: Performance Comparison
Task: Compare the performance of list comprehension vs NumPy operations:
- Import the
timemodule - Create a range of 50,000 numbers using numpy
np.arange() - Time how long it takes to square each number using a list comprehension, i.e. the syntax
[x**2 for x in myarray] - Time how long it takes to square each number using NumPy operations
- Calculate and print how many times faster the NumPy operation is
Exercise 10: Complex Operations
Task: Create a NumPy array representing angles in degrees: angles_deg = np.array([0, 30, 45, 60, 90, 120, 180]) 1. Convert these angles to radians (hint: multiply by π/180, use np.pi) 2. Calculate the sine and cosine of each angle 3. Verify that sin²(x) + cos²(x) = 1 for each angle (use np.sin() and np.cos()) 4. Print all results
Challenge 1: Temperature Conversion
Task: You have temperature readings in Celsius: celsius_temps = np.array([0, 10, 20, 25, 30, 35, 40])
- Convert all temperatures to Fahrenheit using the formula: F = (C × 9/5) + 32
- Convert all temperatures to Kelvin using the formula: K = C + 273.15
- Calculate the temperature range (max - min) for each scale
Challenge 2: Kinetic energy
Task Given arrays of mass (kg) and velocity (m/s) for several objects:
# copy and paste this code below
masses = np.array([2.0, 1.5, 3.0, 0.5]) # in kilograms
velocities = np.array([10.0, 20.0, 15.0, 5.0]) # in meters per second- Calculate the kinetic energy \(K\) for each object using the formula:
\[K = \dfrac{1}{2}m v^2\]
- Calculate the total kinetic energy of the system (sum of all objects)
Challenge 3: Temperature Analysis
Task: Given a NumPy array of daily temperatures for 30 days:
temperatures = np.array([23.5, 24.0, 21.2, 25.6, 22.8, 23.9, 26.1, 27.3, 21.7, 22.5,
24.6, 25.8, 20.9, 22.3, 24.7, 26.0, 27.5, 21.0, 22.9, 24.8,
25.9, 27.6, 20.8, 22.7, 24.5, 26.2, 27.7, 20.5, 22.6, 24.9])Perform the following tasks:
- Find the top 3 hottest days and their temperatures.
- Calculate the day-to-day percentage change in temperature.