Python Libraries and NumPy Tutorial for Beginners (2026)

Python Libraries and NumPy Tutorial for Beginners (2026)

What are Python Libraries?

Python libraries are collections of pre-written code that help developers perform common tasks without writing everything from scratch. A library contains modules, functions, classes, and tools designed for specific purposes.
 
Why Do We Use Python Libraries?

Python libraries help developers:
  • Save development time
  • Reduce coding effort
  • Improve code quality
  • Perform complex tasks easily
  • Increase productivity
  • Write clean and reusable programs


What is NumPy?

NumPy stands for Numerical Python. It is one of the most popular Python libraries used for performing mathematical and numerical operations efficiently.
NumPy provides a powerful object called an ndarray (N-dimensional Array), which stores multiple values of the same data type in a compact and efficient way.
It is much faster than Python lists when working with large amounts of numerical data because it is implemented in optimized C code.

Why Use NumPy?

NumPy is widely used because it:
  • Performs calculations much faster than Python lists
  • Uses less memory
  • Supports multi-dimensional arrays
  • Includes hundreds of mathematical functions
  • Is the foundation of libraries like Pandas, SciPy, TensorFlow, and Scikit-learn
  • Is widely used in Data Science, Artificial Intelligence, Machine Learning, and Scientific Computing

  • Installing NumPy
  • pip install numpy
  • Import NumPy
  • import numpy as np

Using np is a common convention that makes your code shorter and easier to read.

 
What are Arrays in Python (NumPy)?

 An array is a data structure that stores multiple values in a single variable. Think of an array as a row of boxes, where each box contains one value. Instead of creating many separate variables like student1, student2, and student3, you can store all the values together in one array.
  Arrays are mainly used when you need to work with a large amount of similar data, such as marks, salaries, temperatures, product prices, or sensor readings.
   In Python, the most commonly used array is the NumPy array, which is created using the NumPy library. A NumPy array is specially designed for numerical calculations. It stores data more efficiently than a Python list and performs calculations much faster.
Why Do We Use Arrays?
  Imagine you are a teacher who wants to store the marks of 500 students. Creating 500 separate variables would be difficult and confusing. With an array, you can store all the marks in one place.

 example:

import numpy as np
 
marks = np.array([78, 85, 92, 67, 88])
 
print(marks)
Output
[78 85 92 67 88]

Arrays vs Python Lists

At first glance, arrays and lists may look similar because both store multiple values. However, they are designed for different purposes.
A Python list can store different types of data together.

data = ["Rahul", 25, 78.5, True]

A NumPy array is mainly designed to store values of the same type.
numbers = np.array([10, 20, 30, 40])

Arrays use less memory and perform calculations much faster than lists. That is why data scientists, engineers, and machine learning developers prefer NumPy arrays.

Types of Arrays

One-Dimensional Array (1D)
A one-dimensional array contains values in a single row.
import numpy as np
 
numbers = np.array([10, 20, 30, 40, 50])
 
print(numbers)
Output
[10 20 30 40 50]
This type of array is commonly used to store a simple list of values such as marks, prices, or ages.

Two-Dimensional Array (2D)

A two-dimensional array contains rows and columns. It looks like a table.
import numpy as np
 
marks = np.array([
    [78, 82, 90],
    [85, 88, 91],
    [72, 80, 87]
])
 
print(marks)
Output
[[78 82 90]
 [85 88 91]
 [72 80 87]]
This type of array is useful for storing tables such as student records, monthly sales, or employee salaries.
Three-Dimensional Array (3D)
A three-dimensional array contains multiple tables.
import numpy as np
 
data = np.array([
    [[1,2],[3,4]],
    [[5,6],[7,8]]
])
 
print(data)
Three-dimensional arrays are often used in image processing, machine learning, medical imaging, and scientific simulations.

What are Array Operations?

Once an array is created, we usually perform different calculations on it. These calculations are called array operations.
One of the biggest advantages of NumPy is that it allows you to perform calculations on all elements of an array at the same time. You do not need to write loops for most operations. This makes your code shorter, easier to understand, and much faster.
Addition
Suppose a shop increases the price of every product by ₹100.
import numpy as np
 
prices = np.array([500, 700, 900])
 
new_prices = prices + 100
 
print(new_prices)
Output
[600 800 1000]
Every element is increased automatically.
Subtraction
Suppose you want to apply a ₹50 discount to every product.
prices = np.array([500, 700, 900])
 
print(prices - 50)
Output
[450 650 850]
Multiplication
Suppose the quantity of every product becomes twice as much.
quantity = np.array([2, 4, 6])
 
print(quantity * 2)
Output
[4 8 12]
Division
Suppose you want to calculate the average marks by dividing the total by 5.
marks = np.array([250, 300, 350])
 
print(marks / 5)
Output
[50. 60. 70.]

Array-to-Array Operations

NumPy can also perform operations between two arrays.
import numpy as np
 
math = np.array([80, 75, 90])
science = np.array([85, 80, 88])
 
total = math + science
 
print(total)
Output
[165 155 178]
Each value is added to the value in the same position.
Finding Maximum and Minimum Values
import numpy as np
 
marks = np.array([75, 88, 95, 68, 82])
 
print("Highest:", np.max(marks))
print("Lowest:", np.min(marks))
Output
Highest: 95
Lowest: 68
This is useful when finding the highest score, maximum salary, or hottest temperature.
Calculating the Total
import numpy as np
 
sales = np.array([1500, 2200, 1800, 2700])
 
print(np.sum(sales))
Output
8200
Calculating the Average
import numpy as np
 
marks = np.array([70, 80, 90])
 
print(np.mean(marks))
Output
80.0
Average calculations are commonly used in schools, businesses, and research.

What are Mathematical Functions in NumPy?


NumPy provides many built-in mathematical functions that make calculations simple and fast. Instead of writing complex formulas yourself, you can use ready-made functions.
These functions are useful in finance, engineering, machine learning, scientific research, business analytics, and everyday programming.

Square Root


The square root function calculates the square root of each element.
import numpy as np
 
numbers = np.array([4, 9, 16, 25])
 
print(np.sqrt(numbers))
Output
[2. 3. 4. 5.]
Power
The power function raises each value to a specified power.
import numpy as np
 
numbers = np.array([2, 3, 4])
 
print(np.power(numbers, 2))
Output
[ 4  9 16]

Absolute Value

Absolute value converts negative numbers into positive numbers.
import numpy as np
 
numbers = np.array([-5, -10, 15])
 
print(np.abs(numbers))
Output
[ 5 10 15]
This is useful when measuring distance or finding the difference between values.
Exponential Function
import numpy as np
 
print(np.exp([1,2,3]))
The exponential function is commonly used in finance, population growth, and scientific calculations.
Logarithm
import numpy as np
 
print(np.log([1,2,4]))
Logarithmic functions are widely used in machine learning, statistics, and data analysis.
Trigonometric Functions
NumPy provides trigonometric functions such as sine, cosine, and tangent.
import numpy as np
 
print(np.sin(0))
print(np.cos(0))
print(np.tan(0))
These functions are useful in engineering, physics, robotics, and graphics programming.
Rounding Numbers
Sometimes calculations produce decimal values, but you need whole numbers.
import numpy as np
 
numbers = np.array([4.3, 6.8, 9.2])
 
print(np.round(numbers))
Output
[4. 7. 9.]
Ceiling Function
The ceiling function always rounds a number up to the nearest integer.
import numpy as np
 
print(np.ceil([2.1, 3.4, 4.8]))
Output
[3. 4. 5.]
Floor Function
The floor function always rounds a number down to the nearest integer.
import numpy as np
 
print(np.floor([2.9, 3.8, 4.2]))
Output
[2. 3. 4.]
Statistical Functions
NumPy also provides functions for statistical calculations.
import numpy as np
 
marks = np.array([70, 80, 90, 85, 95])
 
print("Total:", np.sum(marks))
print("Average:", np.mean(marks))
print("Median:", np.median(marks))
print("Highest:", np.max(marks))
print("Lowest:", np.min(marks))
print("Standard Deviation:", np.std(marks))
These functions are useful for analyzing exam results, business reports, financial data, and research datasets.
Real-Life Example
Imagine you own a supermarket with thousands of products. Every month you need to:
  • Increase prices by 10%
  • Calculate total sales
  • Find the highest-selling product
  • Calculate the average daily sales
  • Generate monthly reports
Doing all these calculations manually or using loops would take more time. With NumPy arrays and mathematical functions, these tasks can be completed in just a few lines of code. This is why NumPy is one of the most important libraries in Python and is widely used in data science, artificial intelligence, finance, engineering, healthcare, and scientific research.

Python Arrays and NumPy Interview Questions


1. What is NumPy?
NumPy is an open-source Python library used for numerical computing and working with multidimensional arrays.
2. What does NumPy stand for?
 NumPy stands for Numerical Python.
3. What is an array?
An array is a collection of elements stored in a single variable, where all elements are usually of the same data type.
4. How do you import NumPy?
import numpy as np
5. Which function is used to create a NumPy array?
np.array()

6. What is the difference between a list and a NumPy array?
  • Lists can store different data types.
  • NumPy arrays are optimized for numerical data.
  • NumPy arrays are faster and use less memory.

7. What is vectorization?
Vectorization is performing operations on an entire array without using loops.

8. What is the advantage of using NumPy arrays?
  • Faster execution
  • Less memory usage
  • Easy mathematical calculations
  • Better performance with large datasets

9. How do you find the shape of an array? 
array.shape

10. How do you find the number of dimensions of an array?
array.ndim
 

Frequently Asked Questions (FAQs)

1. What is an array in Python?
An array is a data structure used to store multiple values in a single variable. In Python, NumPy arrays are commonly used because they are faster and more efficient than Python lists for numerical calculations.

2. What is NumPy?
NumPy (Numerical Python) is a popular Python library used for numerical computing. It provides powerful array objects and many mathematical functions for fast data processing.

3. Why are NumPy arrays faster than Python lists?
NumPy arrays are implemented in optimized C code and store data more efficiently in memory. They also support vectorized operations, allowing calculations on entire arrays without using loops.

4. What is the difference between a Python list and a NumPy array?
A Python list can store different data types, while a NumPy array is mainly designed to store elements of the same data type. NumPy arrays are faster and consume less memory for numerical operations.

5. What are array operations in NumPy?
Array operations are calculations performed directly on array elements, such as addition, subtraction, multiplication, division, comparison, and statistical operations.

6. What is vectorization in NumPy?
Vectorization is the process of performing operations on an entire array at once instead of using loops. It makes the code shorter, cleaner, and much faster.

7. How do you create a NumPy array?
You can create a NumPy array using the np.array() function.
import numpy as np
numbers = np.array([10, 20, 30, 40])

8. What are the different types of NumPy arrays?
The three main types of arrays are:
  • One-Dimensional (1D) Array
  • Two-Dimensional (2D) Array
  • Three-Dimensional (3D) Array

9. Which mathematical functions are commonly used in NumPy?
Some commonly used mathematical functions are:
  • np.sqrt()
  • np.power()
  • np.abs()
  • np.sum()
  • np.mean()
  • np.max()
  • np.min()
  • np.median()
  • np.std()
  • np.log()
  • np.exp()

10. Where is NumPy used in real-world applications?
NumPy is widely used in:
  • Data Science
  • Machine Learning
  • Artificial Intelligence
  • Data Analysis
  • Finance
  • Scientific Research
  • Engineering
  • Image Processing
 


Comments

Popular posts from this blog

HTML Tag

HTML Input Type Submit Syntax and Example

CSS Text Color Explained with Syntax and HTML Examples