Video Coming Soon...

Created by Zed A. Shaw Updated 2024-11-15 13:50:32
 

23: Introductory Lists

Most programming languages have some way to store data inside the computer. For some languages only have raw memory locations, but programmers easily make mistakes when that's the case. In modern languages you're provided with some core ways to store data called "data structures". A data structure takes pieces of data (integers, strings, and even other data structures) and organizes them in some useful way. In this exercise we'll learn about the sequence style of data structures called a "list" or "Array" depending on the language.

Python's simplest sequence data structure is the list which is an ordered list of things. You can access the elements of a list randomly, in order, extend it, shrink it, and most anything else you could do to a sequence of things in real life.

You make a list like this:

fruit = ["apples", "oranges", "grapes"];

That's all. Just put [ (left-square-bracket) and ] (right-square-bracket) around the list of things and separate them with commas. You can also put anything you want into a list, even other lists:

inventory = [ ["Buick", 10], ["Corvette", 1], ["Toyota", 4]];

In this code I have a list, and that list has 3 lists inside it. Each of those lists then has a name of a car type and the count of inventory. Study this and make sure you can take it apart when you read it. Storing lists inside lists inside other data structures is very common.

Previous Lesson Next Lesson

Register for Learn Python the Hard Way, 5th Edition (2023-2024)

Register today for the course and get the all currently available videos and lessons, plus all future modules for no extra charge.