Exemplar Answers Book: WWW - Learnict.it
Exemplar Answers Book: WWW - Learnict.it
Exemplar Answers Book: WWW - Learnict.it
1
Python Beginners Workbook
for Secondary Schools
www.learnict.it
Licence
NonCommercial — You may not use this work for commercial purposes.
Share Alike — If you alter, transform, or build upon this work, you may
distribute the resulting work only under a licence identical to this one.
2
Python Beginners Workbook
for Secondary Schools
www.learnict.it
○ Your fair dealing or fair use rights, or other applicable copyright exceptions and
limitations;
○ The author's moral rights;
○ Rights other persons may have either in the work itself or in how the work is
used, such as publicity or privacy rights.
● Notice — For any reuse or distribution, you must make clear to others the licence
terms of this work.
3
Python Beginners Workbook
for Secondary Schools
www.learnict.it
Contents
Licence
Contents
Introduction
Hanging words off a string Task 1
Hanging words off a string Task 2
John Motson style lines of commentary
Python can do maths!
Concatenation is a hard word to remember!
Variables Task 1
Variables Task 2
Comparative Operators Task 1
User Input
Comparative Operators 2 If Statements
Comparative Operators 3 More If Statements
Datatypes
Looping While Loops
Inserting Modules
Variables task 3 Using Variables for Calculations
Defining a function Task 1
Defining a Function Task 2
Lists
4
Python Beginners Workbook
for Secondary Schools
www.learnict.it
Introduction
This book aims to be used as a workbook to introduce students to programming in the Python
programming language in a simple manner.
I find that younger learners can often find concepts hard to grasp if the concept is merely
lectured to them.
The aim of the book is to avoid teaching any concepts too complicated for the time being and
teach young learners in the same way they may learn other concepts in a secondary school
using exemplary and repetitive exercises.
It’s important to note that this book is not meant to replace the teaching of concepts in the
classroom but to help reinforce some of the topics with worksheets that students can use to
practice the concepts that have been discussed by a teacher. You will find on many of the
worksheets that to avoid having too much text, I have left out some explanations that may involve
giving the student too much information and detract from the practicing the new skill.
In essence, computer programming is all about solving problems. The aim of this book is to help
teach the basic syntax and concepts of Python before presenting students with more
complicated problem solving tasks later on (probably to be presented in book 2 ).
If you like this book, please share it with colleagues and peers. If you find errors or you have
suggestions for improvement, please contact me via the following channels:
5
Python Beginners Workbook
for Secondary Schools
www.learnict.it
The word print tells Python that you want it to print some words out on the screen.
This is called printing a string. Imagine letters being stringed up back to back and displayed on
the screen in the order they have been strung up!
In between the brackets we also need to add what we want Python to print in quotation marks.
So I could write:
“My name is HeMan”
Like this:
print(“My name is HeMan”)
print("name")
Write another line in the Python language stating who your favourite singer or band is.
What do you think would happen if the Python code you wrote included quotation marks?
For example:
print(“She said “she loves you” and you know you should be glad”)
Try this out in Python and write your answer in the box below.
print('She said "she loves you" and you know you should be glad')
the print function will stop printing out the rest of the line after the second double quote. Python
can also use single quotes in this case. See example above.
6
Python Beginners Workbook
for Secondary Schools
www.learnict.it
By using a combination of both we can write sentences that need to use quotation marks.
Try to see if you can see what is missing from the following lines of code and write in the
correction underneath.
1 print(hello world)
print(“hello world”) missing quotes
3 (‘Game Over’)
print(’Game Over’) missing print
4 print(‘Game Over”)
print(‘Game Over’) mixed quotation symbols used. Stick to either single or double
7
Python Beginners Workbook
for Secondary Schools
www.learnict.it
A line of commentary is a line of text that the computer will not read as code. The computer will
ignore the line and move to the next line of code.
Type the following code into Python and see what happens when you try to run it:
# This line of text is ignored. The next line prints some text
print("My name is...")
'''
When you need to have
several lines of commentary
like this, you can use 3
quotation marks.
'''
print("Slim Shady")
Although there will not be many lines of commentary in this book, you should make a habit of
adding them to each line of your code like the one above to explain what each code does.
8
Python Beginners Workbook
for Secondary Schools
www.learnict.it
The +, / and * symbols are called operators because they tell the computer to do an operation.
try the following in the Python interpreter and write the answers next to the equations.
1. 12 * 12
2. 1 + 78
3. 45 / 3
4. 10 * 10
5. 10 / 2
6. 10 + 10
7. 10 – 2
8. 10 + 10 + 10
9. 10 * 2 + 5 / 2 2
1. 2 + 5.6
2. 2 * 5.6
3. 12 / 1.2
4. 7.8 4
5. 15 / (1.5 * 3)
9
Python Beginners Workbook
for Secondary Schools
www.learnict.it
Or I could write:
1 print(“hello “ + “world)
print(“hello “ + “world”) missing a quotation mark at the end
4 print("if I had 1 apple and 2 pears, I would have " + str(2 + 1) fruit)
print("if I had 1 apple and 2 pears, I would have " + str(2 + 1) + “fruit”)
Must concatenate the last word too.
5 print("if I had ", 1, "apple and ", 2 "pears, I would have ", 2 + 1, "fruit")
print("if I had ", 1, "apple and ", 2, "pears, I would have ", 2 + 1, "fruit")
Missing a comma to concatenate the second integer.
10
Python Beginners Workbook
for Secondary Schools
www.learnict.it
Variables - Task 1
In computing terms a variable is something that the computer has to remember. It can refer to it
and can make changes to it, if you ask it to. Sometimes people refer to a variable as a nickname
for something. In a game, variables are usually the things can change such as the score, the
timer or the lives of a player.
In the example above, you can see the variables are score, timer and best score.
See if you can spot the variables in the following images:
Image Variables
❏ Score
❏ Coins
❏ World
❏ Timer
❏ Character
❏ Score
❏ Highest score
❏ Health Bar (graphical)
❏ number of KO’s
❏ Timer
❏ Character name
1
Fruit Ninja by, © Halfbrick http://fruitninja.com/
2
Mario Bros © by Nintendo
3
Street Fighter 2 © by Capcom
11
Python Beginners Workbook
for Secondary Schools
www.learnict.it
Setting a variable is easy in Python. Whenever any word or number follows a = symbol, it is
regarded by Python has a variable.
For example to set a variable called bandName to The Beatles we would do this:
bandName = “The Beatles”
Try these out and see if you can correct the errors
1 bandName = "The beatles"
print(bandName " had 27 number 1 singles")
print(bandName + " had 27 number 1 singles")
Missing + for concatenation
We can change variables by reassigning the variable as something else. for example:
batman = “Adam West” Every time the variable batman is reassigned
batman = “Michael Keaton” as something else it forgets the old name.
batman = “George Clooney” There is no way to bring that back unless it is
batman = “Christian Bale” reassigned as a previous value.
12
Python Beginners Workbook
for Secondary Schools
www.learnict.it
Variables - Task 2
Don’t forget a variable is just something the computer has to remember. It may have to
remember many variables. Although variables are not just used in games, games are a good
example of how they are used.
For this example we will use a parable of a famous Italian plumber in order to not upset a large
corporation.
lives = 3
print(lives)
lives = lives + 1
1 What would you have to type in to reduce the lives by 1? Try it out and write the correct
answer below.
lives = lives 1 (or alternatively: lives = 1)
2 Mahmud jumps on a lives bonus which gives him 3 lives at once. How would the code to
make his life go up look for this?
lives = lives + 3 (or alternatively: lives += 3)
13
Python Beginners Workbook
for Secondary Schools
www.learnict.it
You would have learned about comparative operators in maths lessons. Try these out in the
Python Interpreter and write the answers down. Python will reply either True or False:
1. 3 < 1
False
2. 3>1
True
3. 3 == 2+1
True
4. 5 2 > 1
True
5. 1 == 1
True
6. 0 > 0.001
False
In Python remember that adding an = symbol creates a variable. In order to test whether
something is equal to something we use ==.
14
Python Beginners Workbook
for Secondary Schools
www.learnict.it
User Input
We have looked at how to write text and how to create variables but now we need to get the
computer to ask questions and save the answer we give.
Python uses the input() function to ask a question to the programme’s user.
For example:
input("What is your name?: ") The first line asks for your name.
print("my name is R2D2") The 2nd line merely states a name.
The problem with the above code is that after asking for your name it doesn’t save it. Do you
remember that by using a = symbol we could create a variable?
yourName = input("What is your name?: ") Now the computer remembers the name you
print("my name is R2D2") give and can use it.
print(yourName + " was my dad's name")
1. Get Python to ask you your name. Python must reply to you and say hello to you while
referring to you using your name. Write the code into box below.
yourName = input("What is your name?: ")
print(“Hi “ + yourName)
2. Now get Python to ask you your name and age. Python must reply hello yourName and tell you
that you are the same age. Write the code into box below.
yourName = input("What is your name?: ")
print(“Hi “ + yourName)
yourAge = input("What is your age?: ")
print(“Oh! I am also “ + yourAge + “years old”)
15
Python Beginners Workbook
for Secondary Schools
www.learnict.it
Task 2. New challenge. Let’s get Python to ask if my name is John. If it is, Python should reply,
“That’s my name too!” if else Python should reply, “Well it’s not a great name but it will do”.
name = input("What is your name?: ")
if name == “John”:
print(“That’s my name too!”)
else:
print(“Well it’s not a great name but it will do”)
Remember the == operator checked if something was the same as something else.
16
Python Beginners Workbook
for Secondary Schools
www.learnict.it
17
Python Beginners Workbook
for Secondary Schools
www.learnict.it
Datatypes
Programming involves asking a computer to work with different types of information. For
example numbers and words. We will focus mainly on 3.
Python understands that numbers are different than strings. For example it can work out an
equation with numbers like 2 * 2 but not with strings like word * word. There are exceptions to
this and you will see.
Task 1
Use Python to tell us what type of information each of these are:
print(type(10))
<class 'int'>
print(type(10.5))
<class 'float'>
Task 2
Try these in Python too using print(type()):
34, “phone”, 99.9, “The Hobbit”, 95.8, 12, 16, 1, 0.5, 0.9, and 12.45.
Task 3
Can you understand the 3 different types of data? Try to explain what the difference between an
integer, a float and a string is.
❏ Integer is a whole number
❏ a float is a number with more than 1 decimal point
❏ a string can contain data that is letters, symbols and numbers.
18
Python Beginners Workbook
for Secondary Schools
www.learnict.it
name = "" As you can see in the example in the left, the
while name != "Batman": code will keeping looping and asking the
print("Somebody call Batman!") same question while the answer is not
print("Are you him? ") Batman.
name = input("What's your name?")
Notice the nesting of statements underneath
the while? It is the indented code that will keep
looping.
Task 1
Write a while loop to ask someone what the best football team in the world is. Repeat the
question until they say the correct team. Test it in Python and write your answer and explanation
in the table below. Share the code with your teacher and class.
team = ""
while team != "Galatasaray":
print("The best team in the world are")
print("GALATASARAY ")
team = input("What's your favourite
team?")
Task 2
Try to write your own infinite while loop like the one above. This is a hard task but you can be
creative and make code for whatever you want. Test it in Python and write your answer and
explanation in the table below. Share the code with your teacher and class.
19
Python Beginners Workbook
for Secondary Schools
www.learnict.it
Task 3
Now we will try to write one more while loop this time using a condition. Again this task is a lot
harder. Look at the example below and try to write your own.
secret_number = 6 This is a little more complicated. it may be a
guesses = 3 good idea to write this out as a flowchart to
while guesses > 0: help you think about how the code works.
print("Can you guess what number I am thinking?)
guess = int(input("write a number "))
if guess == secret_number:
print("Well done, you guessed correctly!") In this code, we also learn of the break
break statement. This will tell the PC to skip to the
else: next line of code that is not in the current
guesses = guesses 1 indented nesting.
(Alternatively we could replace break with
if guesses == 0: guesses = 0)
print("You have no guesses left!")
count = 5
number = 0
print("this programme will work out the total of a list of numbers")
while count > 0:
number = number + int(input(print("Please enter a number :")))
count = count 1
Try it in Python and share your answer with your teacher and class.
20
Python Beginners Workbook
for Secondary Schools
www.learnict.it
Inserting Modules
While Python includes a lot of functions like print, it leaves out a lot of functions that we can load
into our code if we need it. Think of a module as a book with instructions on how to do
something. These modules are stored by Python in a library until we need our code to know how
to use them.
The code below shows an example of how to add actual time to our code.
import time This line imports the module
count = 10
while count >0:
print(count) In the code, you can see sleep. A function
count = count 1 within the time module which pauses the
time.sleep(1) code for the amount of time within the
print("Time is up!") brackets.
Task 1
Create a code to count from 0 to 10. Write the answer below and share with your teacher.
You will need the sleep function.
TASK 1 TASK 2
import time from time import sleep
count = 0 count = 0
while count <11: while count <11:
print(count) print(count)
count = count + 1 count = count + 1
time.sleep(1) sleep(x)
print("You have had 10 seconds!") print("You have had 10 seconds!")
Task 2
When importing modules into your code, Python adds many functions into the memory. This
could slow down how fast it takes to execute your code when it’s time to run it.
Within the time module, there are loads of functions that we didn’t use such as clock. We can
choose to load just the function we need from a module like this:
In order to use the function such as sleep, it now needs to be written into the code differently.
How it was written before (x is represents seconds) How it should be written now
time.sleep(x) sleep(x)
21
Python Beginners Workbook
for Secondary Schools
www.learnict.it
Use the example above to write a Python script to work out the area of a triangle asking the user
for the height and width.
Area of a triangle is height = int(input("What the height of your triangle in CM?"))
base x height ÷ 2 length = int(input("What the length of your triangle in CM?"))
area = height * length / 2
print("the area of the triangle is " + str(area) + "cm\xb2")
area = x × y ÷ 2
Now use the example above to write a Python script to work out the area of a circle by asking the
user for the radius. This is quite a hard task but you should be able to work it out if you could
work it out on a calculator. The first variable has been written for you :)
Area of a circle is r = int(input("What the radius of your circle in CM?"))
Pi x radius x radius pi=3.14
area = pi*r*r
print("the area of the circle is " + str(area) + "cm\xb2")
πr²
22
Python Beginners Workbook
for Secondary Schools
www.learnict.it
Task
1. Write a number machine function to take a 2. Write a function to take the radius of a
number and multiply it by the power of 3 & circle and work out the area. (hard, refer to
divide it by 3. (easy) variables task 3)
23
Python Beginners Workbook
for Secondary Schools
www.learnict.it
Task 1
Write a function called tax that takes the cost of a meal and adds on 22% VAT. This can be
worked out in many ways but you can refer to the equation below for help with adding VAT onto
any number (x).
x + V AT = x ÷ 100 × 122
Write your function below:
def tax(x):
vat = x * 0.20
print(x + vat)
Task 2
Write a function called warningMessage to print out the phrase:
“Danger Will Robinson \n” the number of times of the number entered when the function is
called. eg. If the number 3 is entered, it should print the phrase 3 times.
warningMessage(3)
24
Python Beginners Workbook
for Secondary Schools
www.learnict.it
Lists
A list in Python is a container that holds a list of objects (words or numbers). For example below
is a shopping list for things that need to be bought.
To create a list give it a name (the same way you did for a variable) and put items of the list in
square brackets separated by commas.
Task 1
Create a list called avengers for The Avengers characters, Captain America, Thor, The Hulk and
Iron Man.
theAvengers = ['Captain America', 'Thor', 'The Hulk', 'Iron Man']
In programming languages counting usually starts from 0. Therefore the first item in the list is 0
and the second item is 1.
We can print out single items from a list by typing the list name followed by the item number in
square brackets.
print(shopping[0])
Task 2
a. Write the code to print the third string in the avengers list.
print(theAvengers[2])
b. Write the code to concatenate: “The character who has a special hammer is “ with the 2nd
string in the list.
print("The character who has a special hammer is " + theAvengers[1])
c. Write the code to concatenate that your favourite characters are the 1st and 4th in the list.
print("My favourite Avengers are "+ theAvengers[0] + " and " + theAvengers[3])
25