Instructions: Try to solve each question on your own. Click the "Show Answer" button to reveal the solution.
Question 1

Create a 1D NumPy array from the list `[10, 20, 30, 40]`.

import numpy as np arr = np.array([10, 20, 30, 40]) print(arr)
Question 2

Create a 2D array from a nested list `[[1, 2], [3, 4]]`.

import numpy as np arr = np.array([[1, 2], [3, 4]]) print(arr)
Question 3

Create a 3D array using nested lists.

import numpy as np arr = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) print(arr)
Question 4

Create an array of floats `[1.5, 2.5, 3.5]`.

import numpy as np arr = np.array([1.5, 2.5, 3.5]) print(arr)
Question 5

Create a 1D array from a tuple `(100, 200, 300)`.

import numpy as np arr = np.array((100, 200, 300)) print(arr)
Question 6

Create an array from `[1, 2, 3]` and force the dtype to `float`.

import numpy as np arr = np.array([1, 2, 3], dtype=float) print(arr)
Question 7

Create an array from `[1.1, 2.2, 3.3]` and force the dtype to `int`.

import numpy as np arr = np.array([1.1, 2.2, 3.3], dtype=int) print(arr)
Question 8

Create a 1D array of strings `['apple', 'banana']`.

import numpy as np arr = np.array(['apple', 'banana']) print(arr)
Question 9

Create an array of boolean values `[True, False, True]`.

import numpy as np arr = np.array([True, False, True]) print(arr)
Question 10

Create a 2D array and explicitly set its `dtype` to `complex`.

import numpy as np arr = np.array([[1, 2], [3, 4]], dtype=complex) print(arr)
Question 11

Create an array of 5 zeros.

import numpy as np arr = np.zeros(5) print(arr)
Question 12

Create a 3x3 array of zeros.

import numpy as np arr = np.zeros((3, 3)) print(arr)
Question 13

Create a 2x4 array of zeros with integer data type.

import numpy as np arr = np.zeros((2, 4), dtype=int) print(arr)
Question 14

Create an array of 10 ones.

import numpy as np arr = np.ones(10) print(arr)
Question 15

Create a 4x4 matrix of ones.

import numpy as np arr = np.ones((4, 4)) print(arr)
Question 16

Create a 3x2 matrix of ones with boolean data type.

import numpy as np arr = np.ones((3, 2), dtype=bool) print(arr)
Question 17

Create an uninitialized (empty) 1D array of size 5.

import numpy as np arr = np.empty(5) print(arr)
Question 18

Create an empty 2x2 matrix.

import numpy as np arr = np.empty((2, 2)) print(arr)
Question 19

Create an empty 3x3 matrix with integer data type.

import numpy as np arr = np.empty((3, 3), dtype=int) print(arr)
Question 20

Why might `np.empty()` be faster than `np.zeros()`?

# np.empty() allocates memory without initializing its values to zero, # making it slightly faster if you plan to overwrite all the elements anyway.
Question 21

Create an array of numbers from 0 to 9 using `arange`.

import numpy as np arr = np.arange(10) print(arr)
Question 22

Create an array of numbers from 10 to 19 using `arange`.

import numpy as np arr = np.arange(10, 20) print(arr)
Question 23

Create an array of even numbers from 2 to 20.

import numpy as np arr = np.arange(2, 21, 2) print(arr)
Question 24

Create an array of odd numbers from 1 to 15.

import numpy as np arr = np.arange(1, 16, 2) print(arr)
Question 25

Create a countdown array from 10 down to 1 using `arange`.

import numpy as np arr = np.arange(10, 0, -1) print(arr)
Question 26

Create an array from 0 to 5 with a step of 0.5.

import numpy as np arr = np.arange(0, 5.5, 0.5) print(arr)
Question 27

Create an array of numbers from -5 to 5 using `arange`.

import numpy as np arr = np.arange(-5, 6) print(arr)
Question 28

Create an array of 10 numbers from 0 to 9, then reshape to 2x5.

import numpy as np arr = np.arange(10).reshape(2, 5) print(arr)
Question 29

Use `arange` to create `[100, 90, 80, 70, 60, 50]`.

import numpy as np arr = np.arange(100, 40, -10) print(arr)
Question 30

Can `arange` be used with `dtype=float` explicitly? Provide an example.

import numpy as np arr = np.arange(1, 5, dtype=float) print(arr)
Question 31

Generate exactly 5 equally spaced values between 0 and 1 using `linspace`.

import numpy as np arr = np.linspace(0, 1, 5) print(arr)
Question 32

Generate 10 equally spaced values between 10 and 20.

import numpy as np arr = np.linspace(10, 20, 10) print(arr)
Question 33

Create an array of 3 values between -1 and 1.

import numpy as np arr = np.linspace(-1, 1, 3) print(arr)
Question 34

Simulate time readings from 0s to 10s with exactly 11 samples.

import numpy as np time = np.linspace(0, 10, 11) print(time)
Question 35

Create 7 evenly spaced power readings between 0 kW and 30 kW.

import numpy as np power = np.linspace(0, 30, 7) print(power)
Question 36

Generate 100 points between 0 and 2*pi for a sine wave plot.

import numpy as np x = np.linspace(0, 2 * np.pi, 100) print(x)
Question 37

Use `linspace` to generate `[10., 20., 30., 40., 50.]`.

import numpy as np arr = np.linspace(10, 50, 5) print(arr)
Question 38

What happens if you omit the third parameter in `linspace(0, 1)`?

# By default, `linspace` generates 50 equally spaced samples. import numpy as np arr = np.linspace(0, 1) print(len(arr)) # 50
Question 39

Generate 5 values between 0 and 1, excluding the endpoint (1).

import numpy as np arr = np.linspace(0, 1, 5, endpoint=False) print(arr)
Question 40

Get both the samples and the step size using `linspace` between 0 and 10 with 5 samples.

import numpy as np arr, step = np.linspace(0, 10, 5, retstep=True) print(arr) print(step)
Question 41

Convert a Python list `[5, 10, 15]` to a NumPy array using `asarray`.

import numpy as np lst = [5, 10, 15] arr = np.asarray(lst) print(arr)
Question 42

Use `asarray` to convert a tuple `(1, 2, 3)` into an array.

import numpy as np tup = (1, 2, 3) arr = np.asarray(tup) print(arr)
Question 43

What happens if you pass an existing ndarray to `asarray()`?

# It returns the original array itself, without copying it. import numpy as np original = np.array([1, 2]) new_arr = np.asarray(original) print(original is new_arr) # True
Question 44

Convert a nested list `[[1,2], [3,4]]` to an array using `asarray`.

import numpy as np lst = [[1, 2], [3, 4]] arr = np.asarray(lst) print(arr)
Question 45

Use `asarray` to convert `[1, 2, 3]` and force `dtype=float`.

import numpy as np arr = np.asarray([1, 2, 3], dtype=float) print(arr)
Question 46

What is the difference between `asarray()` and `asanyarray()`?

# `asarray()` always returns a base ndarray, converting subclasses (like numpy.matrix). # `asanyarray()` preserves subclasses, returning the input as-is if it's already a subclass of ndarray.
Question 47

Use `np.copy()` to make an independent copy of an array `a = np.array([1, 2])`.

import numpy as np a = np.array([1, 2]) b = np.copy(a) b[0] = 99 print(a) # [1 2] print(b) # [99 2]
Question 48

Does the assignment operator `=` create a copy of an array?

# No, it creates a reference. Modifying one modifies the other. import numpy as np a = np.array([1, 2]) b = a b[0] = 99 print(a) # [99 2]
Question 49

Convert a string '123' to an array using `asarray`.

import numpy as np arr = np.asarray('123') print(arr) print(type(arr)) #
Question 50

Create a copy of an array using `np.array(arr, copy=True)`.

import numpy as np a = np.array([5, 10]) b = np.array(a, copy=True) b[0] = 99 print(a) # [5 10]
Question 51

Create a 3x3 identity matrix using `np.identity()`.

import numpy as np arr = np.identity(3) print(arr)
Question 52

Create a 4x4 identity matrix of integers using `np.identity()`.

import numpy as np arr = np.identity(4, dtype=int) print(arr)
Question 53

Create a 3x3 identity matrix using `np.eye()`.

import numpy as np arr = np.eye(3) print(arr)
Question 54

Create a 3x5 matrix with 1s on the main diagonal using `np.eye()`.

import numpy as np arr = np.eye(3, 5) print(arr)
Question 55

Create a 4x4 matrix with 1s on the diagonal *above* the main diagonal.

import numpy as np arr = np.eye(4, k=1) print(arr)
Question 56

Create a 4x4 matrix with 1s on the diagonal *below* the main diagonal.

import numpy as np arr = np.eye(4, k=-1) print(arr)
Question 57

Create a diagonal matrix from the list `[5, 10, 15]` using `np.diag()`.

import numpy as np arr = np.diag([5, 10, 15]) print(arr)
Question 58

Extract the main diagonal from a matrix `[[1,2], [3,4]]`.

import numpy as np mat = np.array([[1, 2], [3, 4]]) diag_vals = np.diag(mat) print(diag_vals) # [1 4]
Question 59

Create a matrix with `[1, 2, 3]` placed on the diagonal *above* the main diagonal.

import numpy as np arr = np.diag([1, 2, 3], k=1) print(arr)
Question 60

Extract the diagonal *below* the main diagonal from a 3x3 matrix of ones.

import numpy as np mat = np.ones((3, 3)) diag_vals = np.diag(mat, k=-1) print(diag_vals) # [1. 1.]
Question 61

Use `fromfunction` to create a 1D array of length 5 where each element is its index multiplied by 3.

import numpy as np arr = np.fromfunction(lambda i: i * 3, (5,), dtype=int) print(arr)
Question 62

Use `fromfunction` to create a 3x3 matrix where each element is the sum of its row and column indices.

import numpy as np arr = np.fromfunction(lambda i, j: i + j, (3, 3), dtype=int) print(arr)
Question 63

Use `fromfunction` to create a 4x4 matrix where each element is 1 if row == column else 0.

import numpy as np arr = np.fromfunction(lambda i, j: (i == j).astype(int), (4, 4)) print(arr)
Question 64

Use `fromfunction` to create a 5x5 checkerboard pattern of 0s and 1s.

import numpy as np arr = np.fromfunction(lambda i, j: (i + j) % 2, (5, 5), dtype=int) print(arr)
Question 65

Use `fromfunction` to create a 2x3 matrix where elements are `row_index * 10 + col_index`.

import numpy as np arr = np.fromfunction(lambda i, j: i * 10 + j, (2, 3), dtype=int) print(arr)
Question 66

Use `fromfunction` to create a 1D array of the first 5 squares (0, 1, 4, 9, 16).

import numpy as np arr = np.fromfunction(lambda i: i**2, (5,), dtype=int) print(arr)
Question 67

What data type does `np.fromfunction` pass to the function by default?

# Float. To pass integer coordinates, specify `dtype=int` in `np.fromfunction`.
Question 68

How do you read a whitespace-separated file `data.txt` into a 1D NumPy array using `fromfile()`?

import numpy as np arr = np.fromfile('data.txt', sep=' ') print(arr)
Question 69

How do you read a comma-separated file `marks.txt`?

import numpy as np arr = np.fromfile('marks.txt', sep=',') print(arr)
Question 70

Read only the first 10 integers from a whitespace-separated file `nums.txt`.

import numpy as np arr = np.fromfile('nums.txt', sep=' ', dtype=int, count=10) print(arr)
Question 71

Assume `data.txt` has 20 numbers. Read them and reshape into a 4x5 matrix.

import numpy as np arr = np.fromfile('data.txt', sep=' ').reshape(4, 5) print(arr)
Question 72

How do you read a binary file `array.bin` containing 32-bit floats?

import numpy as np arr = np.fromfile('array.bin', dtype=np.float32) print(arr)
Question 73

What happens if you use `fromfile()` on a text file without specifying `sep`?

# NumPy will attempt to read it as a binary file, resulting in incorrect/garbage values.
Question 74

Create a 2D matrix of shape 3x3 containing `i * j` using a standard python function and `fromfunction`.

import numpy as np def calc(i, j): return i * j arr = np.fromfunction(calc, (3, 3), dtype=int) print(arr)
Question 75

Save an array `np.arange(5)` to a binary file `out.bin`.

import numpy as np arr = np.arange(5) arr.tofile('out.bin')
Question 76

Generate a single random float between 0 and 1.

import numpy as np x = np.random.rand() print(x)
Question 77

Generate an array of 10 random floats between 0 and 1.

import numpy as np arr = np.random.rand(10) print(arr)
Question 78

Generate a 2x5 matrix of random floats between 0 and 1.

import numpy as np arr = np.random.rand(2, 5) print(arr)
Question 79

Generate a 3D array of random floats with shape (2, 3, 4).

import numpy as np arr = np.random.rand(2, 3, 4) print(arr)
Question 80

Generate an array of 5 random floats between 0 and 100.

import numpy as np arr = np.random.rand(5) * 100 print(arr)
Question 81

Generate an array of 5 random floats between 10 and 50.

import numpy as np arr = 10 + (np.random.rand(5) * 40) print(arr)
Question 82

Generate a 3x3 matrix of random floats between 5 and 10.

import numpy as np arr = 5 + (np.random.rand(3, 3) * 5) print(arr)
Question 83

Simulate 10 student percentages (random floats between 0 and 100).

import numpy as np percentages = np.random.rand(10) * 100 print(percentages)
Question 84

Simulate 5 temperature readings between 20°C and 30°C.

import numpy as np temp = 20 + (np.random.rand(5) * 10) print(temp)
Question 85

Convert an array of 5 random floats (0 to 100) into integers using `astype(int)`.

import numpy as np arr = (np.random.rand(5) * 100).astype(int) print(arr)
Question 86

Generate an array of 10 random boolean values (True or False).

import numpy as np bools = np.random.rand(10) > 0.5 print(bools)
Question 87

Generate an array of 10 random 0s and 1s.

import numpy as np zeros_ones = (np.random.rand(10) > 0.5).astype(int) print(zeros_ones)
Question 88

How do you ensure `np.random.rand(5)` generates the exact same numbers every time?

import numpy as np np.random.seed(42) print(np.random.rand(5))
Question 89

What is the difference in arguments between `np.random.rand()` and `np.random.random()`?

# `rand()` takes individual dimensions as separate arguments: `np.random.rand(3, 4)` # `random()` takes a tuple representing the shape: `np.random.random((3, 4))`
Question 90

Generate a random array of shape (4, 4) and scale the values to be between -1 and 1.

import numpy as np arr = -1 + (np.random.rand(4, 4) * 2) print(arr)
Question 91

Simulate 5 random salaries between ₹20,000 and ₹80,000.

import numpy as np salaries = 20000 + (np.random.rand(5) * 60000) print(salaries)
Question 92

Set the random seed to 100, then generate a 2x2 random matrix.

import numpy as np np.random.seed(100) arr = np.random.rand(2, 2) print(arr)
Question 93

Create a 1D array of 5 random floats, and multiply all elements by 10.

import numpy as np arr = np.random.rand(5) * 10 print(arr)
Question 94

Can `np.random.rand()` generate negative numbers natively without mathematical manipulation?

# No, `rand()` only generates numbers from the uniform distribution [0.0, 1.0).
Question 95

Simulate tossing a coin 10 times (Output: array of 'Heads' or 'Tails').

import numpy as np flips = np.where(np.random.rand(10) > 0.5, 'Heads', 'Tails') print(flips)
Question 96

Generate a 5x2 matrix of random numbers from 0 to 1.

import numpy as np arr = np.random.rand(5, 2) print(arr)
Question 97

Generate 10 random numbers between 0 and 1, and count how many are > 0.8.

import numpy as np nums = np.random.rand(10) count = np.sum(nums > 0.8) print(count)
Question 98

Generate 10 random numbers. If a number is > 0.5, replace it with 1, else 0.

import numpy as np arr = np.random.rand(10) arr_binary = (arr > 0.5).astype(int) print(arr_binary)
Question 99

Generate a 2x2 random array. Try generating another 2x2. Are they same? How to make them same?

# They are different. To make them same, use `np.random.seed(x)` before each generation. import numpy as np np.random.seed(1) print(np.random.rand(2,2)) np.random.seed(1) print(np.random.rand(2,2))
Question 100

Generate a random probability array of length 5 (values sum to 1).

import numpy as np probs = np.random.rand(5) normalized_probs = probs / np.sum(probs) print(normalized_probs)