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:
- Integers: [1, 2, 3, 4, 5]
- Strings: ['a', 's', 'd', 'f', 'g']
- Mixed Data Types: You can even combine types in a single list, like [1, 'a', False].
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:
- Keep related data together.
- Maintain a specific order.
- Store as much (or as little) information as you need in one single variable name.
The Right Tool for the Job
Data structures have pros and cons, just like physical storage in the real world:
- The Bookshelf (The List): A bookshelf is excellent for organizing items where you need to see the "spine" or the name of the data clearly and keep things in a specific order.
- The Suitcase: A suitcase is great for holding everything you need for a vacation in one place, but it's not very good for organizing a library. While all the books might fit, it would be difficult to find a specific one without digging through everything.
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.
- seven_eleven_cart[0] is the Iced Latte.
- seven_eleven_cart[1] is the AAA Batteries.
- seven_eleven_cart[2] is the Dim Sum.
So, if you run this:
Time to experiment!
Coding Exercises (VS Code) Instructions:
- Create a file named lists.py in your part19 folder.
- Complete the tasks and use # comments to explain your answers.
Exercise 1: Create Your Own Cart:
The Goal: Practice list creation and naming.
- The Challenge: You might see an error when you run the program. Read the error message carefully and try to solve the problem using type conversion.
- Print the entire list to the terminal.
Exercise 2: The Specific Item Grab:
The Goal: Practice using index numbers.
- The Challenge: Using the my_wishlist you created in Exercise 1, write a print statement that only prints the third item in your list.
- Hint: Remember that Python starts counting at 0!
Exercise 3: The Recipe Builder:
The Goal: Work with mixed data types.
- The Challenge: Create a list called recipe.
- The first item should be a String (the name of the dish).
- The second item should be an Integer (number of ingredients).
- The third item should be a Float (the price of the ingredients).
- Print the list and check if Python handles the different data types correctly.
Exercise 4: Trigger an Error:
The Goal: Learn to read and understand the IndexError.
- The Challenge: Create a list with only 3 items. Write a line of code that tries to print the item at index 10.
- Run the code, look at the error message in the terminal, and identify the specific name of the error Python gives you.
Exercise 5: Quick Replacement:
The Goal: Learn that lists are "mutable" (changeable).
- The Challenge: Create a list: tools = ['hammer', 'screwdriver', 'drill'].
- On a new line, type tools[1] = 'wrench'.
- Print the list. What happened to the screwdriver?
- Hint: You just used an index to swap one item for another!
Don't Forget to commit and Push!