0
0
SciPydata~5 mins

NumPy array foundation review in SciPy

Choose your learning style9 modes available
Introduction

NumPy arrays help us store and work with many numbers easily. They make math with lists faster and simpler.

When you want to do math on many numbers at once, like adding or multiplying lists of numbers.
When you need to store data in a table or grid form, like pixels in an image or measurements over time.
When you want to use tools that need fast number crunching, like machine learning or statistics.
When you want to save memory compared to using regular Python lists for numbers.
Syntax
SciPy
import numpy as np

# Create a NumPy array from a Python list
array_name = np.array([1, 2, 3, 4])

Use np.array() to make a NumPy array from a list or other sequence.

NumPy arrays have a fixed size and all elements must be the same type (usually numbers).

Examples
This creates an empty NumPy array with zero elements.
SciPy
import numpy as np

# Empty array (no elements)
empty_array = np.array([])
print(empty_array)
This creates a NumPy array with just one number.
SciPy
import numpy as np

# Array with one element
single_element_array = np.array([10])
print(single_element_array)
This creates a NumPy array with three numbers.
SciPy
import numpy as np

# Array with multiple elements
multi_element_array = np.array([5, 10, 15])
print(multi_element_array)
This creates a 2D NumPy array with rows and columns.
SciPy
import numpy as np

# 2D array (like a table)
matrix_array = np.array([[1, 2], [3, 4]])
print(matrix_array)
Sample Program

This program shows how to create different NumPy arrays: empty, single element, multiple elements, and 2D arrays. It prints each one to see their contents.

SciPy
import numpy as np

# Create an empty array
empty_array = np.array([])
print("Empty array:", empty_array)

# Create an array with one element
single_element_array = np.array([42])
print("Single element array:", single_element_array)

# Create an array with multiple elements
numbers_array = np.array([10, 20, 30, 40])
print("Multiple elements array:", numbers_array)

# Create a 2D array
matrix_array = np.array([[1, 2], [3, 4]])
print("2D array (matrix):")
print(matrix_array)
OutputSuccess
Important Notes

Creating arrays is very fast and uses less memory than Python lists for numbers.

NumPy arrays support many math operations directly, like adding or multiplying all elements.

Common mistake: Trying to mix different data types in one array can cause unexpected results.

Use arrays when you need fast math on many numbers; use lists if you need to store mixed data types or change size often.

Summary

NumPy arrays store numbers efficiently and support fast math.

You can create arrays with zero, one, or many elements, including multi-dimensional arrays.

Arrays are better than lists for math and big data tasks.