Python Programming Guide
Python Programming Guide
uk
Key vocabulary
print 50 + 50
The answers to these
print 50 – 25
calculations will be printed when
print 50 * 10
the program is run.
print 50 / 5
print (3 * 6) + 2
print (8 + 7) / 3
print (20 – 10) * 5
Key vocabulary
name = ‘Molly’
print ‘Hello’, name You always put text in
This is a variable – inverted commas!
something that can food = ‘chocolate’
change. Print ‘I like to eat’, food
This is a variable –
The raw_input command makes
something that can
the user type in what they want.
change.
This is a variable –
something that can The float command tells the
change. computer the user is typing a number.
Edit and
Change this symbol to do different calculations.
improve:
6. Random numbers www.simonhaughton.co.uk
import random
number = random.randrange(10,20,1)
print number
print number + 10
print number * 10
Programming challenge:
Create a program that calculates the perimeter of a rectangle by adding
together its two lengths and two widths, inputted by the user.
8. Lists www.simonhaughton.co.uk
Edit and Put more items in the lists to make the rainbow zoo more fun!
improve:
9. Functions www.simonhaughton.co.uk
Key vocabulary
Function – A sub-program which can
This function is named cointoss. be called (run) later using its name.
import random
This is a function – a set of
def cointoss():
commands with a name that
options = [‘heads’, ‘tails’] does something (tosses a coin).
result = random.choice(options)
print result
Programming challenge:
cointoss()
cointoss() Change this function to roll a dice
cointoss() instead. Change its name from
cointoss()
cointoss to roll.
cointoss()
Then change the options to
[1, 2, 3, 4, 5, 6]
10. Conditional (if) statements www.simonhaughton.co.uk
Edit and Change the question being asked (and the answer too, if needed).
improve:
11. OR statements www.simonhaughton.co.uk
Edit and Change the question being asked (and the answer too, if needed).
improve:
Key vocabulary
Conditional (IF) statement – Decides which commands to run depending on
whether certain things (conditions) are true or false.
12. Score calculators www.simonhaughton.co.uk
score = 0
This variable sets the score to
answer = raw_input(‘Is it grass green? ’) 0 at the start.
if answer == ‘yes’ or answer == ‘YES’:
print ‘Correct’
score = score + 1 This adds 1 to the score if the
else: user answers correctly.
print ‘Wrong’
answer = raw_input(‘What is 3 + 3? ’)
The quiz uses conditional (if)
if answer == ‘6’ or answer == ‘six’: statements to print if the user
print ‘Correct’ answers correctly or not.
score = score + 1
else:
Programming challenge:
print ‘Wrong’
Create your own quiz that
print ‘Your score is’, score prints the player’s score at the
end.
13. While loops www.simonhaughton.co.uk
Key vocabulary
While loop – Commands in a while loop keep repeating until a condition is
met (e.g. the correct password is inputted).