Week 3 - Section 1 - Data types and a bit of gaming
In this section we will learn about lists and tuples and we will scratch the surface of pygame. Be prepared for excitement.
Chapter 4.2 - And other lists
4.2 - And other lists
So what should i do if i want to create a shopping list? Copy the following command and test it.
>>> my_shopping_list = ["apples","bananas","carrots"]
The items in the list are numbered starting from 0 (as we mentioned in programming we usually start from 0).
So the 0 item in my_shopping_list is "apples"
the 1 item is "bananas"
and the 2 item is "carrots"
Try the following :
>>> print(my_shopping_list) # in print we use parenthesis () and in lists square brackets )
In the last example there is an error :
Traceback (most recent call last):
File "<pyshell>", line 1, in <module>
IndexError: list index out of range
because myshoppinglist does not has an item indexed 3
we can change an item in list :
>>> my_shopping_list = "apricots"
my_friends = ["Jim", "John", "Jack", "Jane"] for item in my_friends: print (item)
We can view the number of items in a list with command len.
and we can add items to a list :
Our list has 5 items now. Check its length.