Creating arrays is the first step to work with data in Python using NumPy. Arrays help organize numbers so you can do math and analysis easily.
When you want to store a list of numbers to calculate their average.
When you need to perform math on many numbers at once, like adding or multiplying all of them.
When you want to prepare data for a graph or chart.
When you want to speed up calculations compared to using regular Python lists.
Syntax
NumPy
import numpy as np
# Create a 1D array from a list
array_name = np.array([1, 2, 3, 4])
# Create a 2D array (matrix) from a list of lists
matrix_name = np.array([[1, 2], [3, 4]])
Use np.array() to create arrays from Python lists.
Arrays can have one or more dimensions (1D, 2D, etc.).
Examples
This creates an empty array with zero elements.
NumPy
import numpy as np
# Empty array (no elements)
empty_array = np.array([])
print(empty_array)
This creates an array with just one number.
NumPy
import numpy as np
# Array with one element
single_element_array = np.array([10])
print(single_element_array)
This creates a 2D array with one row and three columns.
NumPy
import numpy as np
# 2D array with one row
one_row_array = np.array([[5, 6, 7]])
print(one_row_array)
This creates a 2D array with three rows and one column.
NumPy
import numpy as np
# 2D array with one column
one_column_array = np.array([[8], [9], [10]])
print(one_column_array)
Sample Program
This program shows how to create a 1D array and a 2D array using NumPy. It prints both arrays so you can see their structure.
NumPy
import numpy as np
# Create a 1D array from a list of numbers
numbers = [10, 20, 30, 40]
array_numbers = np.array(numbers)
print("Array before any operation:")
print(array_numbers)
# Create a 2D array (matrix) from a list of lists
matrix = [[1, 2], [3, 4]]
array_matrix = np.array(matrix)
print("\n2D Array (matrix):")
print(array_matrix)
OutputSuccess
Important Notes
Creating arrays with NumPy is faster and uses less memory than Python lists for large data.
Arrays allow you to do math on all elements at once without writing loops.
Common mistake: forgetting to import NumPy as np before creating arrays.
Summary
Arrays organize numbers for easy math and analysis.
Use np.array() to create arrays from lists.
Arrays can be empty, have one element, or many dimensions.
Practice
(1/5)
1. Why is it important to use np.array() to create arrays in NumPy instead of just using Python lists?
easy
A. Because NumPy arrays allow fast math operations on many numbers at once.
B. Because Python lists cannot store numbers.
C. Because np.array() creates strings, not numbers.
D. Because Python lists are faster than NumPy arrays.
Solution
Step 1: Understand Python lists vs NumPy arrays
Python lists store items but are slow for math on many numbers.
Step 2: Recognize NumPy arrays' advantage
NumPy arrays allow fast, efficient math on large sets of numbers.
Final Answer:
Because NumPy arrays allow fast math operations on many numbers at once. -> Option A
Quick Check:
NumPy arrays speed up math = A [OK]
Hint: NumPy arrays speed math on many numbers, lists do not [OK]
Common Mistakes:
Thinking Python lists can't store numbers
Confusing array creation with string creation
Believing lists are faster for math
2. Which of the following is the correct way to create a NumPy array from a Python list [1, 2, 3]?
easy
A. np.array{1, 2, 3}
B. array([1, 2, 3])
C. np.list([1, 2, 3])
D. np.array([1, 2, 3])
Solution
Step 1: Recall correct NumPy array syntax
The function to create arrays is np.array() with parentheses and a list inside.