1. What is NumPy?
➤ NumPy is a Python library used for numerical computations using multi-
dimensional arrays and mathematical functions.
2. State any two advantages of NumPy over Python lists.
➤ Faster execution, supports vectorized operations.
3. What is a NumPy array?
➤ A homogeneous, multi-dimensional container for numeric data with fast
operations.
4. How do you install NumPy?
➤ Using pip: pip install numpy or using Anaconda: conda install numpy.
5. Write code to create an array of zeros with shape (2,3).
6. import numpy as np
7. arr = np.zeros((2, 3))
8. What is vectorization in NumPy?
➤ It means applying operations to entire arrays without explicit loops, improving
speed.
9. Define broadcasting in NumPy.
➤ Broadcasting allows NumPy to perform operations on arrays of different shapes by
expanding smaller arrays.
10. What is the use of np.arange()?
➤ It creates arrays with regularly spaced values.
Example: np.arange(1, 10, 2) → [1 3 5 7 9]
11. Write syntax for array slicing.
➤ arr[start:stop:step]
12. What does arr.shape return?
➤ Returns a tuple showing the array's dimensions (rows, columns).
11. Explain the merits and demerits of NumPy.
Merits:
Faster than lists
Supports mathematical operations
Uses less memory
Demerits:
Cannot store mixed data types
Complex for beginners
12. Differentiate between Python lists and NumPy arrays.
| Feature | List | NumPy Array |
|--------|------|--------------|
| Speed | Slower | Faster (vectorized) |
| Data Type | Mixed | Homogeneous |
| Memory | More | Less |
| Operations | Manual loops | Element-wise |
13. Explain indexing and slicing in NumPy with examples.
Indexing: arr[0] returns first element
Slicing: arr[1:4] returns elements from index 1 to 3
Supports 2D: arr[1,2], arr[:,1]
14. What is broadcasting? Explain with an example.
Concept: NumPy stretches smaller arrays to match larger ones
Example:
a = np.array([1, 2, 3])
b = 5
print(a + b) # [6 7 8]
15. Write a Python program to create a 2D NumPy array and print its shape, size,
and datatype.
import numpy as np
arr = np.array([[1,2,3], [4,5,6]])
print(arr.shape, arr.size, arr.dtype)
16. List and explain any four array creation functions in NumPy.
np.array(), np.zeros(), np.ones(), np.arange()
17. Write a program to perform element-wise addition, multiplication, and power.
a = np.array([1,2,3])
b = np.array([4,5,6])
print(a + b, a * b, a ** 2)
18. What are universal functions (ufuncs)? Give examples.
➤ Built-in vectorized functions that apply on arrays.
Examples: np.sqrt(), np.exp(), np.sin()
19. Explain the difference between 1D and 2D indexing.
| Feature | 1D | 2D |
|--------|----|----|
| Syntax | arr[i] | arr[i,j] |
| Access | Linear | Matrix-like |
20. Write code to read and write a NumPy array using .npy format.
np.save('arr.npy', arr)
arr2 = np.load('arr.npy')
21. How is NumPy used in real-life applications?
➤ In data science, ML, image processing, simulations, IoT analytics.
22. Compare NumPy File I/O with normal Python file operations.
➤ NumPy is faster, supports arrays directly, uses .npy, .npz.
23. Write a program to create a 3x3 matrix and print second column using slicing.
arr = np.array([[1,2,3],[4,5,6],[7,8,9]])
print(arr[:,1]) # [2 5 8]
24. Explain the difference between np.linspace() and np.arange().
linspace() gives equally spaced values (even if not integers)
arange() works like range() with integer step
25. What is the use of np.linalg module in NumPy?
➤ Used for linear algebra operations like matrix multiplication, determinant, inverse,
solving equations.
1. What is NumPy?
NumPy is an open-source Python library used for numerical and scientific computing.
It supports fast operations on arrays and matrices.
2. Write any two advantages of NumPy over lists.
1. Faster execution using optimized C code
2. Less memory usage with homogeneous data
3. Write the command to install NumPy using pip.
pip install numpy
4. What is a NumPy array?
A NumPy array is a multidimensional, homogeneous data structure used for efficient
mathematical operations.
5. What does np.zeros((2, 3)) do?
Creates a 2x3 array filled with zeros.
6. Mention any two ufuncs provided by NumPy.
np.sqrt(), np.sin(), np.exp(), np.log() etc.
7. What is broadcasting in NumPy?
Broadcasting allows operations between arrays of different shapes without explicit
replication.
8. Write the difference between np.array() and np.arange().
np.array() creates an array from a list.
np.arange() creates an array from a range of values.
9. How do you find the shape of a NumPy array?
Using .shape attribute.
10. Write the syntax to compute matrix multiplication.
np.dot(A, B) or A @ B
11. What is the function to save an array in .txt format?
np.savetxt("filename.txt", array)
12. What does np.load("file.npy") do?
Loads a binary .npy file into a NumPy array.
13. Name two file formats supported by NumPy file I/O.
.npy, .txt, .csv, .npz
14. Which module in NumPy is used for linear algebra?
numpy.linalg
15. What is the use of np.linspace()?
Creates a linearly spaced array between start and end values.
16. Explain the key features and importance of NumPy in Python programming.
17. List any five merits and demerits of using NumPy arrays.
18. Write a Python program to demonstrate array creation using various NumPy
functions.
np.array(), np.zeros(), np.ones(), np.arange(), np.linspace()
19. Explain indexing and slicing in 1D and 2D NumPy arrays with examples.
20. What are universal functions (ufuncs)? Explain with examples.
np.sqrt(), np.exp(), np.sin(), np.log()
21. Differentiate between Python list operations and NumPy array operations.
22. What is broadcasting in NumPy? Explain broadcasting rules with a program.
23. Write a program using NumPy to perform matrix multiplication, calculate
determinant, and inverse of a matrix.
24. Explain file I/O operations in NumPy with example programs for .txt, .npy,
and .npz formats.
25. Describe the importance of NumPy in data science and machine learning
applications.