7 Collections
7 Collections
STRUCTURES
2
2.1 Variables and Assignment ........................................................................................... 27
2.2 Data Structure – String............................................................................................... 30
2.3 Data Structure – List.................................................................................................. 35
2.4 Data Structure – Tuple ............................................................................................... 38
2.5 Data Structure – Set .................................................................................................. 40
2.6 Data Structure – Dictionary.......................................................................................... 42
2.7 Introducing NumPy Arrays ............................................................................................ 44
2.8 Summary and Problems .............................................................................................. 52
2.8.1 Summary ............................................................................................... 52
2.8.2 Problems ............................................................................................... 52
In [1]: x = 1
x
Out[1]: 1
TRY IT! Assign the value 2 to the variable y. Multiply y by 3 to show that it behaves like the
value 2.
In [2]: y = 2
y
Out[2]: 2
In [3]: y*3
28 CHAPTER 2 VARIABLES AND BASIC DATA STRUCTURES
Out[3]: 6
A variable is like a “container” used to store the data in the computer’s memory. The name of the
variable tells the computer where to find this value in the memory. For now, it is sufficient to know
that the notebook has its own memory space to store all the variables in the notebook. As a result of
the previous example, you will see the variables x and y in the memory. You can view a list of all the
variables in the notebook using the magic command %whos (magic commands are a specialized set of
commands host by the IPython kernel and require the prefix % to specify the commands).
In [4]: %whos
Note! The equality sign in programming is not the same as a truth statement in mathematics. In
math, the statement x = 2 declares the universal truth within the given framework, x is 2. In program-
ming, the statement x=2 means a known value is being associated with a variable name, store 2 in
x. Although it is perfectly valid to say 1 = x in mathematics, assignments in Python always go left,
meaning the value to the right of the equal sign is assigned to the variable on the left of the equal sign.
Therefore, 1=x will generate an error in Python. The assignment operator is always last in the order of
operations relative to mathematical, logical, and comparison operators.
TRY IT! The mathematical statement x = x + 1 has no solution for any value of x. In program-
ming, if we initialize the value of x to be 1, then the statement makes perfect sense. It means, “Add
x and 1, which is 2, then assign that value to the variable x”. Note that this operation overwrites
the previous value stored in x.
In [5]: x = x + 1
x
Out[5]: 2
There are some restrictions on the names variables can take. Variables can only contain alphanu-
meric characters (letters and numbers) as well as underscores; however, the first character of a variable
name must be a letter or an underscore. Spaces within a variable name are not permitted, and the
variable names are case sensitive (e.g., x and X are considered different variables).
2.1 VARIABLES AND ASSIGNMENT 29
TIP! Unlike in pure mathematics, variables in programming almost always represent something
tangible. It may be the distance between two points in space or the number of rabbits in a popu-
lation. Therefore, as your code becomes increasingly complicated, it is very important that your
variables carry a name that can easily be associated with what they represent. For example, the
distance between two points in space is better represented by the variable dist than x, and the
number of rabbits in a population is better represented by n_rabbits than y.
Note that when a variable is assigned, it has no memory of how it was assigned. That is, if the value
of a variable, y, is constructed from other variables, like x, reassigning the value of x will not change
the value of y.
EXAMPLE: What value will y have after the following lines of code are executed?
In [7]: x = 1
y = x + 1
x = 2
y
Out[7]: 2
WARNING! You can overwrite variables or functions that have been stored in Python. For ex-
ample, the command help = 2 will store the value 2 in the variable with name help. After this
assignment help will behave like the value 2 instead of the function help. Therefore, you should
always be careful not to give your variables the same name as built-in functions or values.
TIP! Now that you know how to assign variables, it is important that you remember to never
leave unassigned commands. An unassigned command is an operation that has a result, but that
result is not assigned to a variable. For example, you should never use 2+2. You should instead
assign it to some variable x=2+2. This allows you to “hold on” to the results of previous commands
and will make your interaction with Python much less confusing.
You can clear a variable from the notebook using the del function. Typing del x will clear the
variable x from the workspace. If you want to remove all the variables in the notebook, you can use the
magic command %reset.
In mathematics, variables are usually associated with unknown numbers; in programming, variables
are associated with a value of a certain type. There are many data types that can be assigned to variables.
A data type is a classification of the type of information that is being stored in a variable. The basic
data types that you will utilize throughout this book are boolean, int, float, string, list, tuple, dictionary,
and set. A formal description of these data types is given in the following sections.
30 CHAPTER 2 VARIABLES AND BASIC DATA STRUCTURES
TRY IT! Assign the character "S" to the variable with name s. Assign the string "Hello World"
to the variable w. Verify that s and w have the type string using the type function.
In [2]: s = "S"
w = "Hello World"
In [3]: type(s)
Out[3]: str
In [4]: type(w)
Out[4]: str
Note! A blank space, " ", between "Hello" and "World" is also a type str. Any symbol can be a
character, even those that have been reserved for operators. Note that as a str, they do not perform the
same function. Although they look the same, Python interprets them completely differently.
TRY IT! Create an empty string. Verify that the empty string is an str.
Out[5]: str
Because a string is an array of characters, it has length to indicate the size of the string. For example,
we can check the size of the string by using the built-in function len.
2.2 DATA STRUCTURE – STRING 31
FIGURE 2.1
String index for the example of "Hello World".
In [6]: len(w)
Out[6]: 11
Strings also have indexes that enables us to find the location of each character, as shown in Fig. 2.1.
The index of the position start with 0.
We can access a character by using a bracket and the index of the position. For example, if we want
to access the character "W", then we type the following:
In [7]: w[6]
Out[7]: "W"
We can also select a sequence as well using string slicing. For example, if we want to access
"World", we type the following command.
In [8]: w[6:11]
Out[8]: "World"
[6:11] means the start position is from index 6 and the end position is index 10. In the Python
string slicing range, the upper-bound is exclusive; this means that [6:11] will “slice” the characters
from index 6 to 10. The syntax for slicing in Python is [start:end:step], the third argument, step, is
optional. If you ignore the step argument, the default will be set to 1.
You can ignore the end position if you want to slice to the end of the string. For example, the
following command is the same as the above one:
In [9]: w[6:]
Out[9]: "World"
In [10]: w[:5]
Out[10]: "Hello"
32 CHAPTER 2 VARIABLES AND BASIC DATA STRUCTURES
You can also use a negative index when slicing the strings, which means counting from the end of
the string. For example, -1 means the last character, -2 means the second to last and so on.
In [11]: w[6:-2]
Out[11]: "Wor"
In [12]: w[::2]
Out[12]: "HloWrd"
TRY IT! Use "+" to add two numbers. Verify that "+" does not behave like the addition operator,
+.
In [13]: 1 "+" 2
WARNING! Numbers can also be expressed as str. For example, x = '123' means that x is the
string 123 not the number 123. However, strings represent words or text and so should not have
addition defined on them.
TIP! You may find yourself in a situation where you would like to use an apostrophe as an str.
This is problematic since an apostrophe is used to denote strings. Fortunately, an apostrophe can
be used in a string in the following way. The backslash (\) is a way to tell Python this is part of the
string, not to denote strings. The backslash character is used to escape characters that otherwise
have a special meaning, such as newline, backslash itself, or the quote character. If either single
or double quote is a part of the string itself, in Python, there is an easy way to do this, you can
just place the string in double or single quotes respectively, as shown in the following example.
In [14]: "don't"
Out[14]: "don't"
We can convert other data types to strings as well using the built-in function str. This is useful, for
example, we have the variable x which has stored 1 as an integer type, if we want to print it out directly
as a string, we will get an error saying we cannot concatenate string with an integer.
In [16]: x = 1
print("x = " + x)
-------------------------------------------------------
<ipython-input-16-3e562ba0dd83> in <module>()
1 x = 1
----> 2 print("x = " + x)
The correct way to do it is to convert the integer to string first, and then print it out.
x = 1
In [18]: type(str(x))
Out[18]: str
In Python, string is an object that has various methods that can be used to manipulate it (this is
the so-called object-oriented programming and will be discussed later). To get access to these various
methods, use this pattern string.method_name.
34 CHAPTER 2 VARIABLES AND BASIC DATA STRUCTURES
In [19]: w.upper()
In [20]: w.count("l")
Out[20]: 3
There are different ways to preformat a string. Here we introduce two ways to do it. For example,
if we have two variables name and country, and we want to print them out in a sentence, but we do not
want to use the string concatenation we used before since it will use many "+" signs in the string, we
can do the following instead:
WHAT IS HAPPENING? In the previous example, the %s in the double quotation marks is
telling Python that we want to insert some strings at this location (s stands for string in this case).
The %(name, country) is the location where the two strings should be inserted.
NEW! There is a different way that only introduced in Python 3.6 and above, it is called f-string,
which means formated-string. You can easily format a string with the following line:
You can even print out a numerical expression without converting the data type as we did before.
2.3 DATA STRUCTURE – LIST 35
TRY it! Print out the result of 3*4 directly using f-string.
In [24]: print(f"{3*4}")
12
By this point, we have learned about the string data structure; this is our first sequence data structure.
Let us learn more now.
Out[1]: [1, 2, 3]
The way to retrieve the element in the list is very similar to how it is done for strings, see Fig. 2.2
for the index of a string.
36 CHAPTER 2 VARIABLES AND BASIC DATA STRUCTURES
FIGURE 2.2
Example of list index.
In [5]: list_3[2]
Out[5]: 3
In [6]: list_3[:3]
Out[6]: [1, 2, 3]
In [7]: list_3[-1]
Out[7]: "orange"
In [8]: list_4[0]
Out[8]: [1, 2, 3]
Similarly, we can obtain the length of the list by using the len function.
In [9]: len(list_3)
Out[9]: 5
New items can be added to an existing list by using the append method from the list.
In [11]: list_1.append(4)
list_1
Out[11]: [1, 2, 3, 4]
Note! The append function operate on the list itself as shown in the above example, 4 is added
to the list. But in the list_1 + list_2 example, list_1 and list_2 will not change. You can
check list_2 to verify this.
We can also insert or remove element from the list by using the methods insert and remove, but
they are also operating on the list directly.
In [12]: list_1.insert(2,"center")
list_1
Note! Using the remove method will only remove the first occurrence of the item (read the
documentation of the method). There is another way to delete an item by using its index – function
del.
Out[13]: [1, 2, 3, 4]
We can also define an empty list and add in new element later using the append method. It is used a
lot in Python when you have to loop through a sequence of items; we will learn more about this method
in Chapter 5.
TRY IT! Define an empty list and add values 5 and 6 to the list.
In [14]: list_5 = []
list_5.append(5)
list_5
Out[14]: [5]
In [15]: list_5.append(6)
list_5
38 CHAPTER 2 VARIABLES AND BASIC DATA STRUCTURES
Out[15]: [5, 6]
We can also quickly check if an element is in the list using the operator in.
In [16]: 5 in list_5
Out[16]: True
Using the list function, we can turn other sequence items into a list.
TRY IT! Turn the string "Hello World" into a list of characters.
Out[17]: ["H", "e", "l", "l", "o", " ", "W", "o", "r", "l", "d"]
Lists are used frequently in Python when working with data, with many different possible applica-
tions as discussed in later sections.
Out[1]: (1, 2, 3, 2)
As with strings and lists, there is a way to index tuples, slicing the elements, and even some methods
are very similar to those we saw before.
In [2]: len(tuple_1)
Out[2]: 4
2.4 DATA STRUCTURE – TUPLE 39
In [3]: tuple_1[1:4]
Out[3]: (2, 3, 2)
In [4]: tuple_1.count(2)
Out[4]: 2
You may ask, what is the difference between lists and tuples? If they are similar to each other, why
do we need another sequence data structure?
Tuples are created for a reason. From the Python documentation1 :
Though tuples may seem similar to lists, they are often used in different situations and for different pur-
poses. Tuples are immutable, and usually contain a heterogeneous sequence of elements that are accessed
via unpacking (see later in this section) or indexing (or even by attribute in the case of named tuples).
Lists are mutable, and their elements are usually homogeneous and are accessed by iterating over the
list.
What does it mean by immutable? It means the elements in the tuple, once defined, cannot be
changed. In contrast, elements in a list can be changed without any problem. For example,
Out[5]: [1, 2, 1]
In [6]: tuple_1[2] = 1
-------------------------------------------------------
<ipython-input-6-76fb6b169c14> in <module>()
----> 1 tuple_1[2] = 1
1 https://docs.python.org/3/tutorial/datastructures.html#tuples-and-sequences.
40 CHAPTER 2 VARIABLES AND BASIC DATA STRUCTURES
What does heterogeneous mean? Tuples usually contain a heterogeneous sequence of elements,
while lists usually contain a homogeneous sequence. For example, we have a list that contains different
fruits. Usually, the names of the fruits can be stored in a list, since they are homogeneous. Now we want
to have a data structure to store how many pieces of fruit we have of each type. This is usually where
the tuples comes in, since the name of the fruit and the number are heterogeneous. Such as ("apple", 3)
which means we have 3 apples.
Tuples or lists can be accessed by unpacking as shown in the following example, which requires that
the number of variables on the left-hand side of the equality sign be equal to the number of elements
in the sequence.
In [9]: a, b, c = list_1
print(a, b, c)
1 2 1
Note! The opposite operation to unpacking is packing, as shown in the following example. We
can see that we do not need the parentheses to define a tuple, but it is considered good practice to
do so.
In [10]: list_2 = 2, 4, 5
list_2
Out[10]: (2, 4, 5)
symmetric difference. It is defined by using a pair of braces, { }, and its elements are separated by
commas.
In [1]: {3, 3, 2, 3, 1, 4, 5, 6, 4, 2}
Out[1]: {1, 2, 3, 4, 5, 6}
Using “sets” is a quick way to determine the unique elements in a string, list, or tuple.
Out[2]: {1, 2, 3}
Out[3]: {2, 4, 5, 6}
In [4]: set("Banana")
We mentioned earlier that sets support the mathematical operations like union, intersection, differ-
ence, and symmetric difference.
In [5]: print(set_1)
print(set_2)
{1, 2, 3}
{2, 4, 5, 6}
In [6]: set_1.union(set_2)
42 CHAPTER 2 VARIABLES AND BASIC DATA STRUCTURES
Out[6]: {1, 2, 3, 4, 5, 6}
In [7]: set_1.intersection(set_2)
Out[7]: {2}
Out[8]: True
Within a dictionary, because elements are stored without order, you cannot access a dictionary
based on a sequence of index numbers. To access to a dictionary, we need to use the key of the element
– dictionary[key].
In [2]: dict_1["apple"]
Out[2]: 3
We can get all the keys in a dictionary by using the keys method, or all the values by using the method
values.
2.6 DATA STRUCTURE – DICTIONARY 43
TRY IT! Get all the keys and values from dict_1.
In [3]: dict_1.keys()
In [4]: dict_1.values()
We can also get the size of a dictionary by using the len function.
In [5]: len(dict_1)
Out[5]: 3
We can define an empty dictionary and then fill in the element later. Or we can turn a list of tuples
with (key, value) pairs to a dictionary.
TRY IT! Define an empty dictionary named school_dict and add value "UC Berkeley":"USA".
In [6]: school_dict = {}
school_dict["UC Berkeley"] = "USA"
school_dict
TRY IT! Turn the list of tuples [("UC Berkeley", "USA"), ("Oxford", "UK")] into a dictio-
nary.
We can also check if an element belongs to a dictionary using the operator in.
44 CHAPTER 2 VARIABLES AND BASIC DATA STRUCTURES
Out[9]: True
Out[10]: True
We can also use the list function to turn a dictionary with a list of keys. For example,
In [11]: list(school_dict)
2 http://www.numpy.org.
2.7 INTRODUCING NumPy ARRAYS 45
WARNING! Of course, you can call it any name, but “np” is considered convention and is ac-
cepted by the entire community, and it is a good practice to use it.
To define an array in Python, you can use the np.array function to convert a list.
TRY IT!
Create the
following arrays:
x= 1 4 3
1 4 3
y=
9 2 7
Note! A 2D array can use nested lists to represent, with the inner list representing each row.
Knowing the size or length of an array is often helpful. The array shape attribute is called on an
array M and returns a 2 × 3 array where the first element is the number of rows in the matrix M; and the
second element is the number of columns in M. Note that the output of the shape attribute is a tuple.
The size attribute is called on an array M and returns the total number of elements in matrix M.
TRY IT! Find the rows, columns, and the total size for array y.
In [4]: y.shape
Out[4]: (2, 3)
In [5]: y.size
Out[5]: 6
Note! You may notice the difference that we only use y.shape instead of y.shape(); this is
because shape is an attribute rather than a method in this array object. We will introduce more of
the object-oriented programming in a later chapter. For now, just remember that when we call a
method in an object, we need to use the parentheses, while with an attribute we do not.
Very often we would like to generate arrays that have a structure or pattern. For instance, we may
wish to create the array z = [1 2 3 ... 2000]. It would be very cumbersome to type the entire de-
scription of z into Python. For generating arrays that are in order and evenly spaced, it is useful to use
the arange function in NumPy.
46 CHAPTER 2 VARIABLES AND BASIC DATA STRUCTURES
Using the np.arange, we can create z easily. The first two numbers are the start and end of the
sequence, and the last one is the increment. Since it is very common to have an increment of 1, if an
increment is not specified, Python will use a default value of 1. Therefore np.arange(1, 2000) will
have the same result as np.arange(1, 2000, 1). Negative or noninteger increments can also be used.
If the increment “misses” the last value, it will only extend until the value just before the ending value.
For example, x = np.arange(1,8,2) would be [1, 3, 5, 7].
Sometimes we want to guarantee a start and end point for an array but still have evenly spaced
elements. For instance, we may want an array that starts at 1, ends at 8, and has exactly 10 elements.
To do this, use the function np.linspace. The function linspace takes three input values separated
by commas; therefore, A = linspace(a,b,n) generates an array of n equally spaced elements starting
from a and ending at b.
TRY IT! Use linspace to generate an array starting at 3, ending at 9, and containing 10 elements.
Getting access to the 1D NumPy array is similar to what we described for lists or tuples: it has an
index to indicate the location. For example,
Out[9]: 4
Out[11]: 3
For 2D arrays, it is slightly different, since we have rows and columns. To get access to the data in a
2D array M, we need to use M[r, c], whereby the row r and column c are separated by a comma. This
is referred to as “array indexing.” The r and c can be single number, a list, etc. If you only
think about
1 4 3
the row index or the column index, then it is similar to the 1D array. Let us use the y = as
9 2 7
an example.
TRY IT! Obtain the element at first row and second column of array y.
In [12]: y[0,1]
Out[12]: 4
In [13]: y[0, :]
Here are some predefined arrays that are really useful: the np.zeros, np.ones, and np.empty are
three useful functions. See examples of these predefined arrays below:
48 CHAPTER 2 VARIABLES AND BASIC DATA STRUCTURES
Note! The shape of the array is defined in a tuple with the number of rows as the first item,
and the number of columns as the second. If you only need a 1D array, then use only one number
as the input: np.ones(5).
In [18]: np.empty(3)
Note! The empty array is not really empty; it is filled with random very small numbers.
You can reassign a value of an array by using array indexing and the assignment operator. You can
reassign multiple elements to a single number using array indexing on the left-hand side. You can also
reassign multiple elements of an array as long as both the number of elements being assigned and the
number of elements assigned are the same. You can create an array using array indexing.
TRY IT! Let a = [1, 2, 3, 4, 5, 6]. Reassign the fourth element of A to 7. Reassign the first,
second, and third elements to 1. Reassign the second, third, and fourth elements to 9, 8, and 7.
In [19]: a = np.arange(1, 7)
a
In [20]: a[3] = 7
a
In [21]: a[:3] = 1
a
1 2
TRY IT! Create a 2 × 2 zero array b, and set b = using array indexing.
3 4
Arrays are defined using basic arithmetic; however, there are operations between a scalar (a single
number) and an array and operations between two arrays. We will start with operations between a
scalar and an array. To illustrate, let c be a scalar, and b be a matrix. Then b + c, b - c, b * c and
b / c adds a to every element of b, subtracts c from every element of b, multiplies every element of b
by c, and divides every element of b by c, respectively.
1 2
TRY IT! Let b = . Add and subtract 2 from b. Multiply and divide b by 2. Square ev-
3 4
ery element of b. Let c be a scalar. On your own, verify the reflexivity of scalar addition and
multiplication: b + c = c + b and cb = bc.
In [24]: b + 2
In [25]: b - 2
In [26]: 2 * b
In [27]: b / 2
Out[27]: array([[0.5, 1. ],
[1.5, 2. ]])
In [28]: b**2
Describing operations between two matrices is more complicated. Let b and d be two matrices of
the same size. Then b - d takes every element of b and subtracts the corresponding element of d.
Similarly, b + d adds every element of d to the corresponding element of b.
1 2 3 4
TRY IT! Let b = and d = . Compute b + d and b - d.
3 4 5 6
In [30]: b + d
In [31]: b - d
There are two different kinds of multiplication (and division) for matrices. There is element-by-
element matrix multiplication and standard matrix multiplication. This section will only demonstrate
how element-by-element matrix multiplication and division works. Standard matrix multiplication will
be described in the later chapter on Linear Algebra. Python takes the * symbol to mean element-by-
element multiplication. For matrices b and d of the same size, b * d takes every element of b and
multiplies it by the corresponding element of d. The same is true for / and **.
2.7 INTRODUCING NumPy ARRAYS 51
In [32]: b * d
In [33]: b / d
In [34]: b**d
The transposition of an array, b, is an array, d, where b[i, j] = d[j, i]. In other words, the
transposition switches the rows and the columns of b. You can transpose an array in Python using the
array method T.
In [35]: b.T
NumPy has many arithmetic functions, such as sin, cos, etc., that can take arrays as input arguments.
The output is the function evaluated for every element of the input array. A function that takes an array
as input and performs the function on it is said to be vectorized.
Logical operations are defined only between a scalar and an array and between two arrays of the
same size. Between a scalar and an array, the logical operation is conducted between the scalar and
each element of the array. Between two arrays, the logical operation is conducted element-by-element.
52 CHAPTER 2 VARIABLES AND BASIC DATA STRUCTURES
TRY IT! Check which elements of the array x = [1, 2, 4, 5, 9, 3] are larger than 3. Check
which elements in x are larger than the corresponding element in y = [0, 2, 3, 1, 2, 3].
In [38]: x > 3
In [39]: x > y
2.8.2 PROBLEMS
1. Assign the value 2 to the variable x and the value 3 to the variable y. Clear just the variable x.
2.8 SUMMARY AND PROBLEMS 53
3. Let x = 10 and y = 3. Write a line of code that will make each of the following assignments.
u = x + y
v = xy
w = x/y
z = sin(x)
r = 8sin(x)
s = 5sin(xy)
p = x**y
4. Show all the variables in the Jupyter notebook after you finish Problem 3.
5. Assign string "123" to the variable S. Convert the string into a float type and assign the output to
the variable N. Verify that S is a string and N is a float using the type function.
6. Assign the string "HELLO" to the variable s1 and the string "hello" to the variable s2. Use the ==
operator to show that they are not equal. Use the == operator to show that s1 and s2 are equal if the
lower method is used on s1. Use the == operator to show that s1 and s2 are equal if upper method
is used on s2.
7. Use the print function to generate the following strings:
• The world "Engineering" has 11 letters.
• The word "Book" has 4 letters.
8. Check if "Python" is in "Python is great!".
9. Get the last word "great" from "Python is great!"
10. Assign list [1, 8, 9, 15] to a variable list_a and insert 2 at index 1 using the insert method.
Append 4 to the list_a using the append method.
11. Sort the list_a in problem 10 in ascending order.
12. Turn "Python is great!" into a list.
13. Create one tuple with element "One", 1 and assign it to tuple_a.
14. Get the second element in the tuple_a in Problem 13.
15. Get the unique element from (2, 3, 2, 3, 1, 2, 5).
16. Assign (2, 3, 2) to set_a, and (1, 2, 3) to set_b. Obtain the following:
• union of set_a and set_b
• intersection of set_a and set_b
• difference of set_a to set_b using difference method
54 CHAPTER 2 VARIABLES AND BASIC DATA STRUCTURES
17. Create a dictionary that has the keys "A", "B", "C" with values "a", "b", "c" individually. Print all
the keys in the dictionary.
18. Check if key "B" is in the dictionary defined in Problem 17.
19. Create array x and y, where x = [1, 4, 3, 2, 9, 4] and y=[2, 3, 4, 1, 2, 3]. Compute the
assignments from Problem 3.
20. Generate an array with size 100 evenly spaced between −10 to 10 using linspace function in
NumPy.
21. Let array_a be an array [-1, 0, 1, 2, 0, 3]. Write a command that will return an array con-
sisting of all the elements of array_a that are larger than zero. Hint: Use logical expression as the
index of the array.
⎛ ⎞
3 5 3
22. Create an array y = ⎝2 2 5⎠ and calculate its transpose.
3 8 9
23. Create a 2 × 4 zero array.
24. Change the second column in the above array to 1.
25. Write a magic command to clear all the variables in the Jupyter notebook.