# command Python to make a list and store it in my_list
= ['pizza', 'fries', 3, -2.5, 'donuts']
my_list
# command Python to print my_list
print(my_list)
['pizza', 'fries', 3, -2.5, 'donuts']
Lists can be used to group different values together - it’s just a collection of things.
You can make a list in Python by putting different things in a box of brackets []
separated by commas:
# command Python to make a list and store it in my_list
my_list = ['pizza', 'fries', 3, -2.5, 'donuts']
# command Python to print my_list
print(my_list)
['pizza', 'fries', 3, -2.5, 'donuts']
Now let’s make a list of fruit and save it to the variable fruit
. Have your list include apple, banana, grape, and any other fruit you want!
# command Python to make a list of fruit including apple, banana, and grape
fruit = ['apple','banana','grape']
# command Python to print the list of fruit
print(fruit)
['apple', 'banana', 'grape']
You can also see how many things there are in your list using len
:
Let’s find the length of the fruit
list:
To add lists together, all you have to do is use the +
operator.
['pizza', 'fries', 3, -2.5, 'donuts', 'apple', 'banana', 'grape']
You can also make a list that includes variables:
# assign the value 1 to the variable a
a = 1
# assign the value 2 to the variable b
b = 2
# make a list including values and variables and save it to the variable numbers
numbers = [0, a, b, a+b]
# print the numbers list
print(numbers)
[0, 1, 2, 3]
To add a value to a list, you can use .append()
:
Let’s add mango to our fruit list:
['apple', 'banana', 'grape', 'mango']
You just learned how to: * Make a list * Find the length of a list * Add lists together * Add things to a list
Now it’s time to practice what you learned!