Year 10 Intermediate Python Skills
Year 10 Intermediate Python Skills
Name:
Form:
1
Contents
Contents................................................................................................................................................................................................2
How to use this workbook:...................................................................................................................................................................2
Basic Skills Guide...................................................................................................................................................................................3
Lesson 1 – Data Structures - Two-Dimensional Lists............................................................................................................................4
Lesson 2 – Data Structures - Dictionaries.............................................................................................................................................7
Lesson 3 – Files......................................................................................................................................................................................9
Lesson 4 – Functions and Modules.....................................................................................................................................................12
Lesson 5 – Nested Control Structures.................................................................................................................................................17
Lesson 6 –Errors..................................................................................................................................................................................21
Keywords Glossary..............................................................................................................................................................................25
2
Basic Skills Guide
Python skill Example code Output Explanation Video
Output text Program will output some text video -
to the screen. print
3
the computer it is a number.
Mathematical First, computer will calculate video -
operations the sum inside the brackets. Maths
with integers Next, it will execute the print
command and print the
answer to the screen.
Operator Operation
+ Add
- Subtract
/ Divide
* Multiply
% Modulus
** Exponent
// Floor
division
Mathematical Same as above, but using video -
operations integers (whole numbers) and Maths with
with floats floats (real numbers). floats
Operator Operation
== Equal to
< Less than
<= Less than or
equal to
> More than
>= More than or
equal to
!= Not equal to
4
condition.
When i is equal to or greater
than 10 the loop will end.
While loop Asks the user to enter a video -
colour. Tests if this colour is While loop
equal to “Red”. If not, asks input
the user to try again and
keeps repeating this until the
user types “Red”.
For loop for variableName in video - for
collection: loop
5
If statement If the value in score is greater video - if
than 80 the program will
print “Pass”, otherwise it will
do nothing.
6
Lesson 1 – Data Structures - Two-Dimensional Lists
ACTIVITY 1 – TRY IT OUT
Complete the table below by reproducing the code, snip the output and write an explanation. Use the video links to assist where
available.
element
s of a
Watch Demonstration Video
2D list
listOfLists [[1,2,3],[4,5,6],[7,8,9]]
Editing listOfLists[0][1] 100
element OUTPUT listOfLists
s of a
2D list
Watch Demonstration Video
a [[25,67,92],[“Sam”,”Tom”,”Tim”]]
Mixed OUTPUT a[0]
2D list OUTPUT a[1]
OUTPUT[a[1][0]]
skills OUTPUT[a[1][0][1]]
7
ACTIVITY 2 – DO IT YOURSELF
2. Now edit the middle row and column to hold ‘z’ instead.
There are a number of different ways to achieve this. Try to
keep your solution simple. When you re-print each row you
should see the following output:
8
ACTIVITY 3 - ANNOTATE IT
Watch the video and annotate the code below. You must describe the purpose of each identified part of the code, and any
python skills that are being used in these places.
Note: ignore any instructions in the video that relate to the old paper-based workbook
Type your answer here
9
Lesson 2 – Data Structures - Dictionaries
A dictionary data structure combines each data item (value) with a name (key). It looks a little like this:
Key Value
Name “Rafid”
Age 12
Favourite_Colour “Blue”
Complete the table below by reproducing the code, snip the output and write an explanation. Use the video links to assist where
available.
Editing userData[“Age”] 13
values in a OUTPUT userData[“Age”]
dictionary
ACTIVITY 2 – DO IT YOURSELF
1. Create a dictionary with at least 3 items of data about yourself, then print the full dictionary to the screen.
10
2. Edit your dictionary to change two items of data about yourself, then print the full dictionary to the screen.
3. Add a new item of information to your dictionary, then print the full dictionary to the screen.
4. Use a for loop to print each pair from your dictionary in the format: “My “ + key + “ is “ + value.
11
Lesson 3 – Files
ACTIVITY 1 – TRY IT OUT
Complete the table below by reproducing the code, snip the output and write an explanation. Use the video links to assist where
available.
Python Pseudocode Example code and video link Output and File Contents Explanation
skill
(to be added by student) (to be added by student)
file
Writing to open(‘houses.txt’,’w’)
a text file file.write(‘Watson\nPascal\
nEinstein\nRutherford\n’)
file.write(‘Kelvin\
Watch Demonstration Video
nIsambard\nNewton’)
file.close()
file open(‘houses.txt’,’r’)
Reading text file.read()
from a OUTPUT text
text file file.close()
ACTIVITY 2 – DO IT YOURSELF
1. Create a program that asks the user to enter the names of 5 subjects. It will then save these names to a file.
2. Create a new program that provides the user with two options: read the list of subjects or add a new subject. If they
choose the first option, it should open the file and print out the contents. If they choose the second option it should
add a new subject without deleting the original ones.
12
Type your pseudocode here
1. What do the ‘w’, ‘r’ and ‘a’ options refer to when you open a file?
2. What happens if someone else tries to use a file while you have it open (e.g. for write)?
13
Type your answer here
14
Lesson 4 – Functions and Modules
ACTIVITY 1 – TRY IT OUT
Complete the table below by reproducing the code, snip the output and write an explanation. Use the video links to assist where
available.
X myFunction(“Ada”)
OUTPUT x
ACTIVITY 2 – DO IT YOURSELF
In the main program input a value and then call the function.
15
Challenge: offer the choice between °F to °C or °C to °F. To help here is the re-arranged formula
16
ACTIVITY 3 - ANNOTATE IT
Watch the video and annotate the code below. You must describe the purpose of each identified part of the code, and any
python skills that are being used in these places.
Note: ignore any instructions in the video that relate to the old paper-based workbook
17
ACTIVITY 4 – LOCAL AND GLOBAL VARIABLES
Local variables are created inside a function and can only be “seen” and used inside a function. In the example below s is
a local variable. When this code runs it will produce an error because the print statement cannot see the variable s.
Global variables are created outside a function and can be seen inside any functions. To be allowed to edit them inside a
function you must state that the variable is global first. If you then make any changes to the global variable they will be
seen everywhere outside the function as well.
WARNING: Global variables should be used only when absolutely needed. If you have a choice it is always better to use
local variables. This is because they are cleaned up at the end of the function and prevent confusion if other parts of the
code use the same variable names.
Review the teacher’s example again from activity 3. For each variable below, decide if these have local or global scope:
value
fromUnit
result
inputAmount
mbAmount
18
ACTIVITY 5 – DO IT YOURSELF - MODULES
File : mymaths.py1
1. Create a file mymaths.py
2. Inside this file, define the following two subroutines
• Add: has two numbers as parameters and returns the result of these added together
• Subtract: has two numbers as parameters and returns the result of the second number subtracted from the
first number
File 2: main.py
1. Create a file main.py
2. Inside this file write the line: import mymath
3. Call both your functions to test them using
mymath.add(…)
MY MATHS
MAIN
mymaths.py
main.py
19
20
Lesson 5 – Nested Control Structures
ACTIVITY 1 – TRY IT OUT
Complete the table below by reproducing the code, snip the output and write an explanation. Use the video links to assist where
available.
Python Pseudocode Example code and video link Output (only snip last Explanation
skill portion of long outputs)
(to be added by student)
(to be added by student)
Nested a1
b2
IF IF a < b THEN
statem IF a > 0 THEN
OUTPUT (“A”)
ents ENDIF
ENDIF
Mixed FOR i 0 TO 10
j0
nesting WHILE j < 0
OUTPUT j
jj+1
ENDWHILE
ENDFOR
ACTIVITY 2 - ANSWER IT
Term Meaning
Sequence
Iteration
Selection
Control Structure
ACTIVITY 3 - DO IT YOURSELF
21
1. Convert each pseudocode snippit into Python and then explain its purpose below
2. Write a program that asks the user which times table they would like to print. Then print this times table up to 12x the
number specified. After this has been printed it should ask the user for another number, and repeat the process.
22
Type your pseudocode here
23
ACTIVITY 4 - ANNOTATE IT
Watch the video and annotate the code below. You must describe the purpose of each identified part of the code, and any
python skills that are being used in these places.
Note: ignore any instructions in the video that relate to the old paper-based workbook
24
Lesson 6 –Errors
ACTIVITY 1 – IDENTIFYING ERRORS
1. Research and define the meaning of the three different types of errors below.
Example State which type of error is shown and explain why you
have chosen this type.
Code
Output
Code
Output
Code
Output
25
ACTIVITY 2 – UNDERSTANDING RUNTIME ERRORS
A runtime error will first display a trace that tells you where the error occurred in the code. Then it will give you the name of the
error:
Location of error
Name of error
There are a selection of code examples and their runtime errors below. Research each type of runtime error and explain why it
occurred in the example code.
26
ACTIVITY 3 – FIXING ERRORS
Copy and paste the code below into your Python editor. Then complete the tasks.
word = ['A','P','P','L','E')
guesses = ['','','','','')
completed = False
while not completed
1. Run the code and fix the two syntax errors that are highlighted.
2. Run the code and fix the two runtime errors:
a) Enter any letter to trigger the first runtime error (Name Error)
b) Enter E to trigger the second error (Index Error)
3. Something still isn’t working correctly because it has a logical error. You can demonstrate this if you enter all the letters
correctly. The program should now exit, but instead it keeps asking for another letter. Add a print statement to the
value of completed, and any other relevant variables, so you can see their values change while the program runs.
27
SNIP and PASTE your code here
28
Keywords Glossary
Word Definition
Append (context: file)
Control structure
Data structure
Dictionary
File
For
Function / Subroutine
Global Variable
If
Iteration
Logical Error
Module
Parameter (context:
functions)
Read (context: file)
29
Return Value (context:
functions)
Runtime Error
Selection
Sequence
Syntax Error
Two-dimensional list
Value (context:
Dictionary)
While
30