Book Image

The Python Workshop - Second Edition

By : Corey Wade, Mario Corchero Jiménez, Andrew Bird, Dr. Lau Cher Han, Graham Lee
4.7 (3)
Book Image

The Python Workshop - Second Edition

4.7 (3)
By: Corey Wade, Mario Corchero Jiménez, Andrew Bird, Dr. Lau Cher Han, Graham Lee

Overview of this book

Python is among the most popular programming languages in the world. It’s ideal for beginners because it’s easy to read and write, and for developers, because it’s widely available with a strong support community, extensive documentation, and phenomenal libraries – both built-in and user-contributed. This project-based course has been designed by a team of expert authors to get you up and running with Python. You’ll work though engaging projects that’ll enable you to leverage your newfound Python skills efficiently in technical jobs, personal projects, and job interviews. The book will help you gain an edge in data science, web development, and software development, preparing you to tackle real-world challenges in Python and pursue advanced topics on your own. Throughout the chapters, each component has been explicitly designed to engage and stimulate different parts of the brain so that you can retain and apply what you learn in the practical context with maximum impact. By completing the course from start to finish, you’ll walk away feeling capable of tackling any real-world Python development problem.
Table of Contents (16 chapters)
13
Chapter 13: The Evolution of Python – Discovering New Python Features

The power of lists

You will now look at the first type of data structure in Python: lists.

A list is a type of container in Python that is used to store multiple datasets at the same time. Python lists are often compared to arrays in other programming languages, but they do a lot more.

The following figure shows a list of fruits, along with their respective indices:

Figure 2.2 – A Python list with a positive index

Figure 2.2 – A Python list with a positive index

A list in Python is written within square brackets, [ ]. Each element in the list has its own distinct index. The elements in a list have a finite sequence. Like other programming languages, the index of the first item of a list is 0, the second item has an index of 1, and so on. This has to do with how lists are implemented at a lower programming level, so do take note of this when you are writing index-based operations for lists and other iterable objects.

You will now look at the different ways that lists can be useful.

Exercise 21 – working with Python lists

In this exercise, you will learn how to work with a Python list by coding and creating a list and adding items to it. For example, this could prove useful if you have to use a list to store the items that are in a shopping cart:

  1. Open a new Jupyter Notebook.
  2. Now, enter the following code snippet:
    shopping = ["bread","milk", "eggs"]
    print(shopping)

The output is as follows:

['bread', 'milk', 'eggs']

Here, you created a list called shopping with bread, milk, and eggs inside it.

Since a list is a type of iterable in Python, you can use a for loop to iterate over all of the elements inside a list.

  1. Now, enter and execute the code for a for loop and observe the output:
    for item in shopping:
      print(item)

The output is as follows:

bread
milk
egg

Note

Python lists are different from arrays used in other languages, such as Java and C#. Python allows mixed types in a list – that is, int and string.

  1. Now, use a mixed type of data within the list’s content and enter the following code in a new cell:
    mixed = [365, "days", True]
    print(mixed)

The output is as follows:

[365, 'days', True]

But you might be wondering, in that case, shouldn’t we be allowed to store a list of lists inside a list? We will take an in-depth look at nested lists, which can be used to represent complex data structures, after the next section.

In this exercise, you were introduced to the basics of Python lists.

Now, let’s see what list methods are available in Python.