Get startedGet started for free

Introduction and lists

1. Introduction and lists

Hello, I'm Jason Myers and welcome to the course. I'm an author, open source maintainer, and an avid Python user.

2. Data types

In most programming languages, the data type system sets the stage for the capabilities of the language. Understanding how to use the fundamental data types of a language greatly empowers you as a data scientist. You've already encountered integers and strings, so let's start with container sequences.

3. Container sequences

A container sequence gets its name because it holds a sequence of elements of other data types. In the data science world, we'll use these containers to store our data for aggregation, order, sorting, and more. Python provides several container sequences such as lists, sets, and tuples to name a few. They can be mutable meaning that they can have elements added and removed from them. Immutability, not able to be altered, allows us to protect our reference data, and replace individual data points with sums, averages, derivations, etc. We can iterate, also known as looping, over the data contained within these containers. Being able to iterate over these sequences allows us to group data, aggregate it, and process it over time. Let's start with learning about container types by looking at lists.

4. Lists

Often we need to hold an ordered collection of items, and lists allow us to do just that. Lists are mutable so we can add or remove data from them. Lists also allow us to access an individual element within them using an index. Let's see this in action.

5. Accessing single items in list

If I wanted to store a list of cookies I've eaten this week, I would begin by creating that list of cookies. When I eat another cookie, I'll want to add that to my list as well, which I can do with the append method. Then I can print the whole list of cookies. If I wanted to print the cookie I ate third; then I could use an index, which in this case would be 2 since indexes starts at zero, and print that element from the list. In addition to appending each cookie one at a time, we might also want to combine multiple lists.

6. Combining lists

Python empowers us to combine lists in a few ways. First, we can use operators like the plus sign to add two lists together. When we do this, we will get a new list object returned to us. For example, we can add a list of cookies and cakes to create a list of desserts. Additionally, we can use the extend method on the list to combine two lists effectively appending all the values from a second list.

7. Finding elements in a list

Earlier, we took advantage of the indexability of lists to return the item at a certain index. However, if we only know the value of an element, we can use it with the index method to get its index. Here I'm trying to remember when I had a sugar cookie.

8. Removing elements in a List

Another reason I might want to know the index of a value is to remove it from a list with the pop method. Here I want to remove that sugar cookie from the list of cookies. So I pass the index I found earlier to the pop method, and I store the result of the pop method, which is the value we removed. Finally, I print the list so I can ensure it was removed.

9. Iterating over lists

Often when working with lists, we want to work on that list one element at a time. We can do that by iterating over the list using a list comprehension. Here I want to print each cookie name in title case I've eaten this week. So I use a list comprehension to loop over each cookie in the cookies list, convert it to title case with the dot-title() method and print it.

10. Sorting lists

Python provides the sorted function that accepts an iterable such as a list and returns a new list with the elements in the proper order. Here I sort the cookies list and then print it so I can see the cookie names in alphabetical order. Time for you to try this on your own.

11. Let's practice!