NumPy helps us work with numbers and lists of numbers easily and fast. It makes math with many numbers simple.
What is NumPy
Introduction
Syntax
NumPy
import numpy as np # Create an array arr = np.array([1, 2, 3]) # Do math sum_arr = np.sum(arr)
We usually import NumPy as np to keep code short.
NumPy uses array to store numbers efficiently.
Examples
NumPy
import numpy as np arr = np.array([10, 20, 30]) print(arr)
NumPy
import numpy as np arr = np.array([[1, 2], [3, 4]]) print(arr)
NumPy
import numpy as np arr = np.array([1, 2, 3]) sum_val = np.sum(arr) print(sum_val)
Sample Program
This program shows how to create a NumPy array from a list, then find the average and sum of the numbers.
NumPy
import numpy as np # Create a list of numbers numbers = [5, 10, 15, 20] # Convert list to NumPy array arr = np.array(numbers) # Calculate mean (average) mean_val = np.mean(arr) # Calculate sum sum_val = np.sum(arr) print(f"Numbers: {arr}") print(f"Mean: {mean_val}") print(f"Sum: {sum_val}")
Important Notes
NumPy arrays are faster and use less memory than regular Python lists for numbers.
NumPy has many built-in math functions like sum, mean, max, and min.
NumPy is the base for many other data science tools.
Summary
NumPy helps handle and do math on many numbers easily.
It uses arrays to store numbers efficiently.
It is a key tool for data science and scientific computing.
Practice
1. What is NumPy mainly used for in data science?
easy
Solution
Step 1: Understand NumPy's purpose
NumPy is designed to handle large arrays of numbers and perform math operations quickly.Step 2: Compare options with NumPy's role
Creating web pages and styling them, managing databases and SQL queries, and writing scripts to automate emails are not NumPy's focus.Final Answer:
Handling and performing math on large sets of numbers efficiently -> Option AQuick Check:
NumPy = Efficient number handling [OK]
Hint: NumPy is about numbers and math, not web or databases [OK]
Common Mistakes:
- Confusing NumPy with web or database tools
- Thinking NumPy is for text processing
- Assuming NumPy handles emails or automation
2. Which of the following is the correct way to import NumPy in Python?
easy
Solution
Step 1: Recall standard NumPy import syntax
The common and correct way is 'import numpy as np' to use 'np' as shorthand.Step 2: Check other options for syntax errors
'import numpy as numpy' repeats 'numpy' without proper aliasing; 'import np as numpy' and 'import numpy from np' have wrong order or keywords.Final Answer:
import numpy as np -> Option BQuick Check:
Standard import = import numpy as np [OK]
Hint: Remember: 'import numpy as np' is the standard [OK]
Common Mistakes:
- Using wrong keywords like 'from' in import
- Swapping alias and module names
- Not using 'as' for aliasing
3. What will be the output of this code?
import numpy as np arr = np.array([1, 2, 3]) print(arr * 2)
medium
Solution
Step 1: Understand NumPy array multiplication
Multiplying a NumPy array by 2 multiplies each element by 2.Step 2: Calculate element-wise multiplication
Elements 1, 2, 3 become 2, 4, 6 respectively.Final Answer:
[2 4 6] -> Option DQuick Check:
Array * 2 doubles each element [OK]
Hint: NumPy arrays multiply element-wise, not concatenate [OK]
Common Mistakes:
- Thinking * duplicates the array
- Expecting addition instead of multiplication
- Confusing with Python lists behavior
4. Find the error in this code:
import numpy as np arr = np.array([1, 2, 3]) print(arr + '2')
medium
Solution
Step 1: Analyze the operation arr + '2'
Adding a string to an integer array causes a type error in NumPy.Step 2: Confirm error type
NumPy does not support adding strings to numeric arrays directly.Final Answer:
Cannot add string '2' to a NumPy array of integers -> Option CQuick Check:
Type mismatch causes error [OK]
Hint: NumPy arrays need matching data types for operations [OK]
Common Mistakes:
- Assuming automatic type conversion
- Ignoring data type mismatch errors
- Thinking string addition works like number addition
5. You have a list of daily temperatures:
[20, 22, 19, 24, 21]. Using NumPy, how can you quickly find the average temperature?hard
Solution
Step 1: Identify NumPy function for average
NumPy provides np.mean() to calculate the average of array elements.Step 2: Compare with other options
len() counts elements; sum() / 5 calculates average manually without NumPy's dedicated function; np.sum() sums values; np.mean() finds the average quickly.Final Answer:
np.mean([20, 22, 19, 24, 21]) -> Option AQuick Check:
Use np.mean() for average [OK]
Hint: Use np.mean() to get average quickly [OK]
Common Mistakes:
- Using sum without dividing for average
- Confusing sum and mean functions
- Not using NumPy functions for array operations
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
