Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 18

Computing: Introduction to Python

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?

Q3: What type of operator is < ?


A. Boolean B. Python C. Comparative D. Mathematical

Q4: What type of operator is NOT?


A. Boolean B. Python C. Comparative D. Mathematical
Outstanding Operators
Q5: Which operator means: “greater than or equal to” ?
A. == B. != C. <= D. >=

Q6: Which operator means: “not equal to” ? Are you equal
to this task?
A. == B. != C. <= D. >=

Q7: What is the result of this comparison: 65 <= 56 ?


A. True B. False C. 121 D. 9

Q8: What is the result of this comparison: 65 != 56 ?


A. True B. False C. 121 D. 9
Outstanding Operators
Q9: What is the result of this statement: True and False ?
A. True B. False

Q10: What is the result of this statement: True or False ?


True, that!
A. True B. False

Q11: What is the result of this statement: not (True or False) ?


A. True B. False

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?

Q14: What output would the following code produce?


age = 16
if age >= 16 and age <= 32:
print("You can join the Army.")
else:
print("You can't join the Army.")

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.

What extra toppings would you add?

• Let’s write a program in Python to help Monty make a pizza!


• Start Python IDLE and then click on File and New File ready to create a
new Python program in the script mode window.
Monty’s Menu
Type in the following code:
# Create a pizza ingredients list
ingredients = ['mozzarella', 'basil', 'tomatoes']
# Print the list
print(ingredients)

• Save your Python program with the name: Pizza


• Run your program by clicking on Run then Run
Module or press the F5 key on your keyboard.
• What do you see?

Be careful with the brackets


– some are [squared], others
are (curved).
Monty’s Menu
You should see the following output:
['mozzarella', 'basil', 'tomatoes']
>>>

• 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.

ingredients • We can think of a list as like a bookshelf stored


away in computer memory.
• Each bookshelf is given a name (or identifier),
in this case the bookshelf name is: ingredients.
'tomatoes' • We can then add items to the list; each item is
stored on its own shelf, but all items are in the
'basil' same bookshelf.
'mozzarella’
Monty’s Menu
• Python recognises a new list by the [square brackets]. We must always
use [square brackets] when making a new list.
• We then put all of our list items inside the square brackets and separate
each item with a comma.

• What if we wanted to add more items to our list of ingredients?


ingredients s
Python Pizza Ingredient
'olive oil' mozzarella
basil
'garlic' tomatoes
garlic
'tomatoes' olive oil

'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:

# Add new items


ingredients.append('garlic')
There isn’t
mush-room left
ingredients.append('olive oil') on this pizza!
ingredients.append('mushrooms')
# Print the updated list
print(ingredients)
Lovely Lists
• Now that we know how to create a new list and append new items, let’s
see what else we can do with lists.
• Try adding the following code to the end of your Pizza program and then
run the program once again:
# Print an item from the list
print(ingredients[1])
Notice that we have
You should see the following: [square brackets]
inside (curved
basil
brackets) here.
>>>

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.

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.
Rate Your Progress
Red Light: you have understood some of the objective
and you will think about it some more later on, perhaps
asking a friend or teacher for help.

Amber Light: you have understood most of the objective


and you are happy with your progress.

Green Light: you feel fully confident with this objective and
you understand it well.

Success Criteria:
• To use Python lists as a data structure.

You might also like