ABC of Python Programming Grade 7
ABC of Python Programming Grade 7
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. Arrays
veg = random.choice(vegarray)
print("Mashed",veg," on the ceiling.") What does this program do?
veg = random.choice(vegarray)
print("Boiled",veg," on the floor.") What is the purpose of the
veg = random.choice(vegarray) vegarray?
print("Stewed",veg," in the corner.")
veg = random.choice(vegarray)
print(veg," upon the door.")
Array - A list of values.
Edit and • Put more items in the array to make the poem more interesting.
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 an array to store a list of 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 an array:
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.
Programming challenge:
Create a function that prints the biggest of two values.
In the main program below it, the user will input two integers and they will
be passed to the function as parameters.
Programming challenge:
Create a function which prints whether a if side1 == side2 == side3:
triangle is: equilateral, scalene or isosceles. print("Equilateral")
elif side1 == side2:
In the main program below it, the user will print("Isosceles")
elif side2 == side3:
input the triangle's three side lengths as print("Isosceles")
elif side1 == side3:
integers (checked with the validation print("Isosceles")
function above) and they will be passed to else:
print("Scalene")
the function as parameters.
www.simonhaughton.co.uk 9