Mathew K Analytics

Lesson 25 · Python for Data Science

Master NumPy Math Operations: Essential Python Techniques for Data Science

NumPy is a powerful Python library for handling numbers and data quickly. Today, you will learn why NumPy makes math easy, fast, and fun for everyone. Ready…

⬇ Download notebookOpen in Colab ↗

What you'll learn

Data

No separate download needed — the notebook creates or downloads everything it uses.

📓 Full notebook

Download .ipynb
 

Welcome to NumPy Math Operations!#

NumPy is a powerful Python library for handling numbers and data quickly.

Today, you will learn why NumPy makes math easy, fast, and fun for everyone.

Ready to start your NumPy journey?

# Before using NumPy, we need to install and import it.
 
import numpy as np
 
print('NumPy is ready!')
NumPy is ready!

What is an Array?#

A NumPy array is like a super-powered list for numbers.

Arrays make it easy to do math operations with many values at once.

Let us create your first array!

# Create a simple NumPy array
my_array = np.array([1, 2, 3, 4, 5])
print('Here is your array:', my_array)
Here is your array: [1 2 3 4 5]
# Arrays look like lists, but NumPy lets us do math on all elements at once.
result = my_array + 2
print('Add 2 to every value:', result)
Add 2 to every value: [3 4 5 6 7]

Math Operations with Arrays#

You can use +, -, *, /, and more with arrays.

This works for all items, instantly!

# Subtract 1 from each value
print('Subtract 1:', my_array - 1)

# Multiply every value by 3
print('Multiply by 3:', my_array * 3)

# Divide all by 2
print('Divide by 2:', my_array / 2)
Subtract 1: [0 1 2 3 4]
Multiply by 3: [ 3  6  9 12 15]
Divide by 2: [0.5 1.  1.5 2.  2.5]
# You can also do math with two arrays of the same shape.
array_b = np.array([10, 20, 30, 40, 50])
sum_arrays = my_array + array_b
print('Add two arrays:', sum_arrays)
Add two arrays: [11 22 33 44 55]

Square Roots and Exponents#

NumPy offers special functions for advanced math.

Let us try taking the square root and squaring numbers!

# Take the square root with np.sqrt
roots = np.sqrt(my_array)
print('Square roots:', roots)

# Raise every value to the power of 2
squared = np.power(my_array, 2)
print('Squares:', squared)
Square roots: [1.         1.41421356 1.73205081 2.         2.23606798]
Squares: [ 1  4  9 16 25]
# Use input and math with arrays
user_num = int(input('Pick a whole number: '))
result = my_array * user_num
print('Your number times every value:', result)
Your number times every value: [ 4  8 12 16 20]
 
# What happens if we use two arrays of different sizes?
array_short = np.array([1, 2])
try:
    print(my_array + array_short)
except ValueError as e:
    print('Error:', e)
    
Error: operands could not be broadcast together with shapes (5,) (2,) 

Useful Statistical Functions#

Arrays make it easy to get sums, averages, and more.

These are great for grades, sales, science, and budgeting.

# Find the sum, mean, min, and max.
print('Sum:', np.sum(my_array))
print('Mean (average):', np.mean(my_array))
print('Minimum:', np.min(my_array))
print('Maximum:', np.max(my_array))
Sum: 15
Mean (average): 3.0
Minimum: 1
Maximum: 5
# You can change an array element by index.
my_array[0] = 42
print('New array after change:', my_array)
New array after change: [42  2  3  4  5]

Removing or Masking Values#

NumPy does not remove values directly, but you can select only the values you want.

This can be useful for skipping outliers or cleaning your data.

# Select only values greater than 10
filtered = my_array[my_array > 10]
print('Values above 10:', filtered)
Values above 10: [42]
# Use array slicing to get only certain values.
first_two = my_array[:2]
print('First two values:', first_two)
First two values: [42  2]

Iterating through Arrays#

We can loop through arrays to process values one at a time.

This is great for counting, searching, or making changes.

# Loop through and print each value in my_array.
for value in my_array:
    print('Array element:', value)
    
Array element: 42
Array element: 2
Array element: 3
Array element: 4
Array element: 5
# Use a list comprehension with arrays.
plus_five = [x + 5 for x in my_array]
print('Each value plus five:', plus_five)
Each value plus five: [np.int64(47), np.int64(7), np.int64(8), np.int64(9), np.int64(10)]

Mini Project: Grades Calculator#

Imagine you have test scores and want to do some quick math with them.

Let us try using arrays to make this much easier.

# Ask the user for five test scores
scores = []
for i in range(5):
    s = float(input(f'Enter score {i + 1}: '))
    scores.append(s)
scores_array = np.array(scores)
print('Your scores:', scores_array)
Your scores: [90.  78.5 85.  92.  88. ]
 
# Calculate average, minimum, and maximum score
avg = np.mean(scores_array)
minimum = np.min(scores_array)
maximum = np.max(scores_array)
print('Average:', avg)
print('Lowest:', minimum)
print('Highest:', maximum)
Average: 86.7
Lowest: 78.5
Highest: 92.0

Quick Recap!#

You explored arrays, fast math, statistics, and even built a simple project.

NumPy can help with science, finance, games, and more.

Keep practicing, and try changing the examples to fit your real-life data.

Thank you for joining this NumPy Math Operations lesson.

If you had fun learning, please like, comment, and subscribe for more Python videos!

Share this lesson with a friend who wants to master Python and data science too.

See you in the next lesson!

Found this useful?

All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.