11: Lists in Python

Try it:

Store and use lots of data in the same variable
Lists in python : Store and use lots of data in the same variable

Below you’ll see some example python code that contains a shopping list

Press Ctrl + Enter  to run the code.

This code starts by creating a list called shopping which contains "bananas", "oranges", "chocolate" and "toilet roll"

It then adds "pizza" to the end of the list and removes "chocolate"

Challenges:

Start by running the code.

  1. Add 'socks' to the shopping list

    There’s two ways to do to this. You could either add it on line 3 so that it’s one of the original values in the list or you could copy and paste line 9 to append "socks" to the end of the list.

  2. Remove 'oranges' from the list

    Again, there are two ways to do this. You could remove it from line 3 or use shopping.remove() 

  3. Add items to the list until there are exactly 10 items

    You can find out how many items are in a list using len(shopping) which calculates the length of the list called shopping. Just add more values until you’ve got 10 in total. 

On the next page you’ll get some code with both syntax and logic errors

Leave a Reply

Your email address will not be published. Required fields are marked *