Lists, Arrays, Numpy Basics
Lists, Arrays, Numpy Basics
Lists, Arrays, Numpy Basics
ipynb - Colab
اللهم ال سهل إال ما جعلته سهال وأنت تجعل الصعب إن شئت سهال
Ordered/ Indexing
Mutable
Heterogeneous >> Store Different Data Types
Dynamic >> Size is changable >> Lists can grow or shrink as needed
# OR you can skip the step of initialization and put elements direct <as u like>
my_list1 = ['Ahmed', 'Sara', 1, 303]
my_list1
''' Why we used douple parentheses in the previous step not just only one??
Try to code this and check the output:
https://colab.research.google.com/drive/1iMtDnMQhTzqanKqjr0NtF7MgUa47dQnk#scrollTo=jx… 1/21
02/01/2025, 05:22 NumPy.ipynb - Colab
my_list2 = list('Ahmed', 'Sara', 1, 303)
'''
' Why we used douple parentheses in the previous step not just only one??\nTry to cod
''' Now let's explore this line >> my_list2 = list(('Ahmed', 'Sara', 1, 303))
1. The inner parentheses () define a tuple ('Ahmed', 'Sara', 1, 303)
2. The list() constructor takes this tuple as a single argument
3. It converts the tuple into a list: ['Ahmed', 'Sara', 1, 303]'''
' Now let's explore this line >> my_list2 = list(('Ahmed', 'Sara', 1, 303))\n1. The i
nner parentheses () define a tuple ('Ahmed' 'Sara' 1 303)\n2 The list() construct
[1, 2, 3, 4]
b
d
https://colab.research.google.com/drive/1iMtDnMQhTzqanKqjr0NtF7MgUa47dQnk#scrollTo=jx… 2/21
02/01/2025, 05:22 NumPy.ipynb - Colab
# my_list6[0:4] Instead of write that to access from the 1st element u can use the next li
my_list6[:4] # >> That will access from element with Index 0 to the element with Index 3
my_list6[2:] # >> That will access from element with Index 2 to the last element
my_list7.extend([11, 'g'])
my_list7
my_list7.insert(2, 'a') # Insert Ahmed at the index 2 and shift the other elements that wa
my_list7
['a', 'b', 'a', 'c', 0, 'd', 101, 'e', 'False', 11, 'g']
https://colab.research.google.com/drive/1iMtDnMQhTzqanKqjr0NtF7MgUa47dQnk#scrollTo=jx… 3/21
02/01/2025, 05:22 NumPy.ipynb - Colab
my_list7.remove('a') # Removes the first occurrence of the value 'a'
my_list7
l0 = my_list7.pop(0) # >> What should be the value of the variable l0? what had been stored
l0
'b'
my_list7
[]
5. Operations on lists
He put the 1st list elements, then the second one elements
[1, 2, 3, 1, 2, 3, 1, 2, 3]
4
46
68
134
176
200
https://colab.research.google.com/drive/1iMtDnMQhTzqanKqjr0NtF7MgUa47dQnk#scrollTo=jx… 5/21
02/01/2025, 05:22 NumPy.ipynb - Colab
# 2. Using enumerate() function
avengers = ['Iron Man', 'Captain America', 'Thor', 'Hulk']
for index, value in enumerate(avengers):
print(index, value)
0 Iron Man
1 Captain America
2 Thor
3 Hulk
1 Iron Man
2 Captain America
3 Thor
4 Hulk
<class 'enumerate'>
e_list = list(e)
print(e_list)
[(0, 'Iron Man'), (1, 'Captain America'), (2, 'Thor'), (3, 'Hulk')]
# 3. Using zip() function >> this function takes the 1st element from the 1st list with th
avengers = ['Iron Man', 'Captain America', 'Thor', 'Hulk']
names = ['Tony Stark', 'Steve Rogers', 'Thor Odinson', 'Bruce Banner']
z = zip(avengers, names)
print(type(z))
<class 'zip'>
z_list = list(z)
print(z_list)
[('Iron Man', 'Tony Stark'), ('Captain America', 'Steve Rogers'), ('Thor', 'Thor Odins
https://colab.research.google.com/drive/1iMtDnMQhTzqanKqjr0NtF7MgUa47dQnk#scrollTo=jx… 6/21
02/01/2025, 05:22 NumPy.ipynb - Colab
# Print zip with * >> the splat operator to print all the elements!
avengers = ['Iron Man', 'Captain America', 'Thor', 'Hulk']
names = ['Tony Stark', 'Steve Rogers', 'Thor Odinson', 'Bruce Banner']
z = zip(avengers, names)
print(*z)
('Iron Man', 'Tony Stark') ('Captain America', 'Steve Rogers') ('Thor', 'Thor Odinson'
# Arrays support many operations, similar to lists but are limited to homogeneous data.
# 2.1 Adding elements to an array
int_arr.append(4) # Adding single element by append
int_arr
int_arr.extend([5, 6]) # Adding multiple elements or you can say that you add array to the
int_arr
int_arr
int_arr[:3] # >> slice/ select elements from the element that has index 0 to the element t
# Doesn't take the element in index 3 in consider
int_arr[2:] # >> slice/ select elements from the element that has index 2 to the last elem
https://colab.research.google.com/drive/1iMtDnMQhTzqanKqjr0NtF7MgUa47dQnk#scrollTo=jx… 8/21
02/01/2025, 05:22 NumPy.ipynb - Colab
# 2.4 Changing array's element
int_arr[0] = -1 # changing the value of the 1st element to be -1 instead of 1
int_arr
int_arr[1:] = arr.array('i', [-2, -3, -4]) # changing the value of multiple elements
int_arr
Numpy
Speed: Numpy is very fast and python lists are very slow. ::: NumPy operations are
implemented in C, enabling faster execution compared to native Python loops and
lists.
Memory Efficient: NumPy arrays are more memory-efficient than Python lists
because they store data in contiguous memory locations and use a fixed data type
for all elements.
1. Linear algebra
2. Statistical analysis (mean, median, mode)
3. Mathematical functions
Python lists are primarily support 1D arrays ::: At the other side Numpy supports
multi-dimensional arrays, enabling powerful operations on matrices and tensors.
NumPy is the backbone of many other Python libraries, like: Pandas, SciPy, Scikit-
learn
https://colab.research.google.com/drive/1iMtDnMQhTzqanKqjr0NtF7MgUa47dQnk#scrollTo=jx… 9/21
02/01/2025, 05:22 NumPy.ipynb - Colab
array([1, 2, 3, 4, 5])
# 1.2 2D Array
c = np.array([[1, 2, 3], [4, 5, 6]])
c
array([[1, 2, 3],
[4, 5, 6]])
array([[1],
[2],
[3],
[4],
[5]])
Even if there is only one column, as long as there are multiple dimensions, it is considered 2D.
[ [1],
[2],
[3],
[4],
[5] ]
array([[[ 1, 2, 3],
[ 4, 5, 6]],
(2, 2, 3)
https://colab.research.google.com/drive/1iMtDnMQhTzqanKqjr0NtF7MgUa47dQnk#scrollTo=j… 10/21
02/01/2025, 05:22 NumPy.ipynb - Colab
array([1, 3, 5, 7, 9])
array([[[1, 1],
[1, 1]],
[[1, 1],
[1, 1]],
[[1, 1],
[1, 1]],
[[1, 1],
[1, 1]]], dtype=int32)
array([[99, 99],
[99, 99]])
array([[1, 2, 3],
[4, 5, 6]])
https://colab.research.google.com/drive/1iMtDnMQhTzqanKqjr0NtF7MgUa47dQnk#scrollTo=j… 11/21
02/01/2025, 05:22 NumPy.ipynb - Colab
array([[4, 4, 4],
[4, 4, 4]])
# Another way!
np.full(c.shape, 4)
array([[4, 4, 4],
[4, 4, 4]])
array([[[0.55595524, 0.99336253],
[0.0704707 , 0.95317059]],
[[0.74502318, 0.42871848],
[0.97169651, 0.65316088]],
[[0.70029031, 0.13333858],
[0.71847773, 0.08011527]],
[[0.52686587, 0.29535199],
[0.46681343, 0.90753534]]])
array([[1, 1, 2],
[4, 3, 1],
[3, 0, 2]])
array([[5, 5, 5],
[5, 5, 5],
[5, 4, 5]])
# Identity matrix
np.identity(5)
https://colab.research.google.com/drive/1iMtDnMQhTzqanKqjr0NtF7MgUa47dQnk#scrollTo=j… 12/21
02/01/2025, 05:22 NumPy.ipynb - Colab
# Repeat array
arr = np.array([[1, 2, 3]])
r1 = np.repeat(arr, 3, axis = 0) # >> repeat arr on 0(x) axis 3 times
print(r1)
[[1 2 3]
[1 2 3]
[1 2 3]]
[[1 1 1 2 2 2 3 3 3]]
(5,)
The output of (num,) represents the output of shape 1D array, num indicates number of elements
in the array.
# For 2D array
b.shape
(5, 1)
c.shape # 2D array
(2, 3)
(2, 2, 3)
a.dtype
dtype('int64')
dtype('int16')
As we set the dtype of a int16: itemsize is the size of a single array element in
bytes.
10
10
https://colab.research.google.com/drive/1iMtDnMQhTzqanKqjr0NtF7MgUa47dQnk#scrollTo=j… 14/21
02/01/2025, 05:22 NumPy.ipynb - Colab
x = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
x
array([[ 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10]])
10
array([1, 2, 3, 4, 5])
array([ 5, 10])
array([ 6, 8, 10])
array([[ 1, 2, 3, 4, -1],
[ 6, 7, 8, 9, 10]])
array([[ 1, 2, 0, 4, -1],
[ 6, 7, 0, 9, 10]])
With 3D
y = np.array([[[1, 2, 3], [4, 5, 6]], [[-1, -2, -3], [-4, -5, -6]]])
y
array([[[ 1, 2, 3],
[ 4, 5, 6]],
https://colab.research.google.com/drive/1iMtDnMQhTzqanKqjr0NtF7MgUa47dQnk#scrollTo=j… 15/21
02/01/2025, 05:22 NumPy.ipynb - Colab
# 6.A Get a specific element
y[0, 1, 2] # >> 1st matrix, 2nd row, 3rd column
array([[ 4, 5, 6],
[-4, -5, -6]])
array([ 1, -1])
array([[ 4, 5, 6],
[-4, -5, -6]])
array([[[ 1, 2, 3],
[ 9, 9, 9]],
array([1, 2, 3])
b[0] = 100
b
array([100, 2, 3])
array([100, 2, 3])
a = np.array([1, 2, 3])
b = a.copy()
b
https://colab.research.google.com/drive/1iMtDnMQhTzqanKqjr0NtF7MgUa47dQnk#scrollTo=j… 16/21
02/01/2025, 05:22 NumPy.ipynb - Colab
array([1, 2, 3])
b[0] = 100
b
array([100, 2, 3])
array([1, 2, 3])
keyboard_arrow_down 8. Mathematics
a = np.array([1, 2, 3, 4])
a
array([1, 2, 3, 4])
a +=2
a
array([3, 4, 5, 6])
a -=2
a
array([1, 2, 3, 4])
a*=2
a
array([2, 4, 6, 8])
a / 2
a
array([2, 4, 6, 8])
a ** 2
# Take sin
np.sin(a)
https://colab.research.google.com/drive/1iMtDnMQhTzqanKqjr0NtF7MgUa47dQnk#scrollTo=j… 17/21
02/01/2025, 05:22 NumPy.ipynb - Colab
# Take cos
np.cos(a)
b = np.array([1, 0, 1, 0])
a + b
array([3, 4, 7, 8])
b = np.full((3, 2), 2)
print(b)
[[1. 1. 1.]
[1. 1. 1.]]
[[2 2]
[2 2]
[2 2]]
# Multiply a and b
np.matmul(a, b)
array([[6., 6.],
[6., 6.]])
array([[6., 6.],
[6., 6.]])
1.0
a = np.array([1, 2, 3])
norm1 = norm(a, 1) # 1 is for l1 norm >> calc its sum of the absolute values
norm1
6.0
https://colab.research.google.com/drive/1iMtDnMQhTzqanKqjr0NtF7MgUa47dQnk#scrollTo=j… 18/21
02/01/2025, 05:22 NumPy.ipynb - Colab
6.0
norm2 = norm(a, 2) # 2 is for l2 norm >> calc its distance from the origin, it cal by squa
norm2
3.7416573867739413
3.7416573867739413
array([[1, 2, 3],
[4, 5, 6]])
np.min(stats)
np.min(stats, axis = 0)
array([1, 2, 3])
np.min(stats, axis = 1)
array([1, 4])
np.max(stats)
np.max(stats, axis = 0)
https://colab.research.google.com/drive/1iMtDnMQhTzqanKqjr0NtF7MgUa47dQnk#scrollTo=j… 19/21
02/01/2025, 05:22 NumPy.ipynb - Colab
array([4, 5, 6])
np.sum(stats)
21
np.sum(stats, axis = 0)
array([5, 7, 9])
array([[1, 2, 3, 4],
[5, 6, 7, 8]])
array([[[1, 2],
[3, 4]],
[[5, 6],
[7, 8]]])
# Horizontal Stacking
v1 = np.array([1, 2, 3, 4])
v2 = np.array([5, 6, 7, 8])
array([1, 2, 3, 4, 5, 6, 7, 8, 5, 6, 7, 8])
# Vertical Stacking
v1 = np.array([1, 2, 3, 4])
v2 = np.array([5, 6, 7, 8])
https://colab.research.google.com/drive/1iMtDnMQhTzqanKqjr0NtF7MgUa47dQnk#scrollTo=j… 20/21
02/01/2025, 05:22 NumPy.ipynb - Colab
array([[1, 2, 3, 4],
[5, 6, 7, 8],
[5, 6, 7, 8],
[1, 2, 3, 4]])
References:
W3scools
GeeksforGeeks
https://colab.research.google.com/drive/1iMtDnMQhTzqanKqjr0NtF7MgUa47dQnk#scrollTo=j… 21/21