A Beginner’s Guide to Python 3 for Data Science — Part 2

Khan Anik Rahman
5 min readDec 13, 2022

--

In part 1, we discussed how we can add grocery prices using Python code. With Python, we can do all sorts of mathematical operations with just a few lines of code. The + symbol is one of many arithmetic operators we have available to do this. Using these operators, we tell Python what is it that we are trying to achieve with our code.

Let’s get familiar with a few basic operators.

Table: Arithmetic Operators

Python Lists

List is a compound data type. We can group items together in lists. These items have to be enclosed by third brackets. The values are separated by either commas or spaces.

sample_1 = [1, 2, 3, 4] 
sample_2 = [1, True, "test"]
sample_3 = [[1, 2, 3, 4], [1, True, "test]]

Lists can group together any type of data. We can even create a list of lists; how neat is that!

Sub-setting Lists

Lists are ordered data types and follow 0 — indexing. In the code block above, the first item of sample_1 is at index 0, and the 4th item is at index 3. We can access list items using their index.

Here is the generalized syntax: list_name[index]

To get a better grasp of the concept, run the codes below.

sample_1[2]
sample_2[0]
sample_3[3]

Python also has reverse indexing. Reverse indexing starts from -1. The index, -1, is always going to be the last item of a list. To give an example, for the list, sample_1, both sample_1[2] and sample_1[-2] will print out 3.

List Slicing

Selecting single values from a list is just one part of the story. It’s also possible to slice a list. We can select multiple elements from a list by slicing.

Here is the generalized slicing syntax: list_name[start_index : end_index]

While slicing, the value corresponding to end_index is not included in the output. If we want to print “b” and “c” from list x below, the slicing should be x[1:3] not x[1:2].

x[1:2] will only print “b” as index 2 is excluded.

Make sure to run the following lines of code.

x = ["a", "b", "c", "d"]
print(x[:2])
print(x[2:])
print(x[1:3])
print(x[:])

Let’s get some more practice in.

y = [["a", "b", "c"], ["d", "e", "f"], ["g", "h", "i"]]
print(y[2])

If you’ve run the code block above, you will see that printing y[2] prints the 3rd list of the list of lists, y. We can go even deeper and extract the items in that inner list.

Here is the code to do that.

print(y[2][0]) 
print(y[2][1])
print(y[2][2])

y[2][0] refers to “g”, while the following lines refer to “h” and “i” respectively.

Think of it this way: For y[2][0], y[2] zooms into the list of lists y and selects the item at index 2, which is a list. The 0 after 2 then picks out the 0th item from the inner list which is the list item, “g.”

Replacing List Items

If we know the index of a list item, we can replace it. The following code replaces the item at index 1 of list x with the item “r”.

x = ["a", "b", "c", "d"]
x[1] = "r"
print(x)

We can also replace multiple list items. Try the following code yourself.

x = ["a", "b", "c", "d"]
x[2:] = ["s", "t"]
print(x)

Extend a List

We can extend lists using the + operation. The following code will extend list x by adding list y to the end of it.

The new y will now be: [“a”, “b”, “c”, “d”, “e”, “f”]

x = ["a", "b", "c", "d"]
y = x + ["e", "f"]
print(y)

Delete a List Item

We are able to delete any list items using the del() function. The following code deletes the item at index 1, “b”, from list x.

x = ["a", "b", "c", "d"]
del(x[1])
print(x)

Copying a List

We often need to make copies of a list. Suppose, we have a list, a = [“x”, “y”, “z”] and want to make a copy of it. Let’s try the instinctive way.

a = ["x", "y", "z"]
b = a
print(b)
print(a)

The output will be the following.

Looks good right? Now, let’s delete the first item of our newly copied list.

del(b[0])
print(b)
print(a)

This is the outcome. Can you identify what's wrong here?

We only wanted to change list b but changing b ended up changing the original list, list a, as well. This is very bad for our code as the data we initially saved just got overwritten. To understand what went wrong, we need to talk a bit about how lists work.

When we make a list, the variable that it’s assigned to points to the memory location where that list is saved rather pointing to the list items. As a result, when we wrote b = a, we actually copied over list a’s memory location to list b rather than only copying the list data. As a result, when we changed the new list, it also changed the original list.

Please note that, this concept is a bit advanced and might need a while to sink in. The takeaway is we can’t copy a list by just assigning it to a new variable.

The proper way to make a copy of list a is the following.

b = a[:]

Now, we are using list slicing to correctly copy the items of list a to list b. This eliminates any possibilities of the error we just faced, and this is the best practice when it comes to copy lists.

That’s it for today. Congratulations! By now, you should have a good understanding of how lists work in Python. Take a well-deserved break, and don’t forget to revisit these concepts again.

Ending Notes

Quote of the Day

“Believe you can and you’re halfway there.” — Theodore Roosevelt.

--

--