A Children's Guide To Python Programming: by Simon Haughton
A Children's Guide To Python Programming: by Simon Haughton
Python programming
By Simon Haughton
print("Hello world.")
print("\n")
print("I am learning Python.")
www.simonhaughton.co.uk 2
2. Calculations and random numbers
www.simonhaughton.co.uk 3
3. Number variables and adding comments
Open the Python app and start a program.
# This is a comment.
Does text on a line starting with a hash then a
space (# ) do anything when the program is run?
www.simonhaughton.co.uk 4
4. If statements
answer = input("Do cats bark? ") What does this program do?
if answer = = "no":
print("Correct") Why do you think two equals
else:
print("Wrong")
signs are used and not just one?
Edit and • Change the question being asked (and the answer
improve: too, if needed).
Start a program.
Programming challenge:
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?
www.simonhaughton.co.uk 5
5. Lists
Edit and • Put more items in the list to make the rainbow zoo more fun!
improve:
vehicles = ["bus","car","train"]
print(vehicles[0])
print(vehicles[1])
print(vehicles[2]) Can you see what the:
vehicles.append("plane") .append, .pop,
print(vehicles) .insert and .remove
vehicles.pop(2) commands do?
vehicles.insert(2,"boat")
print(vehicles)
vehicles.remove("car")
print(vehicles)
Programming challenge:
Create a list to store some names. Add commands to: .append, .pop,
.insert and .remove names. Find out what the .sort() command does.
www.simonhaughton.co.uk 6
6. Functions
Programming challenge:
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)?
www.simonhaughton.co.uk 7
7. Iteration (looping)
password = "fish"
guess = ""
If = = means 'equal to',
while (password != guess): what does != mean?
guess = input("Enter password: ")
if password = = guess: What does a while loop do?
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 8
8. Parameters and validation
Open the Python app and start a program.
def spell(word):
for i in range(0,len(word)): Parameter - A way of passing a
print(word[i])
value from the main program to a
spell("said") function when it is called (run).
spell("because")
Programming challenge:
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)!
def validation():
number = 0 What is the
while True:
try: purpose of this
number = int(input("Type a whole number: ")) function?
except ValueError:
print("Not a whole number!") How could it
else:
return(number) be useful?
x = validation()
Programming challenge:
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.
www.simonhaughton.co.uk 9
9. Algorithms
Algorithm - An explanation of a the processes or instructions a program carries
out, usually described in a flowchart.
Programming challenge:
Create a simple version of a Snakes and Ladders game:
move function
Start
Press the
keyboard to roll
Set position = 0
Set dicenumber as a
random number Is
between 1 and 6. yes Print a "Well
position
done" message
> 100?
position = no
position +
dicenumber Run move function Stop
no
Stop
• 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.
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
www.simonhaughton.co.uk 10