Last time out, we had a look at variables and basic data types in Python. We stored some stuff in variables, examined string concatenation and string methods. We also looked at number types in Python which are integers and floats.
I do hope you have put in a lot of practice in understanding the previous chapters. If you need a refresher, do check out the first two chapters of this tutorial series. In the first part, we went through a brief introduction to Python, and in the second part, we examined variables and basic data types.
In this chapter, we’ll be looking at lists in Python.
Lists
Lists are one of the most important features of the Python programming language. They are simply a collection of items in a particular order. The cool thing about lists is that the data contained therein do not have to be of the same data type. You can put integers, floats, and strings in a single list. You can even put a list in a list!
In Python, lists are an enclosure defined by square brackets ([ ]). The elements placed in a list are separated by commas (,).
For example, a list storing the names of the best movie series ever would look like this.
best_movies = [‘Harry Potter’, ‘Game of Thrones’, ‘Prison Break’, ‘The Blacklist’]
Accessing List Elements
So, you have just created this super cool list of best movies and you want to pick the Harry Potter movie. Your first attempt might be to print the list. You probably won’t get the output you desired as printing a list pretty much just displays the list as it is, commas and all.
You don’t want that. You really want the Harry Potter movie.
Luckily, Python offers a way out: Indexing.
List Indexing
By definition, lists are a collection of items in a particular order. Because of this order, each element in a list has its own position called an index and it can be accessed using this value.
Index values in Python begin at 0, not 1. Hence, to access our much loved Harry Potter movie, we simply do this.
Don’t forget that index values begin at 0! It can get pretty confusing at first when working with your own lists, but with practice, you’ll get used to it.
If you have a list that you don’t know the length of, you may want to access the last value of that list. Python provides a way to do this.
print(best_movies[-1]) returns The Blacklist. To get the second and third items from the end of the list, use [-2], and [-3] respectively and the sequence goes on and on.
What Happens When You Try to Get an Index That Doesn’t Exist?
If you try to get an index value that is, say longer that the list you are working it, Python returns an IndexError: list index out of range.
Using Individual Values
Individual values from a list can be used like you would use any other variable, be it a string or a number.
For example, we treated string concatenation in the previous chapter, we can use a value from our best_movies list to compose a sentence.
msg = “The world’s best movie is “ + best_movies[0] + “.”
Printing the above returns, The world’s best movie is Harry Potter.
Phew! That is a chunk load of Python, yeah, I think so too. In the next chapter, we’ll be looking at adding new elements to a predefined list, changing elements and removing elements.
Don’t forget to practice, practice, practice!
If you have any questions, feel free to reach me.