NumPy in Python | Arrays, 1D Arrays, 2D Arrays and Creating Arrays from Lists | Complete Notes | CBSE Class 11 Informatics Practices (2026–27)
Class 11 · Informatics Practices
NumPy in Python | Arrays, 1D Arrays, 2D Arrays and Creating Arrays from Lists
Python provides several built-in data structures such as lists, tuples, dictionaries, and sets. While lists are suitable for storing collections of data, they are not the most efficient choice for performing large-scale numerical computations. To overcome this limitation, Python provides the NumPy library.
NumPy (Numerical Python) is one of the most popular Python libraries used for scientific computing, data analysis, machine learning, artificial intelligence, and engineering applications. It provides a powerful array object that is faster and more memory-efficient than Python lists when performing numerical operations.
Learning Outcomes
After studying this chapter, you will be able to:
- Understand the purpose of the NumPy library.
- Create NumPy arrays.
- Create arrays from Python lists.
- Work with one-dimensional (1D) arrays.
- Work with two-dimensional (2D) arrays.
- Access elements of NumPy arrays.
What is NumPy?
NumPy (Numerical Python) is a Python library that provides support for creating and working with multidimensional arrays. It also offers numerous mathematical functions for performing fast numerical computations.
Why Do We Need NumPy?
Although Python lists can store multiple values, they are slower when processing large amounts of numerical data. NumPy arrays are optimized for speed and memory usage, making them suitable for scientific and mathematical applications.
| Python List | NumPy Array |
|---|---|
| General-purpose data storage. | Optimized for numerical data. |
| Consumes more memory. | Consumes less memory. |
| Slower for calculations. | Faster mathematical operations. |
| Supports mixed data types. | Generally stores elements of the same data type. |
Installing NumPy
If NumPy is not already installed, it can be installed using the following command:
pip install numpy
Importing NumPy
Before using NumPy, it must be imported into the Python program.
import numpy as np
The alias np is commonly used to make the code shorter and easier to write.
What is an Array?
An array is a collection of similar data elements stored in contiguous memory locations. In NumPy, arrays are represented using the ndarray (N-dimensional array) object.
Advantages of NumPy Arrays
- Fast numerical calculations.
- Less memory consumption.
- Easy mathematical operations.
- Efficient handling of large datasets.
- Foundation for Data Science and Artificial Intelligence.
Creating an Array from a Python List
The array() function is used to convert a Python list into a NumPy array.
Syntax
np.array(list_name)
Example
import numpy as np
numbers = [10, 20, 30, 40, 50]
arr = np.array(numbers)
print(arr)
Output
[10 20 30 40 50]
One-Dimensional (1D) Array
A one-dimensional (1D) array stores elements in a single row. It has only one axis.
Example
import numpy as np
marks = np.array([85, 92, 78, 88, 95])
print(marks)
Output
[85 92 78 88 95]
Accessing Elements of a 1D Array
import numpy as np
numbers = np.array([5, 10, 15, 20, 25])
print(numbers[0])
print(numbers[3])
Output
5
20
Negative Indexing in Arrays
import numpy as np
numbers = np.array([5, 10, 15, 20, 25])
print(numbers[-1])
print(numbers[-2])
Output
25
20
Two-Dimensional (2D) Array
A two-dimensional (2D) array consists of rows and columns. It is similar to a matrix or table.
Example
import numpy as np
marks = np.array([
[85, 90, 88],
[76, 81, 95],
[92, 89, 84]
])
print(marks)
Output
[[85 90 88]
[76 81 95]
[92 89 84]]
Structure of a 2D Array
| Column 0 | Column 1 | Column 2 | |
|---|---|---|---|
| Row 0 | 85 | 90 | 88 |
| Row 1 | 76 | 81 | 95 |
| Row 2 | 92 | 89 | 84 |
Accessing Elements of a 2D Array
Elements in a 2D array are accessed using the row index followed by the column index.
Syntax
array_name[row][column]
Example
import numpy as np
marks = np.array([
[85, 90, 88],
[76, 81, 95],
[92, 89, 84]
])
print(marks[0][1])
print(marks[2][0])
Output
90
92
Real-Life Applications of NumPy
| Application | Use of NumPy |
|---|---|
| Artificial Intelligence | Training machine learning models. |
| Data Science | Analyzing large datasets. |
| Computer Vision | Processing images as arrays. |
| Scientific Research | Performing numerical computations. |
| Weather Forecasting | Analyzing climate data. |
Comparison Between Python Lists and NumPy Arrays
| Python List | NumPy Array |
|---|---|
| Built into Python. | Provided by the NumPy library. |
| Can store different data types. | Generally stores elements of the same data type. |
| Suitable for general-purpose programming. | Suitable for numerical and scientific computations. |
| Slower for mathematical calculations. | Faster for mathematical calculations. |
| Consumes comparatively more memory. | Consumes comparatively less memory. |
One-Dimensional (1D) Array vs Two-Dimensional (2D) Array
| 1D Array | 2D Array |
|---|---|
| Contains only one row of elements. | Contains multiple rows and columns. |
| Has one axis. | Has two axes. |
| Similar to a Python list. | Similar to a table or matrix. |
| Accessed using one index. | Accessed using row and column indexes. |
Solved Program 1: Create a NumPy Array
import numpy as np
numbers = np.array([5, 10, 15, 20])
print(numbers)
Output
[ 5 10 15 20 ]
Solved Program 2: Display Individual Elements
import numpy as np
marks = np.array([85, 90, 88, 95])
print(marks[0])
print(marks[2])
Output
85
88
Solved Program 3: Display All Elements of a 1D Array
import numpy as np
numbers = np.array([12, 24, 36, 48])
for value in numbers:
print(value)
Output
12
24
36
48
Solved Program 4: Display All Elements of a 2D Array
import numpy as np
matrix = np.array([
[10, 20],
[30, 40]
])
for row in matrix:
print(row)
Output
[10 20]
[30 40]
Solved Program 5: Access an Element from a 2D Array
import numpy as np
matrix = np.array([
[5, 10, 15],
[20, 25, 30],
[35, 40, 45]
])
print(matrix[1][2])
Output
30
Solved Program 6: Display a Complete Row
import numpy as np
marks = np.array([
[85, 90, 88],
[76, 81, 95],
[92, 89, 84]
])
print(marks[1])
Output
[76 81 95]
Solved Program 7: Create an Array from a List of Cities
import numpy as np
cities = ["Jaipur", "Delhi", "Mumbai"]
arr = np.array(cities)
print(arr)
Output
['Jaipur' 'Delhi' 'Mumbai']
Common Errors
| Mistake | Correct Practice |
|---|---|
| Forgetting to import NumPy. | Always use import numpy as np before creating arrays. |
| Using array() without np. | Use np.array(). |
| Using an invalid index. | Ensure the index is within the valid range. |
| Confusing Python lists with NumPy arrays. | Remember that NumPy arrays belong to the NumPy library. |
| Accessing a wrong row or column in a 2D array. | Use the correct row and column indexes. |
Interview Corner
Q. Why is NumPy preferred over Python lists for numerical computations?
Answer: NumPy arrays are faster, consume less memory, and are specially designed for efficient numerical and scientific computations.
Competency-Based Questions
- A data analyst needs to process thousands of numerical values quickly. Should Python lists or NumPy arrays be used? Give a reason.
- A teacher stores the marks of students in rows and columns. Which type of NumPy array is more suitable?
- An image processing application stores pixel values in rows and columns. Which type of array is used?
- A programmer wants to convert a Python list into a NumPy array. Which function should be used?
- How are elements accessed in a two-dimensional array?
Multiple Choice Questions
- NumPy stands for:
- (a) Numeric Program
- (b) Numerical Python
- (c) Number Processing
- (d) Numeric Package
- Which statement imports NumPy using the standard alias?
- (a) import NumPy
- (b) import numpy
- (c) import numpy as np
- (d) include numpy
- Which function creates a NumPy array?
- (a) create()
- (b) array()
- (c) list()
- (d) ndarray()
- A two-dimensional array consists of:
- (a) Only rows
- (b) Only columns
- (c) Rows and columns
- (d) Keys and values
- Which library is widely used for numerical computing in Python?
- (a) Turtle
- (b) Pandas
- (c) NumPy
- (d) Tkinter
Quick Revision
| Concept | Remember |
|---|---|
| NumPy | Python library for numerical computing. |
| Array | Collection of similar data elements. |
| np.array() | Creates a NumPy array. |
| 1D Array | Single row of elements. |
| 2D Array | Rows and columns. |
| Import Statement | import numpy as np |
Important Points to Remember
- NumPy stands for Numerical Python.
- NumPy is imported using import numpy as np.
- np.array() converts a Python list into a NumPy array.
- A NumPy array usually stores elements of the same data type.
- A 1D array has one axis, whereas a 2D array has two axes.
- Elements of a 2D array are accessed using row and column indexes.
- NumPy arrays are faster and more memory-efficient than Python lists for numerical operations.
CBSE Exam Tips
- Remember the syntax for importing NumPy.
- Practice creating arrays from Python lists.
- Understand the difference between 1D and 2D arrays.
- Practice accessing elements using indexes.
- Learn the advantages of NumPy arrays over Python lists.
- Practice output-based questions involving array indexing.
Summary
NumPy is a powerful Python library used for numerical and scientific computing. It provides the efficient ndarray data structure, which is faster and more memory-efficient than Python lists for handling numerical data. Using the np.array() function, Python lists can easily be converted into NumPy arrays. NumPy supports both one-dimensional (1D) and two-dimensional (2D) arrays, making it suitable for representing vectors, tables, matrices, and datasets. A clear understanding of NumPy arrays forms the foundation for advanced topics such as data science, machine learning, artificial intelligence, and scientific computing.