Creating NumPy Arrays (Beginner Guide)
In this tutorial, you’ll learn how to create simple NumPy arrays step by step.
We’ll also see how Python lists are converted into arrays 👇
🔹 Step 1: Import NumPy
Before using NumPy, you need to import it.
import numpy as np
👉 np is just a short name for NumPy.
🔹 Step 2: Create a Simple Array
You can create a NumPy array using values inside a list.
arr = np.array([10, 20, 30, 40]) print(arr)
Output:
[10 20 30 40]
👉 This is a 1D array (simple list-like structure).
🔹 Converting Python List to NumPy Array
Step 1: Create a Python list
my_list = [5, 15, 25, 35]
Step 2: Convert it into NumPy array
import numpy as np arr = np.array(my_list) print(arr)
Output:
[ 5 15 25 35]
👉 So internally, NumPy takes list values and converts them into a faster structure.
🔹 Real-Life Scenario
Scenario: You have daily sales stored in a list.
- Day 1 → 200
- Day 2 → 500
- Day 3 → 300
👉 You convert this list into an array to perform faster calculations.
🔹 NumPy Array vs Python List
| Feature | Python List | NumPy Array |
|---|---|---|
| Speed | Slower | Faster ⚡ |
| Data Type | Mixed allowed | Same type only |
| Calculations | Manual | Quick operations |
🔹 When to Use Python List?
- Small amount of data
- Mixed values (text + numbers)
- Basic storage
Example: Names of students, shopping list
🔹 When to Use NumPy Array?
- Large numerical data
- Fast calculations needed
- Data analysis tasks
Example: Sales data, marks of students, analytics data
🔚 Final Thoughts
NumPy arrays are simple to create and very powerful.
- Convert lists easily
- Work faster
- Perfect for real-world data