a children's guide to python...
TRANSCRIPT
www.simonhaughton.co.uk 2
1. Printing text and creating variables
� Open the Python app and tap .
Press and start a program.
Type a name for your program and select a folder to save it in.
� Type these commands into the 'script' window:
� Press and start a program.
Type a name for your program and select a folder to save it in.
� Type these commands in and then them:
Press and watch
the 'interpreter' window.
print("Hello world.")
print("\n")
print("I am learning Python.")
What does the print command do?
What does printing \n do?
What happens if you make a mistake in
your commands?
Program - A sequence of
commands that are followed
in order to carry out a task.
Run - Carrying out the
commands in a program. Also
known as execute.
forename = input("What is your forename? ")
print("Hello",forename)
What does the input
command do?
Does it matter if you type
in text other than your
name? Variable – A value that can be
stored and used in a program.
• Add a variable to store a surname. Then add a print
command so it prints their full name.
Edit and
improve:
print("Hi",forename,surname,"!")
www.simonhaughton.co.uk 3
2. Calculations and random numbers
� Open the Python app and tap .
Press and start a program.
Type a name for your program and select a folder to save it in.
� Type these commands in and then them:
� Press and start a program.
Type a name for your program and select a folder to save it in.
� Type these commands in and then them:
print(100+10) Is the calculation still solved if
you use a negative number or
a decimal number?
• Change the commands to do a different calculation,
such as a: take away -, multiplication * or division /.
Edit and
improve:
Testing - Trying out a program to check if it works as expected.
Debugging - Finding and correcting mistakes in a program's source code.
import random
number = random.randrange(10,20,1)
print(number)
What does the .randrange
command do?
• Change the number 10 to a smaller number and the
number 20 to a bigger number to see what effect
this has on the program.
• Add some commands to do calculations with the
random number. e.g.
Edit and
improve:
print(number+10)
www.simonhaughton.co.uk 4
3. Number variables and adding comments
� Open the Python app and start a program.
Type these commands in and then them:
� Add these commands to your program:
number = int(input("Type a whole number: "))
answer = number * 8
print(number,"multiplied by 8 is",answer)
What happens if you type in a decimal number
instead of an integer (whole number)?
• Find out what changing int to float lets you do.
(Remember to change it back to int afterwards!)
• Add commands so the answer to an addition is
printed as well. You will need to use another variable
called answer2:
• Change the program so you have to type in two
numbers at the start to use in each calculation. You
will need to use another variable called number2.
Remember to print it on the screen before you show
the answer!
Edit and
improve:
# This is a comment.
Comments - Notes in a program's code which explain
what commands do to remind you. They are not run.
Does text on a line starting with a hash then a
space (# ) do anything when the program is run?
• Type some comments beside some commands to
explain what they do.
Edit and
improve:
answer2 = number + 6
print(number,"add 6 is",answer2)
www.simonhaughton.co.uk 5
4. If statements
� Open the Python app and start a program.
Type these commands in and then them:
� Start a program.
Type these commands in and then them:
answer = input("Do cats bark? ")
if answer = = "no":
print("Correct")
else:
print("Wrong")
IF statement - Decides which commands to run depending
on whether certain things (conditions) are true or false.
What does this program do?
Why do you think two equals
signs are used and not just one?
mark = int(input("Score: "))
if mark > 80:
print("Outstanding")
elif mark > 40:
print("Great")
else:
print("Good")
• Change the question being asked (and the answer
too, if needed).
Edit and
improve:
What does this program do?
What does the elif command
let you do?
• Add another elif command in the middle so that a
score of more than 60 is rated as "Super".
Edit and
improve:
Create a program that asks a maths calculation and
prints if the user answers it right or wrong. Can you
change one of the numbers in it to a random number?
Programming challenge:
www.simonhaughton.co.uk 6
5. Lists
� Open the Python app and start a program.
Type these commands in and then them:
� Start a program, type these commands in and then them:
import random
colours = ["red","green"]
animals = ["lions","bears"]
print("My rainbow zoo has:")
colour = random.choice(colours)
animal = random.choice(animals)
print(colour,animal)
colour = random.choice(colours)
animal = random.choice(animals)
print(colour,animal)
Use copy and paste to help
you quickly copy this!
What does this program do?
What are the purposes of
the lists?
List - A set of values.
• Put more items in the list to make the rainbow zoo more fun! Edit and
improve:
vehicles = ["bus","car","train"]
print(vehicles[0])
print(vehicles[1])
print(vehicles[2])
vehicles.append("plane")
print(vehicles)
vehicles.pop(2)
vehicles.insert(2,"boat")
print(vehicles)
vehicles.remove("car")
print(vehicles)
Can you see what the:
.append, .pop,
.insert and .remove
commands do?
Create a list to store some names. Add commands to: .append, .pop,
.insert and .remove names. Find out what the .sort() command does.
Programming challenge:
www.simonhaughton.co.uk 7
6. Functions
� Open the Python app and start a program.
Type these commands in and then them:
import random
def cointoss():
options = ["heads","tails"]
result = random.choice(options)
print(result)
cointoss()
cointoss()
cointoss()
cointoss()
cointoss()
What does this program do?
Why is better to call the
function five times than to
copy all of its commands five
times?
Function - A sub-program which
is placed at the start of a bigger
program and can be called (run)
later using its name.
• Change the program so it shows the results of rolling
a six-sided dice instead. You don't need to put ""
around the options because they are numbers.
Edit and
improve:
Create a program that tells a user's fortune by calling (running) a
function two times which randomly picks a prediction from a list:
e.g. You will be given money.
You will become famous.
You will see an alien.
You will find a lost item.
You will score well in a test.
Can you ask the user to input their name so that it is included in the
predictions (e.g. Tom will be given money)?
Programming challenge:
www.simonhaughton.co.uk 8
7. Iteration (looping)
� Open the Python app and start a program.
Type these commands in and then them:
� Start a program, type these commands in and then them:
� Start a program, type these commands in and then them:
for i in range(4):
print("Hello world")
What happens if you change
4 to a different number?
Iteration - A way of repeating or looping commands multiple times.
for i in range(1,10):
print(i*10)
What happens if you change 1
and 10 to different numbers?
If = = means 'equal to',
what does != mean?
What does a while loop do?
password = "fish"
guess = ""
while (password != guess):
guess = input("Enter password: ")
if password = = guess:
print("Correct")
else:
print("Try again")
Programming challenge:
Create a program in which the computer sets the password as a random
integer from 1 to 100 and user has to correctly guess it.
Can you use: if, elif and else commands to give the user clues (e.g. "Too
high" or "Too low")? Can you add a variable which counts the number of
guesses (count = count + 1)?
www.simonhaughton.co.uk 9
8. Parameters and validation
� Open the Python app and start a program.
Type these commands in and then them:
� Start a program, type these commands in and then them:
def spell(word):
for i in range(0,len(word)):
print(word[i])
spell("said")
spell("because")
Parameter - A way of passing a
value from the main program to a
function when it is called (run).
Validation - Automatic checking by a computer to ensure that an entered value is sensible.
What is the
purpose of this
function?
How could it
be useful?
Create a function that prints the biggest of two values, passed to it in parameters.
The user will input the two integers they want to compare using the validation function.
Programming challenge:
• Insert the len(word)command to make the function print how
many letters are in the word as well.
• Change the program so you can type any word in to get passed to the
function.
• Insert the ord(word[i]) command to the iteration so the special
Unicode number of each letter is printed as the word is spelled out.
Edit and
improve:
Create a function that uses the chr(integer) command to convert a Unicode
integer you type in into a letter. You could use this to decipher a secret code made
from Unicode numbers (possibly having to add/subtract another number first as well)!
Programming challenge:
def validation():
number = 0
while True:
try:
number = int(input("Type a whole number: "))
except ValueError:
print("Not a whole number!")
else:
return(number)
x = validation()
www.simonhaughton.co.uk 10
9. Algorithms
Create a simple version of a Snakes and Ladders game:
• Can you add more print commands to display what is happening on screen?
• Can you make the game print the player's name at the end?
• Can you add another player to the game whose position is stored in a variable
called position2? You will need to make the game let each player move in
turns. You could create a variable called finished which is set to 0 at the
start and changes to 1 when a player wins, forcing the game to stop.
Programming challenge:
Algorithm - An explanation of a the processes or instructions a program carries
out, usually described in a flowchart.
Many thanks to Paul Meakin and Phil Bagge for the inspiration to learn Python and write this guide!
www.code-it.co.uk/philbagge.html
Set position = 0
Is position
> 100?
Run move function
Print a "Well
done" message
Start
Stop
yes
no
Press the
keyboard to roll
Start
no
Set dicenumber as a
random number
between 1 and 6.
position =
position +
dicenumber
Is position
on a snake or
ladder number?
Stop
Change the position so
it is more or less (use: if,
elif and else)
yes
move function
Main program