Flat_Eric Contemplating Data Type Conversions

Lists

He's making a list, and checking it twice.

Lists

The next data type we are going to learn about is Lists, and this is a big one! Lists are incredibly powerful and will become a fundamental part of your coding journey.

To refresh your memory, we create a String by wrapping text in single or double quotes—anything inside those quotes is part of that string. Lists, however, are denoted by square brackets [ ].

How Lists Work

Inside a list, we store different objects separated by commas. Lists are versatile because they can hold:

In other programming languages like JavaScript or Ruby, you might have heard these referred to as Arrays. In Python, we simply call them Lists. Essentially, a list is a collection of items stored in a specific order. Think of a list like a digital container where you can keep your data organized and easy to access.

Understanding Data Structures

Lists are our first look at Data Structures. This might be the first time you’ve heard this term, but it is one of the most important concepts in all of programming.

At its simplest, a Data Structure is just a way to organize information in a specific place so it can be used efficiently. If variables are like single envelopes holding one piece of paper, a data structure is like a box, a file folder, or a bookcase.

Why use them?

Imagine trying to find a specific book if all your books were just thrown randomly on the floor. It would be a nightmare! A bookcase (the data structure) gives those books a specific order and location, making them easy to find and manage.

In Python, a List is that bookcase. It allows you to:

The Right Tool for the Job

Data structures have pros and cons, just like physical storage in the real world:

The Shopping Cart Analogy

Think about when you open the 7-Eleven app. As you scroll through the app, you start adding items to your digital cart: an iced latte, a pack of AAA batteries, and some dim sum.

In Python, that cart is a List. You are taking different "objects" and grouping them together under one variable name:

 
# A real-life Python list

seven_eleven_cart = ['iced_latte', 'aaa_batteries', 'dim_sum']
    

When we want to see what is inside our list, we have two main ways to do it: viewing the entire collection or grabbing one specific item.

Printing the Whole Cart

If you simply print the variable name, Python shows you everything inside the brackets:

 
# A real-life Python list

seven_eleven_cart = ['iced_latte', 'aaa_batteries', 'dim_sum']
print(seven_eleven_cart)
    

 
Output: ['iced_latte', 'aaa_batteries', 'dim_sum']
    

Accessing by Index

To grab a specific item, we use its index (its position number). In Python, we start counting from 0.

So, if you run this:

Time to experiment!

Coding Exercises (VS Code) Instructions:

Exercise 1: Create Your Own Cart:

The Goal: Practice list creation and naming.

Exercise 2: The Specific Item Grab:

The Goal: Practice using index numbers.

Exercise 3: The Recipe Builder:

The Goal: Work with mixed data types.

Exercise 4: Trigger an Error:

The Goal: Learn to read and understand the IndexError.

Exercise 5: Quick Replacement:

The Goal: Learn that lists are "mutable" (changeable).

Don't Forget to commit and Push!

This course was built by DevSTEM - we turn teaching materials into interactive web courses like this one.

Build your own course