Imagine receiving a dataset containing thousands of rows and hundreds of columns. Before performing any calculations, you first need answers to questions such as:
- How many dimensions does this array have?
- How many rows and columns are present?
- What type of data is stored?
- How much memory is the array consuming?
- How many elements does the array contain?
NumPy provides several built-in array properties (attributes) that answer these questions instantly. These properties help Data Scientists understand the structure of data before performing analysis or building Machine Learning models.
In this lesson, you will also learn several built-in functions such as zeros(), ones(), arange(), and linspace(), which allow you to create arrays quickly without typing every element manually.
These concepts are used daily in Data Science, Artificial Intelligence, Machine Learning, Deep Learning, Robotics, Computer Vision, and Scientific Computing.
What are NumPy Array Properties?
Every NumPy array contains built-in information called properties or attributes.
These attributes describe the array's internal structure and provide useful information such as its dimensions, shape, size, data type, and memory usage.
Instead of calculating this information manually, NumPy allows us to retrieve it instantly using built-in attributes.
Consider the following array.
import numpy as np
arr = np.array([
[10, 20, 30],
[40, 50, 60]
])
Output
[[10 20 30] [40 50 60]]
Throughout this lesson, we will use this array to understand every property.
Why are Array Properties Important?
Suppose you receive a dataset from a company containing customer information.
Before cleaning or analyzing the data, you must understand its structure.
Array properties help you answer questions like:
- Is the dataset one-dimensional or two-dimensional?
- How many rows and columns are present?
- What type of values are stored?
- How much memory is being used?
- How many values exist in total?
Without these properties, working with large datasets would become difficult.
That is why almost every Data Scientist checks array properties before starting any analysis.
Important NumPy Array Properties
NumPy provides many attributes, but beginners should first understand these six.
| Property | Purpose |
| ndim | Number of dimensions |
| shape | Number of rows and columns |
| size | Total number of elements |
| dtype | Data type of elements |
| itemsize | Memory used by one element |
| nbytes | Total memory used |
These six properties form the foundation of understanding every NumPy array.
Understanding ndim
The ndim property tells us the number of dimensions present in an array.
Dimension simply means the number of axes.
For example,
A normal list
[10 20 30]
has only one axis.
It is called a 1-D Array.
An array like
[[10 20 30] [40 50 60]]
contains rows and columns.
It is called a 2-D Array.
Example
import numpy as np
arr = np.array([
[10,20,30],
[40,50,60]
])
print(arr.ndim)
Output
2
Explanation
The array has
- 2 Rows
- 3 Columns
Therefore,
Dimension = 2
Understanding shape
The shape property tells us how many rows and columns exist inside the array.
Example
import numpy as np
arr=np.array([
[10,20,30],
[40,50,60]
])
print(arr.shape)
Output
(2, 3)
Meaning
2 Rows 3 Columns
If an array contains
5 Rows 4 Columns
then
print(arr.shape)
will return
(5,4)
The shape property is extremely important because almost every Machine Learning algorithm expects data in a particular shape.
Understanding size
The size property tells us the total number of elements stored in the array.
Example
import numpy as np
arr=np.array([
[10,20,30],
[40,50,60]
])
print(arr.size)
Output
6
Explanation
There are
10 20 30 40 50 60
Total Elements
6
Formula
Rows × Columns = Total Elements 2 × 3 = 6
Understanding dtype
The dtype property tells us what type of data is stored inside the array.
Example
import numpy as np arr=np.array([10,20,30]) print(arr.dtype)
Output
int64
Here,
Every value is an integer.
Therefore,
NumPy automatically stores the array as int64.
Example with Decimal Numbers
import numpy as np arr=np.array([2.5,5.6,7.8]) print(arr.dtype)
Output
float64
Example with Strings
import numpy as np arr=np.array(["Python","NumPy","AI"]) print(arr.dtype)
Example Output
<U6
The exact output may differ depending on the longest string and your NumPy version, but it indicates a Unicode string data type.
Knowing the data type is important because mathematical operations work differently for integers, floating-point numbers, strings, and Boolean values.
Understanding itemsize
The itemsize property tells us how much memory is occupied by a single element in the array.
Example
import numpy as np arr=np.array([10,20,30,40]) print(arr.itemsize)
Output
8
Explanation
Each integer occupies 8 bytes of memory.
If an array contains four integers,
Each element individually uses
8 Bytes
Understanding memory usage becomes very important while working with datasets containing millions of records.
Understanding nbytes
The nbytes property tells us the total memory consumed by the entire array.
Example
import numpy as np arr=np.array([10,20,30,40]) print(arr.nbytes)
Output
32
Formula
nbytes = itemsize × size
For our array,
itemsize = 8 size = 4
Therefore,
8 × 4 = 32 Bytes
Instead of calculating this manually, NumPy provides the result instantly.
Comparing All Properties Together
import numpy as np
arr = np.array([
[10,20,30],
[40,50,60]
])
print("Dimensions :", arr.ndim)
print("Shape :", arr.shape)
print("Size :", arr.size)
print("Data Type :", arr.dtype)
print("Item Size :", arr.itemsize)
print("Total Bytes:", arr.nbytes)
Output
Dimensions : 2 Shape : (2, 3) Size : 6 Data Type : int64 Item Size : 8 Total Bytes: 48
Real-World Example
Imagine you are building a Machine Learning model that predicts house prices.
Before training your model, you load a dataset.
The very first thing you do is inspect its properties:
print(data.shape) print(data.ndim) print(data.dtype)
From this information, you immediately know whether the dataset has the expected number of rows and columns, whether the values are numeric, and whether it is ready for preprocessing.
Understanding array properties is therefore an essential first step in every Data Science workflow.
Creating Arrays Using zeros()
In many real-world applications, we often need to create arrays before storing actual data. Instead of manually typing dozens or even thousands of values, NumPy provides built-in functions that automatically create arrays filled with default values.
One of the most commonly used functions is zeros().
The zeros() function creates an array where every element is initialized with 0.
This function is extremely useful in Machine Learning, Deep Learning, Image Processing, Computer Vision, and Scientific Computing because many algorithms require empty arrays before processing begins.
Syntax
np.zeros(shape)
Where:
- shape → Number of rows and columns
Example 1: One-Dimensional Zero Array
import numpy as np arr = np.zeros(5) print(arr)
Output
[0. 0. 0. 0. 0.]
Notice that NumPy creates floating-point values (0.) by default.
Example 2: Two-Dimensional Zero Array
import numpy as np arr = np.zeros((3,4)) print(arr)
Output
[[0. 0. 0. 0.] [0. 0. 0. 0.] [0. 0. 0. 0.]]
Here,
- Rows = 3
- Columns = 4
Why do we use zeros()?
Suppose you are creating a Machine Learning model.
Before storing prediction values, you may need an empty array.
Instead of writing
[0,0,0,0,0,0,0,0,0,0]
NumPy creates it instantly.
predictions = np.zeros(1000)
One line creates an array containing 1000 values.
Creating Arrays Using ones()
The ones() function works almost exactly like zeros().
The only difference is that every element is initialized with 1.
This function is frequently used while initializing weights, matrices, masks, and testing algorithms.
Syntax
np.ones(shape)
Example 1
import numpy as np arr = np.ones(5) print(arr)
Output
[1. 1. 1. 1. 1.]
Example 2
import numpy as np arr = np.ones((2,3)) print(arr)
Output
[[1. 1. 1.] [1. 1. 1.]]
Real-World Example
Suppose every student receives 1 attendance point initially.
Instead of writing
[1,1,1,1,1,1,1]
We can simply write
attendance = np.ones(100)
This creates an array for 100 students in a single statement.
Difference Between zeros() and ones()
| zeros() | ones() |
| Fills array with 0 | Fills array with 1 |
| Used for empty initialization | Used for default values |
| Common in Machine Learning | Common in Neural Networks |
| Returns floating-point values by default | Returns floating-point values by default |
Creating Arrays Using arange()
Typing every number manually becomes impossible when working with long sequences.
Imagine writing
1 2 3 4 5 6 7 8 9 ...100
NumPy provides arange() to solve this problem.
The arange() function automatically generates numbers within a specified range.
Syntax
np.arange(start, stop, step)
Where:
- start → Starting value
- stop → Ending value (not included)
- step → Difference between consecutive numbers
Example 1
import numpy as np arr = np.arange(1,11) print(arr)
Output
[ 1 2 3 4 5 6 7 8 9 10 ]
Notice that 11 is not included.
Example 2
import numpy as np arr = np.arange(0,20,2) print(arr)
Output
[ 0 2 4 6 8 10 12 14 16 18 ]
Every element increases by 2.
Example 3
import numpy as np arr = np.arange(10,51,10) print(arr)
Output
[10 20 30 40 50]
Why use arange()?
Suppose you need student roll numbers.
Instead of writing
1,2,3,4,5,6,7,8.....1000
Simply write
roll_numbers = np.arange(1,1001)
Thousands of values are generated instantly.
Creating Arrays Using linspace()
Sometimes we don't know the step size.
Instead, we know exactly how many values we want.
For this purpose, NumPy provides linspace().
It creates equally spaced values between two numbers.
Syntax
np.linspace(start, stop, number_of_values)
Example
import numpy as np arr = np.linspace(0,10,5) print(arr)
Output
[ 0. 2.5 5. 7.5 10. ]
Notice
- Start value included
- End value included
- Exactly 5 values
Another Example
import numpy as np arr = np.linspace(1,100,10) print(arr)
Output
[ 1. 12. 23. 34. 45. 56. 67. 78. 89. 100.]
Difference Between arange() and linspace()
| arange() | linspace() |
| Uses step size | Uses number of values |
| Stop value is excluded | Stop value is included |
| Mostly used for loops and indexing | Mostly used for graphs and scientific calculations |
| Returns integer or float values | Usually returns floating-point values |
Which Function Should You Use?
Use zeros() when you need an empty array initialized with zeros.
Use ones() when every value should start as one.
Use arange() when you know the step size.
Use linspace() when you know how many equally spaced values you need.
Common Beginner Mistakes
Mistake 1
Forgetting double parentheses.
Incorrect
np.zeros(2,3)
Correct
np.zeros((2,3))
Mistake 2
Expecting the stop value in arange().
np.arange(1,5)
Output
[1 2 3 4]
5 is not included.
Mistake 3
Using linspace() expecting integers.
np.linspace(0,5,4)
Output
[0. 1.66666667 3.33333333 5. ]
This is completely normal because linspace() generates equally spaced values, not necessarily integers.
Best Practices
- Always import NumPy using:
import numpy as np
- Use meaningful variable names.
student_marks temperature sales_data image_pixels
instead of
a b c
- Always inspect an array using:
print(arr.shape) print(arr.dtype) print(arr.size)
before processing data.
Real-World Applications
These functions are used extensively across Data Science and AI.
zeros() initializes neural network parameters and placeholder arrays.
ones() creates masks, default values, and weight matrices.
arange() generates sequences such as roll numbers, IDs, timestamps, and iteration values.
linspace() is widely used for plotting graphs, mathematical functions, signal processing, simulations, and scientific visualization where evenly spaced values are required.
Understanding these functions is essential because they are used repeatedly in libraries such as Pandas, Matplotlib, SciPy, TensorFlow, PyTorch, and Scikit-learn.
Practice Questions
Congratulations! You have now learned the most important NumPy array properties and built-in array creation functions. It's time to test your understanding through practical exercises.
Try solving each question on your own before checking the answers. Practicing these questions will strengthen your confidence and improve your problem-solving skills.
Question 1
Create a NumPy array containing the following numbers:
10, 20, 30, 40, 50
Print the array.
Answer
import numpy as np arr = np.array([10, 20, 30, 40, 50]) print(arr)
Output
[10 20 30 40 50]
Question 2
Create a 3 × 3 array filled with zeros.
Answer
import numpy as np arr = np.zeros((3,3)) print(arr)
Output
[[0. 0. 0.] [0. 0. 0.] [0. 0. 0.]]
Question 3
Create a 2 × 4 array filled with ones.
Answer
import numpy as np arr = np.ones((2,4)) print(arr)
Output
[[1. 1. 1. 1.] [1. 1. 1. 1.]]
Question 4
Generate numbers from 1 to 20 using arange().
Answer
import numpy as np arr = np.arange(1,21) print(arr)
Output
[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20]
Question 5
Generate all even numbers between 0 and 20.
Answer
import numpy as np arr = np.arange(0,21,2) print(arr)
Output
[ 0 2 4 6 8 10 12 14 16 18 20]
Question 6
Generate 6 equally spaced numbers between 0 and 10.
Answer
import numpy as np arr = np.linspace(0,10,6) print(arr)
Output
[ 0. 2. 4. 6. 8. 10.]
Question 7
Find the following properties of the array.
import numpy as np
arr = np.array([
[10,20,30],
[40,50,60]
])
- ndim
- shape
- size
- dtype
Answer
import numpy as np
arr = np.array([
[10,20,30],
[40,50,60]
])
print(arr.ndim)
print(arr.shape)
print(arr.size)
print(arr.dtype)
Output
2 (2, 3) 6 int64
Question 8
Print the memory occupied by the following array.
import numpy as np arr = np.array([10,20,30,40])
Answer
import numpy as np arr = np.array([10,20,30,40]) print(arr.itemsize) print(arr.nbytes)
Output
8 32
Question 9
Create a 4 × 5 zero matrix.
Answer
import numpy as np matrix = np.zeros((4,5)) print(matrix)
Question 10
Create a 5 × 5 matrix filled with ones.
Answer
import numpy as np matrix = np.ones((5,5)) print(matrix)
Mini Coding Challenge
Without looking at the notes, write a Python program that:
- Imports NumPy
- Creates a 3 × 3 zero matrix
- Creates a 3 × 3 ones matrix
- Generates numbers from 1 to 10
- Generates five equally spaced values between 10 and 100
- Prints the shape of every array
Expected Solution
import numpy as np zero_matrix = np.zeros((3,3)) one_matrix = np.ones((3,3)) numbers = np.arange(1,11) values = np.linspace(10,100,5) print(zero_matrix.shape) print(one_matrix.shape) print(numbers.shape) print(values.shape)
Interview Questions
These are commonly asked in Python and Data Science interviews.
1. What is NumPy?
Answer
NumPy is an open-source Python library used for numerical computing and high-performance array operations.
2. What does ndarray stand for?
Answer
N-dimensional Array.
3. What is the difference between shape and size?
Answer
shape returns the number of rows and columns.
size returns the total number of elements.
4. What is dtype?
Answer
It returns the data type of the array elements.
5. What is itemsize?
Answer
It returns the memory occupied by one element in bytes.
6. What is nbytes?
Answer
It returns the total memory consumed by the complete array.
Formula:
nbytes = itemsize × size
7. What is the difference between zeros() and ones()?
Answer
zeros() creates an array filled with zeros.
ones() creates an array filled with ones.
8. What is the difference between arange() and linspace()?
Answer
arange() creates values using a fixed step size.
linspace() creates a fixed number of equally spaced values.
9. Which function includes the ending value?
Answer
linspace()
10. Which function excludes the ending value?
Answer
arange()
Common MCQs
Question 1
Which property returns the number of dimensions?
A. shape
B. ndim
C. dtype
D. size
Answer: B. ndim
Question 2
Which property returns the total number of elements?
A. size
B. shape
C. dtype
D. nbytes
Answer: A. size
Question 3
Which function creates an array of zeros?
A. ones()
B. zeros()
C. empty()
D. array()
Answer: B. zeros()
Question 4
Which function creates equally spaced values?
A. zeros()
B. ones()
C. linspace()
D. shape()
Answer: C. linspace()
Question 5
Which property returns the memory occupied by the complete array?
A. size
B. itemsize
C. nbytes
D. shape
Answer: C. nbytes
Assignment
Complete the following tasks on your own.
- Create a 5 × 5 zero matrix.
- Create a 5 × 5 ones matrix.
- Generate numbers from 50 to 100 using arange().
- Generate 11 equally spaced values between 0 and 100 using linspace().
- Print the following properties for every array:
- shape
- ndim
- size
- dtype
- itemsize
- nbytes
- Compare the outputs of arange() and linspace() and write two differences.
Summary
In this lesson, you learned how to inspect NumPy arrays using important properties such as ndim, shape, size, dtype, itemsize, and nbytes. You also explored powerful array creation functions like zeros(), ones(), arange(), and linspace(), which are essential for creating arrays efficiently in Data Science and Machine Learning projects.
Understanding these concepts is crucial because almost every advanced Python library—including Pandas, TensorFlow, PyTorch, SciPy, and Scikit-learn—relies on NumPy arrays internally. Mastering these fundamentals will make future topics much easier to understand.
Assignment 16: NumPy Fundamentals Challenge
Level 1
Import the NumPy library using the standard alias (np) and print the installed NumPy version.
Level 2
Create a one-dimensional NumPy array containing the values:
10, 20, 30, 40, 50
Print the array.
Level 3
Create the following two-dimensional NumPy array and print it.
1 2 3 4 5 6
Level 4
Create a 3 × 4 array filled with zeros using np.zeros().
Level 5
Create a 2 × 5 array filled with ones using np.ones().
Level 6
Generate numbers from 1 to 20 using np.arange() and print the array.
Level 7
Generate all even numbers between 0 and 30 using np.arange().
Level 8
Generate 10 equally spaced values between 0 and 100 using np.linspace().
Level 9
Create the following array:
[
[5, 10, 15],
[20, 25, 30]
]
Print the following properties:
- Number of Dimensions (ndim)
- Shape (shape)
- Size (size)
Level 10
Create a NumPy array containing decimal values:
2.5, 5.5, 8.5, 10.5
Print its data type using dtype.
Level 11
Create a 4 × 4 zero matrix and print the following:
- Shape
- Size
- Memory occupied by one element (itemsize)
- Total memory (nbytes)
Level 12
Create a 5 × 5 ones matrix and print:
- Number of Dimensions (ndim)
- Shape (shape)
- Size (size)
- Data Type (dtype)
- Memory per Element (itemsize)
- Total Memory (nbytes)
Level 13
Generate numbers from 10 to 100 with a step size of 10 using np.arange().
Print:
- Generated Array
- Shape
- Number of Dimensions
- Total Number of Elements
Level 14
A teacher wants to prepare a marks sheet for 50 students before entering their actual marks.
Perform the following tasks:
- Create an array of 50 zeros using np.zeros().
- Print the array.
- Print:
- Shape
- Size
- Data Type
- Memory per Element (itemsize)
- Total Memory (nbytes)
Level 15
A weather station records temperature values from 0°C to 50°C.
Generate 11 equally spaced temperature values using np.linspace().
Print:
- Generated Array
- Number of Dimensions (ndim)
- Shape (shape)
- Size (size)
- Data Type (dtype)
- Memory per Element (itemsize)
- Total Memory (nbytes)
Multiple Choice Questions (MCQs)
MCQ 1
Which NumPy property returns the total number of elements in an array?
A. shape
B. size
C. ndim
D. dtype
MCQ 2
Which NumPy function creates equally spaced values between two specified numbers?
A. arange()
B. zeros()
C. ones()
D. linspace()





