AlgoCademy
Lesson
Code

Lists in {lang}


When we're dealing with large amounts of data, we want to make sure we can organize and manage it properly.

In {lang}, we use lists to store several pieces of data in one place.


List Declaration / Creation:

You start a list declaration with an opening square bracket, end it with a closing square bracket, and put a comma between each entry, like this:

sandwich = ["peanut butter", "jelly", "bread"]

In this program we created a list consisting of 3 string items: "peanut butter", "jelly" and "bread".

Then, we stored that list in a variable sandwich so we can later access it. For example, I can print the list:

# Creating the list:
sandwich = ["peanut butter", "jelly", "bread"]

# Printing the list:
print(sandwich) # Output: ["peanut butter", "jelly", "bread"]

Items Data Types

Lists can store any data types (strings, integers, booleans, etc.) and the items shouldn't necessary be of the same data type. For example:

myList = ["Hello world!", 32, False]

myList consits of one string item, one number item and one boolean item.

We use lists to better organize pieces of data which are part of the same family or share the same meaning, like the name of our friends or the items in a sandwich.

So most of the time we will have lists where the items share the same data type.


Assignment
Follow the Coding Tutorial and let's play with some arrays.


Hint
Look at the examples above if you get stuck.