</>
Skip to content
NumPy lessons (5/28)

NumPy — Arrays

Create arrays

import numpy as np

# From list
arr = np.array([1, 2, 3, 4, 5])
print(arr)

# 2D array
arr_2d = np.array([[1, 2, 3], [4, 5, 6]])
print(arr_2d)

# 3D array
arr_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])

Data types

# Specify dtype
arr = np.array([1, 2, 3], dtype=np.float32)
print(arr.dtype)

# Convert types
arr_int = arr.astype(np.int32)

Array from function

# Using fromfunction
def my_func(i, j):
    return i + j

arr = np.fromfunction(my_func, (3, 3))
print(arr)

Empty arrays

# Empty (uninitialized)
empty = np.empty((3, 3))

# Zeros
zeros = np.zeros((3, 3))

# Ones
ones = np.ones((3, 3))

# Full
full = np.full((3, 3), 7)

Mini Practice

  1. Create 1D, 2D, 3D arrays
  2. Specify data types
  3. Create empty/zero/one arrays
  4. Use fromfunction

Up Next

Continue with Array Creation - Different creation methods.

Related Topics

Frequently Asked Questions about Arrays

What is Arrays in NumPy?

Arrays is a fundamental concept in NumPy. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Arrays?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Arrays.

Why is Arrays important in NumPy?

Arrays is essential for NumPy development. Understanding this concept will help you write better code and solve real-world problems more effectively.