🎯 Your First Array

Welcome to the exciting world of NumPy arrays! Creating your first array is like taking your first step into high-performance numerical computing. NumPy arrays are the foundation for all scientific computing in Python - once you master them, you'll have the power to process data at lightning speed.

Let's create some arrays and see the magic happen!

import numpy as np

# Your very first NumPy array!
my_first_array = np.array([1, 2, 3, 4, 5])
print("🎉 My first NumPy array:")
print(my_first_array)
print(f"Type: {type(my_first_array)}")

# See the power - multiply entire array instantly!
doubled = my_first_array * 2
print(f"Doubled: {doubled}")

# Mathematical operations are incredibly easy
sum_all = np.sum(my_first_array)
mean_value = np.mean(my_first_array)
print(f"Sum: {sum_all}")
print(f"Mean: {mean_value}")

🛠️ Creating Arrays from Lists

The most common way to create arrays is from Python lists:

import numpy as np

# Different ways to create arrays from lists
integers = np.array([1, 2, 3, 4, 5])
floats = np.array([1.1, 2.2, 3.3, 4.4, 5.5])
mixed = np.array([1, 2.5, 3, 4.0])  # Gets converted to float

print("Integer array:", integers)
print("Float array:", floats)
print("Mixed array (converted to float):", mixed)
print()

# Check the data types
print(f"Integers dtype: {integers.dtype}")
print(f"Floats dtype: {floats.dtype}")
print(f"Mixed dtype: {mixed.dtype}")

📊 Creating 2D Arrays (Matrices)

2D arrays are like tables or spreadsheets - perfect for structured data:

import numpy as np

# Create a 2D array (matrix) from nested lists
matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print("2D array (matrix):")
print(matrix)
print(f"Shape: {matrix.shape}")  # (rows, columns)
print()

# A smaller example - like a data table
student_scores = np.array([
    [85, 92, 78],  # Student 1: Math, Science, English
    [91, 87, 94],  # Student 2: Math, Science, English
    [76, 89, 82]   # Student 3: Math, Science, English
])

print("Student scores:")
print(student_scores)
print(f"Shape: {student_scores.shape}")  # 3 students, 3 subjects

🎲 Built-in Array Creation Functions

NumPy provides convenient functions to create common array patterns:

import numpy as np

# Arrays of zeros and ones
zeros = np.zeros(5)
ones = np.ones(4)
print("Zeros:", zeros)
print("Ones:", ones)
print()

# Sequential numbers
sequence1 = np.arange(6)        # 0 to 5
sequence2 = np.arange(1, 8)     # 1 to 7
sequence3 = np.arange(0, 10, 2) # 0 to 10, step by 2

print("Sequence 0-5:", sequence1)
print("Sequence 1-7:", sequence2)
print("Even numbers 0-10:", sequence3)
print()

# Evenly spaced numbers
even_spaced = np.linspace(0, 1, 5)  # 5 numbers from 0 to 1
print("Evenly spaced 0-1:", even_spaced)

2D Array Creation Functions

import numpy as np

# 2D arrays with built-in functions
zeros_2d = np.zeros((3, 4))     # 3 rows, 4 columns of zeros
ones_2d = np.ones((2, 3))       # 2 rows, 3 columns of ones
identity = np.eye(3)            # 3×3 identity matrix

print("3×4 zeros:")
print(zeros_2d)
print()

print("2×3 ones:")
print(ones_2d)
print()

print("3×3 identity matrix:")
print(identity)

🔍 Examining Your Arrays

Once you create arrays, you'll want to inspect their properties:

import numpy as np

# Create a sample array to examine
data = np.array([[10, 20, 30], [40, 50, 60]])

print("Sample array:")
print(data)
print()

# Examine properties
print(f"Shape: {data.shape}")      # (2, 3) - 2 rows, 3 columns
print(f"Size: {data.size}")        # 6 total elements
print(f"Data type: {data.dtype}")  # int32 or int64
print(f"Dimensions: {data.ndim}")  # 2 dimensions
print(f"Length: {len(data)}")      # 2 (first dimension)
print()

# Basic statistics
print(f"Minimum: {data.min()}")
print(f"Maximum: {data.max()}")
print(f"Mean: {data.mean()}")
print(f"Sum: {data.sum()}")

⚡ Basic Array Operations

Arrays make mathematical operations incredibly easy:

import numpy as np

# Create sample arrays
arr1 = np.array([1, 2, 3, 4])
arr2 = np.array([10, 20, 30, 40])

print("Array 1:", arr1)
print("Array 2:", arr2)
print()

# Mathematical operations
addition = arr1 + arr2
subtraction = arr2 - arr1
multiplication = arr1 * arr2
division = arr2 / arr1

print("Addition:", addition)
print("Subtraction:", subtraction)
print("Multiplication:", multiplication)
print("Division:", division)
print()

# Operations with single numbers (broadcasting)
doubled = arr1 * 2
plus_ten = arr1 + 10

print("Doubled:", doubled)
print("Plus 10:", plus_ten)

🎮 Your Turn: Create Arrays

Let's practice creating different types of arrays:

import numpy as np

# Example solutions (try your own first!)
print("Practice Examples:")
print()

# 1. Personal data
personal = np.array([25, 175, 70])  # age, height, weight
print("Personal data [age, height, weight]:", personal)

# 2. Weekly temperatures
temperatures = np.array([22, 25, 23, 21, 24, 26, 20])
print("Weekly temperatures:", temperatures)

# 3. Grade matrix (3 students, 4 subjects)
grades = np.array([
    [85, 92, 78, 89],  # Student 1
    [91, 87, 94, 83],  # Student 2  
    [76, 89, 82, 95]   # Student 3
])
print("Grade matrix:")
print(grades)

# 4. Countdown
countdown = np.arange(10, 0, -1)
print("Countdown:", countdown)

# 5. Even numbers
even_numbers = np.arange(2, 21, 2)
print("First 10 even numbers:", even_numbers)

🌟 Array vs List Comparison

See the difference between arrays and lists in action:

import numpy as np

# Compare list vs array operations
python_list = [1, 2, 3, 4, 5]
numpy_array = np.array([1, 2, 3, 4, 5])

print("Python list:", python_list)
print("NumPy array:", numpy_array)
print()

# Multiplying by 2
print("Multiply by 2:")
print("List approach:", [x * 2 for x in python_list])  # Need loop
print("Array approach:", numpy_array * 2)               # Vectorized!
print()

# Adding arrays/lists
list2 = [10, 20, 30, 40, 50]
array2 = np.array([10, 20, 30, 40, 50])

print("Adding:")
print("Arrays:", numpy_array + array2)    # Element-wise addition
print("Lists:", python_list + list2)      # Concatenation!

🎯 Key Takeaways

🚀 What's Next?

Excellent! You've created your first NumPy arrays and seen their power. Now let's dive deeper into understanding exactly why NumPy arrays are so much better than regular Python lists.

Continue to: NumPy vs Python Lists

You're building real numerical computing skills! 🔢⚡

Was this helpful?

😔Poor
🙁Fair
😊Good
😄Great
🤩Excellent