GCSE
Practical programming skills in Python
Fundamentals
1
Topic 1
GCSE
Practical programming skills in Python
Fundamentals
1
Topic 1
Be able to identify and correct common errors in computer programs
Be able to use inputs, outputs, arithmetic and string handling
Objectives
Starter activity
Find 8 errors in the following code:
age = input("Enter your age: ")
If age = 18:
print("You are just an adult"
else if age < 18:
print("You are an adult")
else
print("You are not an adult)
Starter activity Answers
Find 8 errors in the following code:
age = int(input("Enter your age: "))
if age == 18:
print("You are just an adult")
elif age > 18:
print("You are an adult")
else:
print("You are not an adult")
Debugging
Debugging is the process of removing bugs from your programs
There are three main types of error:
Syntax error: e.g. If age = 18
Logic error, e.g. if age < 18:
print("You are an adult")
Runtime error:
e.g. age = int(input("Enter your age: "))
then entering text instead of an integer
Worksheet 1
Complete Question 1
Inputs and outputs
Printing a message to the screen in Python is pretty straightforward
print("Hello, world!")
Quite often you will need to print a "string literal" and the value of a variable together
print("Hello" , name)
print("Hello " + name)
Inputs and outputs
If you use a comma then you don’t need to worry about data types BUT you can’t control the spacing
print(name, "owes £", amount)
>>> Dave owes £ 3.75
If you use a plus sign then you do need to worry about data types BUT you can control the spacing
print(name + " owes £" + str(amount))
>>> Dave owes £3.75
Inputs and outputs
You can’t use commas in an input, so there are two possible solutions:
print("When did you turn", age)
date = input()
When did you turn 18
May
or
date = input("When did you turn " + str(age) + " ")
When did you turn 18 May
Inputs and outputs
If your input is a number then you need to change the input statement to make the data type clear
name = input("Enter employee name: ")
age = int(input("Enter their age: "))
wage = float(input("Hourly wage: "))
Data Types and casting
In Python you only need to know four data types:
String – A piece of text
str() or a mixture of letters and numbers
Integer – A whole number
int()
Float – A number with a decimal point
float() known as Real in some languages
Boolean – True or False
bool() Note the capital letters
Worksheet 1
Complete Question 2
Arithmetic
The main arithmetic operators are straightforward
num1 = 9
num2 = 3
print(num1 + num2) 12
print(num1 - num2) 6
print(num1 * num2) 27
print(num1 / num2) 3.0
Dividing two numbers always gives a float
Arithmetic rules
BIDMAS/BODMAS rules still apply
num1 = 9
num2 = 3
answer = num1 + num2 * num2
print(answer) 18
answer = (num1 + num2) * num2
print(answer) 36
Arithmetic functions
There are several useful functions:
pi = 3.1415927
num1 = round(pi) 3
num2 = round(pi, 3) 3.142
# Given that 7 / 2 = 3.5
7 // 2 # floor division 3
7 % 2 # modulo (remainder) 1
Roots and powers
Examples of roots and powers include:
# Powers
5 ** 2 # 52 25
5 ** 3 # 53 125
# Roots
25 ** (1/2) # square root 5
125 ** (1/3) # cube root 3√125 5
Worksheet 1
Complete Questions 3 and 4
Plenary
Find 8 errors in the following code:
max = 10
valid = True
mynum= input("Pick a number up to" max)
if mynum < Max:
valid = False
if valid != "false":
result = mynum * 3
print(str(mynum)+" cubed = " + int(result))
else
print("Error, too big")
Plenary Answers
Find 8 errors in the following code:
max = 10
valid = True
mynum = int(input("Pick a number up to"
+ str(max) + ": "))
if mynum > max:
valid = False
if valid != False:
result = mynum ** 3
print(str(mynum) + " cubed = " + str(result))
else:
print("Error, too big")