Q1. What is NumPy and why is it used?
NumPy is a fundamental library for numerical computing in Python. It provides ndarray, a fast, efficient multidimensional array object, and many vectorized operations. It's the foundation for many data science and machine learning libraries (pandas, scikit-learn).
Q2. What is the difference between a NumPy array and a Python list?
NumPy arrays are homogeneous (all elements same type) and store data in contiguous memory, making operations faster. They support vectorized operations (element-wise without loops). Lists can hold different types and are slower for numerical computations.
Q3. How do you create a NumPy array?
Use np.array() from a list, or functions like np.zeros(), np.ones(), np.arange(), np.linspace(), np.random.rand(). Example:
import numpy as np
arr = np.array([1,2,3])
zeros = np.zeros((3,3))
range_arr = np.arange(0, 10, 2)Q4. What are broadcasting and vectorization?
Broadcasting allows operations on arrays of different shapes without explicitly replicating data. Vectorization refers to applying operations to whole arrays element-wise without explicit loops. Both lead to concise, efficient code. Example:
arr = np.array([1,2,3])
arr + 5 # broadcasting scalarQ5. How do you index and slice NumPy arrays?
Use similar syntax as lists, but with multidimensional indexing. Example:
arr = np.arange(12).reshape(3,4)
print(arr[1,2]) # element at row1,col2
print(arr[:,1]) # all rows, second column
print(arr[1:3, 1:3]) # slice Boolean indexing is also supported.