Your First Array
Create your first NumPy array from a Python list, print it, inspect its type, index and slice it, change values, and convert it back to a list.
Time to create a real array: Rohit Sharma's scores from his first 5 matches. Everything you will do in NumPy starts with an array like this.
Definition
np.array(object)is the function that creates an ndarray from existing Python data — usually a list (or a list of lists). It copies the values into a new contiguous block of memory and automatically picks a singledtypethat fits all of them.
- object — the data to convert: a list, tuple, nested list, or another array.
- copies — changing the array later does not change the original list (and vice versa).
- automatically picks a dtype — whole numbers →
int64, any decimal →float64,True/False→bool.
Syntax
python
np.array(object, dtype=None)| Parameter | Meaning | Default |
|---|---|---|
object | List / tuple / nested list / array to convert | required |
dtype | Force a data type, e.g. int, float, "int32" | None (NumPy infers it) |
(Full details of np.array are in the Creating Arrays chapter.)
Example — create and inspect
python
import numpy as np
rohit_runs = np.array([56, 0, 74, 88, 32])
print(rohit_runs)
print(type(rohit_runs))
print(rohit_runs.dtype)
print(rohit_runs.shape)
print(repr(rohit_runs))text
[56 0 74 88 32]
<class 'numpy.ndarray'>
int64
(5,)
array([56, 0, 74, 88, 32])How it works:
print(arr)shows values separated by spaces, not commas — that is how you recognise a NumPy array in printed output. Numbers are right-aligned (0) so columns line up.type(...)confirms it is anumpy.ndarray..dtypeisint64— 64-bit integers, because all values are whole numbers..shapeis(5,)— a 1-D array with 5 elements. The trailing comma means it is a one-element tuple.repr(arr)(what a Jupyter notebook shows when you type justrohit_runs) printsarray([...])with commas.
Example — use it: index, slice, maths, change values
python
import numpy as np
rohit_runs = np.array([56, 0, 74, 88, 32])
print(rohit_runs[0])
print(rohit_runs[-1])
print(rohit_runs[1:4])
print(rohit_runs + 5)
print(rohit_runs.sum(), rohit_runs.max(), rohit_runs.min())
print(len(rohit_runs))
rohit_runs[1] = 12
print(rohit_runs)
rohit_runs[2] = 99.7
print(rohit_runs)
print(rohit_runs.tolist())text
56
32
[ 0 74 88]
[61 5 79 93 37]
250 88 0
5
[56 12 74 88 32]
[56 12 99 88 32]
[56, 12, 99, 88, 32]How it works:
- Indexing (
[0],[-1]) and slicing ([1:4], stop excluded) work exactly like Python lists. rohit_runs + 5adds 5 to every score and returns a new array; the original is unchanged..sum(),.max(),.min()are methods built into every array.len()still works and gives the length of the first dimension.- Arrays are mutable:
rohit_runs[1] = 12changes the value in place. - Assigning
99.7into an integer array silently truncates it to99— the dtype never changes on assignment. .tolist()converts back to a normal Python list (with commas).
Printed output: how to tell what you have
| What you see | What it is |
|---|---|
[56, 0, 74] (commas) | Python list |
[56 0 74] (spaces) | NumPy array, via print() |
array([56, 0, 74]) | NumPy array, via repr() / notebook display |
np.int64(56) | A NumPy scalar shown by repr() in NumPy 2 (print shows just 56) |
[72. 45.5] (dots) | Float array — 72. means 72.0 |
Common mistakes
- ❌
np.array(56, 0, 74)— passing values as separate arguments. ✅ Wrap them in a list:np.array([56, 0, 74]). - ❌ Expecting
arr[2] = 99.7to store a decimal in an int array. ✅ Create the array as float (np.array([...], dtype=float)) if you need decimals. - ❌ Forgetting that
arr + 5returns a new array. ✅ Store it:arr = arr + 5(orarr += 5) if you want to keep the result.
Interview tip
Q: Why does np.array([1, 2, 3])[0] = 9.99 store 9?
A: An array's dtype is fixed at creation. Assigning a float into an int64 array casts the value to int, which truncates toward zero. To keep decimals, create the array with a float dtype.
Key points to remember
- np.array(list) creates an ndarray, copying the data and choosing one dtype automatically.
- print(arr) shows values separated by spaces; repr(arr) shows array([...]) with commas.
- Indexing and slicing work just like lists: arr[0], arr[-1], arr[1:4].
- arr + 5 is applied to every element and returns a new array.
- Arrays are mutable, but the dtype never changes: assigning 99.7 into an int array stores 99.
- .tolist() converts an array back into a regular Python list.
Try it yourself
Create, inspect and use an array
Mutating values and the int-truncation trap
Practice: build your own player's array
Revision Flashcards
Tap a card to flip it and see the answer.
