0
0
NumPydata~5 mins

What is NumPy

Choose your learning style9 modes available
Introduction

NumPy helps us work with numbers and lists of numbers easily and fast. It makes math with many numbers simple.

When you want to handle large lists of numbers quickly.
When you need to do math like adding, multiplying, or finding averages on many numbers.
When you want to work with tables or grids of numbers, like images or data.
When you want to prepare data for machine learning or statistics.
When you want to use other tools that need fast number handling.
Syntax
NumPy
import numpy as np

# Create an array
arr = np.array([1, 2, 3])

# Do math
sum_arr = np.sum(arr)

We usually import NumPy as np to keep code short.

NumPy uses array to store numbers efficiently.

Examples
This creates a NumPy array from a list and prints it.
NumPy
import numpy as np
arr = np.array([10, 20, 30])
print(arr)
This creates a 2x2 grid (matrix) of numbers.
NumPy
import numpy as np
arr = np.array([[1, 2], [3, 4]])
print(arr)
This sums all numbers in the array and prints the result.
NumPy
import numpy as np
arr = np.array([1, 2, 3])
sum_val = np.sum(arr)
print(sum_val)
Sample Program

This program shows how to create a NumPy array from a list, then find the average and sum of the numbers.

NumPy
import numpy as np

# Create a list of numbers
numbers = [5, 10, 15, 20]

# Convert list to NumPy array
arr = np.array(numbers)

# Calculate mean (average)
mean_val = np.mean(arr)

# Calculate sum
sum_val = np.sum(arr)

print(f"Numbers: {arr}")
print(f"Mean: {mean_val}")
print(f"Sum: {sum_val}")
OutputSuccess
Important Notes

NumPy arrays are faster and use less memory than regular Python lists for numbers.

NumPy has many built-in math functions like sum, mean, max, and min.

NumPy is the base for many other data science tools.

Summary

NumPy helps handle and do math on many numbers easily.

It uses arrays to store numbers efficiently.

It is a key tool for data science and scientific computing.