Professional Documents
Culture Documents
Python 6
Python 6
Lesson seven
Learning Objective
• To use a textual programming language to solve a variety of
computational problems.
Success Criteria
• To use Python lists as a data structure
• Adding list items.
• Print an item.
Outstanding Operators
Q1: What type of operator is == ?
A. Boolean B. Python C. Comparative D. Mathematical
Are you a
Q2: What type of operator is AND ? smooth
A. Boolean B. Python C. Comparative D. Mathematical operator?
Q6: Which operator means: “not equal to” ? Are you equal
to this task?
A. == B. != C. <= D. >=
Q12: What is the result of this statement: True and not False ?
A. True B. False
Outstanding Operators
Q13: What result would Python give for the following?
>>> not True
How many did
A. True B. False you answer
correctly?
A. True B. False C. “You can join the Army” D. “You can’t join the Army”
Monty’s Menu
Monty the Python loves pizza. His favourite is a margherita
(mozzarella cheese, basil, tomatoes, garlic and olive oil)
with some extra toppings on top.
• Well done: you have just created your first ‘list’ in Python.
• Python lists are very useful for storing lots of similar information all in
the same place (and with the same name or identifier).
• In most other programming languages, lists are known as arrays.
'basil'
'mozzarella'
Let’s find out how to add more items…
Monty’s Menu
Add the following lines of code to your Pizza program, and then run the
program by clicking on Run then Run Module or by pressing the F5 key
on your keyboard:
# Create a pizza ingredients list
ingredients = ['mozzarella', 'basil', 'tomatoes']
# Print the list
Be careful again with
print(ingredients)
the brackets, some
# Add a new item are [squared], others
ingredients.append('garlic') are (curved).
# Print the updated list
print(ingredients)
Monty’s Menu
You should see the following output:
['mozzarella', 'basil', 'tomatoes']
['mozzarella', 'basil', 'tomatoes', 'garlic']
>>>
• The Python command ‘append’ adds a new item to the end of a list.
We type the name of the list first, then a dot, then the command: append.
• Try appending more items to the list – you can add as many as you like:
That’s because print(ingredients[1]) prints item number 1 from the list, which
is ‘basil’. Shouldn’t item 1 be ‘mozzarella’? It turns out that in Python lists, the
first item is item 0 (zero).
Try changing the code above to print(ingredients[0]) and see what happens.
Can you print other items?
Lovely Lists
What happens if you try to print an item that doesn’t exist? Try this…
# Print an item from the list
print(ingredients[100])
Python can’t find item number 100, so we would get an error message:
IndexError: list index out of range
Thankfully, there is a way that we can count the number of items in a
Python list. Try adding the following code to your program and run:
# Count the number of items in the list
length = len(ingredients)
print("There are", length, "items in the list.")
The output then shows:
There are 6 items in the list.
>>>
Lovely Lists
Let’s have a quick recap of what we’ve learned so far today…
Key Terms
In Python, a list is a type of data structure.
Lists are used to store multiple items of similar data.
We can think of a list as like a bookshelf with many shelves; on each ‘shelf’
we can store a different piece of information (data). Lists must have a
name; the special computing term for name is identifier. In most other
programming languages, lists are called arrays. In Python, there are many
useful things that we can do with lists.
# Create a new list
mySquad = ['Sally', 'Joe', 'Bedriya']
# Add an item to the list
mySquad.append('Billie')
# Count how many items are in the list
print(len(mySquad))
Lovely Lists
Finally with lists, here’s a run-down of some other useful things that Python
can do. Spend a few minutes experimenting with your Pizza program and try
out some of these new ideas…
# Create a pizza ingredients list
ingredients = ['mozzarella', 'basil', 'tomatoes']
# Insert a new item at position number 3 (yes, it is 3!)
ingredients.insert(2, 'anchovies')
# Don't like mushrooms? No worries, remove an item
ingredients.remove('mushrooms')
# Put the items into alphabetical order
ingredients.sort()
# Put the items into reverse alphabetical order
ingredients.reverse()
# Print out the updated list
print(ingredients)
Let’s Bring It All Together
Key Terms
In Python, a list is a type of data structure.
Lists are used to store multiple items of similar data.
Green Light: you feel fully confident with this objective and
you understand it well.
Success Criteria:
• To use Python lists as a data structure.