0% found this document useful (0 votes)
85 views

Year 10 Intermediate Python Skills

This document provides a workbook for a Year 10 Computer Science programming course on intermediate Python skills. It includes 6 lessons on various Python concepts like data structures, files, functions, and errors. Each lesson includes example code, expected output, explanations, and links to instructional videos. It also includes a basic skills guide that outlines foundational Python concepts like printing, variables, input, math operations, comparisons, and loops with example code and videos.

Uploaded by

Logan Surman
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views

Year 10 Intermediate Python Skills

This document provides a workbook for a Year 10 Computer Science programming course on intermediate Python skills. It includes 6 lessons on various Python concepts like data structures, files, functions, and errors. Each lesson includes example code, expected output, explanations, and links to instructional videos. It also includes a basic skills guide that outlines foundational Python concepts like printing, variables, input, math operations, comparisons, and loops with example code and videos.

Uploaded by

Logan Surman
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 30

Year 10 Computer Science

Programming 4 – 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

How to use this workbook:


1. Save a copy to your unit folder
2. Put your name and form on the front
3. For every task:
a. Plan your code by typing pseudocode into the red box
b. Snip and paste python into the blue box
c. Type answers into the green box
d. Save as often as you can!

2
Basic Skills Guide
Python skill Example code Output Explanation Video
Output text Program will output some text video -
to the screen. print

Output text Program will output some text


to the screen.

Output text Program will output multiple video -


lines of text to the screen. print
Each print statement starts a multiple
new line.

String + will join together two strings video -


concatenation (pieces of text) print with
concatena
te
Variables Data may be stored in a video -
variable so it can be print
remembered later. variable
Variables have a name and a
space in memory where they
keep a value.
You assign data to a variable
using =.
When you use the name of
the variable the computer will
fetch the value that is stored
inside it.
Input text The input command displays video -
the text, waits for the user to input text
type something and then press
enter.
The information the user
typed is stored in the variable
username.
The print command will
output the text shown joined
to the text saved in the
variable username.
Input whole The int() command is used to video -
numbers tell the program that the data input
is a whole number. The numbers
program will run the input()
command first, then the int()
command will convert the
information to a number, then
it will be stored in the variable.
If you want the variables to be
treated as numbers, so you
can do arithmetic, then you
must use a command to tell

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

Comparison Comparison operations will video -


Operators compare the left and right Comparison
sides and provide an answer
of True or False. E.g. 1 equal
to 1, will produce the answer
True.

Operator Operation
== Equal to
< Less than
<= Less than or
equal to
> More than
>= More than or
equal to
!= Not equal to

While loop Creates a variable called i and video -


assigns the number 0 to this While loop
variable. counting
Tests if i is less than 10, and if
true, executes the code
indented below the while
statement.
Prints i to the screen and then
increases i by 1, before
returning to the top of the
while statement to retest the

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

This will repeat the block of


code indented below once for
each item in the collection.
Each time the code repeats,
the variable will contain the
next item in the collection.

In this example, the variable i


will point in turn to each
number between 0 and 10
(not including 10).
Complex Store numbers in variables.
calculations Combine these with
mathematical operators and
store the result in another
variable.
Print the variable holding the
result.
Finding out the The type() command will tell video -
data type us the type of any piece of data types
data.
Integers are whole numbers.
Floats are real numbers i.e.
they have a decimal place.
Strings are any collection of
characters (letters, numbers,
symbols)
Changing the You can change the type of a video -
data type piece of data using the int(), data type
float() or str() commands. conversion
This may result in some
information being lost, for
example a float -> integer
conversion will delete the
decimal place.
If the conversion is not
possible, an error will be
thrown.

5
If statement If the value in score is greater video - if
than 80 the program will
print “Pass”, otherwise it will
do nothing.

If/Else If the value in score is creater video - if


statement than 80 the program will else
print “Pass”, otherwise it will
print “Fail”.

If/Elif/Else The first test must always video - if


statement follow an IF statement, but if elif else
further tests are needed,
they should use the ELIF key
word (short for else if). If all
the if and elif tests fail, the
else code will execute last.
If must always come first, but
using elif and else are
optional.

Functions Defining a function creates a video -


block of code, with a name, Simple
but does not execute the Function
code.

Calling a function uses the


name of the function and
tells it to go and execute that
block of code.

Functions Defining a function creates a video -


block of code, with a name, Double
but does not execute the function
code. Between the brackets
the function may specify one
or more parameters that
must be specified when the
function is called. The return
key word will pass data back
to the calling code.

Calling a function uses the


name of the function and
tells it to go and execute that
block of code. Any data that
is returned may be assigned
to a variable using =.

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.

Optional video – revise basic lists

Python Pseudocode Example code and video link Output Explanation


skill
(to be added by student) (to be added by student)
listOfLists  [[1,2,3],[4,5,6],[7,8,9]]
Creating OUTPUT listOfLists
a 2D list
Watch Demonstration Video
For loop FOR l  listOfLists
OUTPUT l
with a ENDFOR
list
listOfLists  [[1,2,3],[4,5,6],[7,8,9]]
Accessin a  listOfLists[0][1]
g OUTPUT a

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]]

Watch Demonstration Video

7
ACTIVITY 2 – DO IT YOURSELF

1. Create 5x5 grid using a two-dimensional array that stores


the first 25 letters of the alphabet. Use a for loop to print
each row to the screen to produce the following output:

Type your pseudocode here

SNIP and PASTE your code here

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:

Type your pseudocode here

SNIP and PASTE your code here

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

Type your answer here

Type your answer here

Type your answer here

Type your answer here

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”

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.

Watch Demonstration Video

Python Pseudocode Example code and video link Output Explanation


skill
(to be added by student) (to be added by student)
Creating a userData 
dictionary {“Name”:”Rafid”,”Age”:12,
”Favourite_Colour”:”Blue”}
Accessing OUTPUT
values in a userData[“Name”]
OUTPUT userData[“Age”]
dictionary
OUTPUT
userData[“Favourite_Colou
r”]
Adding userData[“ShoeSize”]  5
values to a OUTPUT
userData[“ShoeSize”]
dictionary

Editing userData[“Age”]  13
values in a OUTPUT userData[“Age”]
dictionary

Using FOR key in userData.keys()


OUTPUT key
dictionarie ENDFOR
s with
loops FOR values in userData.values()
OUTPUT value
ENDFOR

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.

Type your pseudocode here

SNIP and PASTE your code here

10
2. Edit your dictionary to change two items of data about yourself, then print the full dictionary to the screen.

Type your pseudocode here

SNIP and PASTE your code here

3. Add a new item of information to your dictionary, then print the full dictionary to the screen.

Type your pseudocode here

SNIP and PASTE your code here

4. Use a for loop to print each pair from your dictionary in the format: “My “ + key + “ is “ + value.

Type your pseudocode here

SNIP and PASTE your code here

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()

Watch Demonstration Video


file 
Appending open(‘houses.txt’,’a’)
to a text file.write(‘Einstein’)
file file.clouse()
Watch Demonstration Video

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.

Type your pseudocode here

SNIP and PASTE your code here

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

SNIP and PASTE your code here

ACTIVITY 3 – FIND IT OUT

1. What do the ‘w’, ‘r’ and ‘a’ options refer to when you open a file?

Type your answer here

2. What happens if someone else tries to use a file while you have it open (e.g. for write)?

Type your answer here

3. What happens when you close the file?

Type your answer here

4. What happens if you try to use a file after closing it?

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.

Python Pseudocode Example code and video link Output Explanation


skill (refer to subroutines (to be added by student) (to be added by student)
instead of functions)
SUBROUTINE myFunction()
Revise: OUTPUT “Hello”
Simple ENDSUBROUTINE
functions
myFunction()

Watch Demonstration Video


SUBROUTINE
Revise: myFunction(name)
Single OUTPUT “Hello”,name
function ENDSUBROUTINE
parameter
myFunction(“Ada”)

Watch Demonstration Video


SUBROUTINE
Revise: myFunction(name, age)
Multiple OUTPUT “Hello”,name
function OUTPUT “You are”, age,
parameter “Years old”
ENDSUBROUTINE
s
Watch Demonstration Video
myFunction(“Ada”, 2)
SUBROUTINE
Revise: myFunction(name)
Return result name*3
values RETURN result
ENDSUBROUTINE

X  myFunction(“Ada”)
OUTPUT x

Watch demonstration video


Importing File: names.py File: names.py
functions
from a SUBROUTINE
separate printMultiple(name)
module OUTPUT name*3
ENDSUBROUTINE

File: demo.py File: demo.py


IMPORT names
names.printMultiple(“Ada”

Watch summary demonstration video (optional)

ACTIVITY 2 – DO IT YOURSELF

Write a function that converts degrees Celsius to Fahrenheit, where


°F = °C  x  9/5 + 32

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

°C = (°F  -  32)  x  5/9

Type your pseudocode here

SNIP and PASTE your code here

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

Type your answer here

Type your answer here

Type your answer here

Type your answer here

Type your answer here

17
ACTIVITY 4 – LOCAL AND GLOBAL VARIABLES

READ ME and TRY ME

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:

Variable name Local or global scope?


CONVERSION_UNIT

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

Type your pseudocode here

MAIN

Type your pseudocode here

mymaths.py

SNIP and PASTE your code here

main.py

SNIP and PASTE your code here

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.

Revise control structures video

Watch demonstration video

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 a1
b2
IF IF a < b THEN
statem IF a > 0 THEN
OUTPUT (“A”)
ents ENDIF
ENDIF

Nested FOR number  0 TO 5


FOR letter in “Hello”
FOR OUTPUT letter
loops ENDFOR
ENDFOR
Nested i1
WHILE i < 10
WHILE j0
loops WHILE j < i
OUTPUT j
jj+1
ENDWHILE
ii+1
ENDWHILE

Mixed FOR i  0 TO 10
j0
nesting WHILE j < 0
OUTPUT j
jj+1
ENDWHILE
ENDFOR

ACTIVITY 2 - ANSWER IT

Research each of the key programming terms.

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

Pseudocode Python Describe purpose of code here


FOR i  1 TO 12
FOR j  1 TO 12
OUTPUT i, “x”, j, “=”,
i*j
ENDFOR
ENDFOR
secretNumber = 100
REPEAT
OUTPUT “Guess a
number”
guess  USERINPUT
IF (guess =
secretNumber) THEN
OUTPUT “Correct”
ELSE
OUTPUT “Try again”
ENDIF
UNTIL guess =
secretNumber
import random
import time
size  random.randint()
WHILE True
FOR i  0 TO size
line  “*”
FOR j  0 TO size
IF (i = 0 OR i =
size-1)
line  line + “*“
ELSE
line  line + “ “
ENDIF
ENDFOR
line  line + “*”
OUTPUT line
time.sleep(1)
ENDFOR
ENDWHILE

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

SNIP and PASTE your code 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

Type your answer here

Type your answer here

Type your answer here

Type your answer here


Type your answer here

Type your answer here

Type your answer here


Type your answer here

24
Lesson 6 –Errors
ACTIVITY 1 – IDENTIFYING ERRORS

1. Research and define the meaning of the three different types of errors below.

Type of error Describe this type of error


Syntax Error
Runtime Error
Logic (Semantic) Error

2. Identify which error is shown in each example 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.

Code Runtime Error Explain what the error


means and how it can
be fixed.

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

# Ask the user to guess a letter.


guess = input("Guess a letter: ").upper()

# Use a loop to compare each letter


# in the word with guess. If they are the same,
# add this to the guess list. You will need to
# use a variable to count the index position so
# you know where to save the guess.
index = 0
for letter in word:
if letter == guess:
guesses[index+1) = guess
index = index + 1

# Print the user’s guess list.


print(guesses

# Loop through your list of guesses and check


# if there are any blank spaces left. If there
# are blank spaces, then the completed variable
# should be set to false. Otherwise it should be true.
completed = False
for letter in guessed:
if letter == '':
completed = False

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)

Close (context: file)

Control structure

Data structure

Dictionary

Dynamic data structure

File

For

Function / Subroutine

Global Variable

If

Immutable data structure

Iteration

Key (context: Dictionary)

Key-Value Pair (context:


Dictionary
Local Variable

Logical Error

Module

Mutable data structure

Nested control structure

Open (context: file)

Parameter (context:
functions)
Read (context: file)

29
Return Value (context:
functions)
Runtime Error

Selection

Sequence

Static data structure

Syntax Error

Two-dimensional list

Value (context:
Dictionary)
While

Write (context: file)

30

You might also like