0% found this document useful (0 votes)
8 views14 pages

NumPy Vs PythonLists

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views14 pages

NumPy Vs PythonLists

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

NumPy Arrays vs Python Lists

Python Lists
1. Python Lists
• A list is a built-in Python data structure.
• It can store different types of elements (integers, floats, strings,
objects).
• Example:
my_list = [1, 2, 3, "hello", 4.5]
Lists are flexible, but not optimized for numerical or mathematical
operations.
Example:
lst = [1, 2, 3, 4]
result = [x * 2 for x in lst] # Need loops for operations
print(result) # [2, 4, 6, 8]
NumPy Arrays
NumPy (Numerical Python) provides a special type of array: ndarray
(N-dimensional array).
• Unlike lists, NumPy arrays:
– Store only one data type (all integers, or all floats, etc.).
– Are stored in continuous memory, making them much faster.
– Support vectorized operations (apply operations directly
without loops).
Example:
import numpy as np
arr = np.array([1, 2, 3, 4])
print(arr * 2) # [2 4 6 8]
Key Differences
• Python List vs NumPy Array

Feature Python List NumPy Array


Data Types Mixed types allowed Same type only
Speed Slower (uses Python loops) Much faster (C-based)
Memory More memory usage Less memory (compact)
Math Operations Manual (loops required) Direct & vectorized
Dimensions 1D (list of lists for 2D) Supports multi-D arrays
Example Comparison
# Python List
lst = [1, 2, 3, 4]
lst_double = [x*2 for x in lst]
print(lst_double) # [2, 4, 6, 8]

# NumPy Array
import numpy as np
arr = np.array([1, 2, 3, 4])
print(arr * 2) # [2 4 6 8]

•Lists: Loop required for math.


•Arrays: Direct operations.
Array Creation
• import numpy as np

• # From Python list


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

• # 2D Array (list of lists)


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

• # Using built-in functions


zeros = np.zeros((2, 3)) # 2x3 array of 0s
ones = np.ones((3, 2)) # 3x2 array of 1s
rand = np.random.rand(2, 2) # Random numbers between 0 and 1
ar = np.arange(0, 10, 2) # [0 2 4 6 8]
lin = np.linspace(0, 1, 5) # 5 numbers between 0 and 1
Indexing
Like lists, but more powerful.

arr = np.array([10, 20, 30, 40, 50])

print(arr[0]) # 10 (first element)


print(arr[-1]) # 50 (last element)

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

print(arr2d[0, 0]) # 1 (first row, first col)


print(arr2d[2, 1]) # 8 (third row, second col)
Slicing
Works similar to Python lists but extends to multiple
dimensions
arr = np.array([10, 20, 30, 40, 50])

print(arr[1:4]) # [20 30 40]


print(arr[:3]) # [10 20 30]
print(arr[::2]) # [10 30 50] (step=2)

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

print(arr2d[0:2, 1:3])
# [[2 3]
# [5 6]]
Reshaping
Change the shape (rows × columns) without changing the data
arr = np.arange(1, 9) # [1 2 3 4 5 6 7 8]

reshaped = arr.reshape(2, 4) # 2 rows, 4 cols


print(reshaped)
# [[1 2 3 4]
# [5 6 7 8]]

reshaped2 = arr.reshape(4, 2) # 4 rows, 2 cols


print(reshaped2)
# [[1 2]
# [3 4]
# [5 6]
# [7 8]]

# -1 lets NumPy figure out dimension automatically


reshaped_auto = arr.reshape(-1, 4)
print(reshaped_auto)
# [[1 2 3 4]
# [5 6 7 8]]
Performing arithmetic operations across arrays
1. Element-wise Arithmetic
If two arrays have the same shape, NumPy performs
operations element by element

import numpy as np

a = np.array([10, 20, 30, 40])


b = np.array([1, 2, 3, 4])

print(a + b) # [11 22 33 44]


print(a - b) # [ 9 18 27 36]
print(a * b) # [10 40 90 160]
print(a / b) # [10. 10. 10. 10.]
2. Scalar Operations
You can add, subtract, multiply, or divide arrays with
a single number (scalar)

arr = np.array([1, 2, 3, 4])

print(arr + 5) # [6 7 8 9]
print(arr * 2) # [2 4 6 8]
print(arr - 1) # [0 1 2 3]
print(arr / 2) # [0.5 1. 1.5 2. ]
3. Multi-Dimensional Arrays
Operations extend naturally to 2D and beyond

x = np.array([[1, 2], [3, 4]])

y = np.array([[5, 6], [7, 8]])

print(x + y)
# [[ 6 8]
# [10 12]]

print(x * y) # element-wise product


# [[ 5 12]
# [21 32]]
4. Broadcasting
If arrays have different shapes, NumPy automatically “broadcasts” the
smaller one to match

arr = np.array([[1, 2, 3],


[4, 5, 6]])

# Add scalar (broadcasted to every element)


print(arr + 10)
# [[11 12 13]
# [14 15 16]]

# Add 1D array to 2D array (row-wise broadcasting)


b = np.array([1, 2, 3])
print(arr + b)
# [[2 4 6]
# [5 7 9]]
5. Mathematical Functions
NumPy provides fast element-wise math
functions

arr = np.array([1, 2, 3, 4, 5])

print(np.sqrt(arr)) # [1. 1.41 1.73 2. 2.23]


print(np.exp(arr)) # e^x values
print(np.log(arr)) # natural log
print(np.sin(arr)) # sine values

You might also like