Lesson 1: First
steps
Year 8 – Intro to Python programming
Starter activity
Can you follow these instructions?
You have two strips of paper. One is X cm long, the other is Y cm
long.
a b c
Align the two Cut the longer Discard the part that Repeat a toc
strips at one end. strip where the you have cut off that until the strips
shorter strip ends. is the same length as are equal in
the shorter strip. length.
Measure that
length and report
it — it’s the
required result.
Starter activity
Euclid’s method: finds the LCF of X and Y
Following these instructions ❠ This is the granddaddy of all
algorithms, because it is the oldest
solves a problem: it calculates the non-trivial algorithm that has
largest common factor of X and survived to the present day.
Y. Donald Knuth
This algorithm was recorded by The Art of Computer Programming
Euclid in around 300 BC.
Have you heard of the term
‘algorithm’ before?
Unit objectives
In the previous units,
In this unit, you will...
you...
● Used a block-based programming ● Use a text-based
language (Scratch) to create programming language
programs that involved: (Python) to build on these
○ Variables, operators, and concepts and extend them
expressions
○ Sequence, selection, and
iteration
○ Subroutines
○ Lists
print("Hello Python!")
Objectives
In this lesson, you will...
● Define algorithms and programs and describe how they are
executed
● Write and execute your first Python programs
They will involve displaying messages, assigning values to variables, and
receiving input from the keyboard.
● Battle syntax errors!
Activity 1
Algorithms
An algorithm is a set of precise
instructions, expressed in some
sort of language (e.g. textual,
visual).
Understanding the language is
necessary in order to execute
the instructions.
Executing these instructions is
meant to solve a problem.
Activity 1
Programs
A program is a set of precise
instructions, expressed in a
programming language.
Translating the programming
language is necessary for a
machine to be able to execute
the instructions.
Activity 1
Python programs
To execute a Python program, you
need a Python interpreter.
This is a program that translates
and executes your Python
program.
The Python interpreter doesn’t necessarily
run on your computer.
Activity 1
Syntax
All programming languages have All languages have rules for
rules for syntax, i.e. how syntax, i.e. how sentences can
statements can be assembled. be assembled.
Programs written in a Speech or text in a language
programming language must must follow its syntax.
follow its syntax.
Programs with syntax errors Humans can infer meaning even
cannot be translated and in cases when syntax rules are
executed. violated.
For example, “tonigth see you”, instead of
“see you tonight”, will probably be
understood.
Activity 1
Syntax
All programming languages have
rules for syntax, i.e. how
statements can be assembled.
Programs written in a
programming language must In Scratch, syntax errors are not
follow its syntax. possible: rules are enforced by
Programs with syntax errors the blocks and the way they fit
cannot be translated and together.
executed. You can still make logical errors !
That’s when your program
doesn’t work the way it should.
Activity 1
Syntax
All programming languages have if remaining < 10:
print("We are getting there")
rules for syntax, i.e. how
else:
statements can be assembled. print("Still some way to go")
Programs written in a
programming language must In Python, you can (and you will)
follow its syntax. make syntax errors. You will need
Programs with syntax errors
to follow the syntax rules.
cannot be translated and
executed. Syntax errors can be frustrating when you
start learning a text-based programming
language.
Activity 1
Syntax
SyntaxError: invalid syntax
SyntaxError: Missing parentheses in call to
'print'.
SyntaxError: EOL while scanning string literal
Has this happened
YO
to
?
Don’t be overwhelmed by these
errors. They are here to
discourage the faint-hearted. You
can fix them!
U
Activity 2
Your first steps in Python
Use pair programming .
Driver
Control the keyboard and mouse.
Navigator
Provide support and instructions.
Alternate between roles.
Activity 2
Your first steps in Python
print("Hello world!") Every time you learn a new
programming language,
it’s a tradition to get your first
program to say “hello”.
Live coding
Activity 2
Walk-through of the environment
Activity 2
Your first steps in Python
Complete the first task in your
worksheet to create your own
‘Hello world’ program.
Activity 2
Your first steps in Python: commentary
print("Hello world!") Checklist:
✔ Have you spelt print correctly?
Python is case-sensitive: using capitals
makes a difference.
✔ Have you used (opening and closing) round
brackets around the message to be
displayed?
✔ Have you used (single or double) quotation
marks around the message to be
displayed?
Activity 2
Your first steps in Python: commentary
print("Hello world!") You will need the print function:
when your program must display text,
numbers, or the values of variables and
expressions.
Activity 2
Your first steps in Python
Complete the second task in
your worksheet to greet the user
by name.
Activity 2
Your first steps in Python: commentary
user = "Claude" You will need an assignment statement:
print("Hello", user) when your program must use a name
(an identifier) to keep track of a value.
user is a variable, i.e. a name for a value.
The variable user currently refers to the
value "Claude".
The quotation marks around the value show
the type of the value: it is a string (a piece
of text).
Activity 2
Your first steps in Python
Complete the third task in your
worksheet.
Activity 2
Your first steps in Python: commentary
user = "Claude" user is a variable.
print("Hello", user) It is assigned a string value.
lucky = 13 lucky is another variable.
print("My lucky number is", lucky) It is assigned an integer value.
It is useful to sketch variables and
their corresponding values, as they
change during program execution.
user "Claude"
lucky 13
Activity 2
Your first steps in Python
Complete the final task in your
worksheet, to make your
program receive input from the
user.
Activity 2
Your first steps in Python: commentary
print("What’s your name?") You will need the input function:
user = input() when your program must receive keyboard
print("Hello", user) input from the user.
When input is invoked, the program pauses,
waiting for keyboard input.
The text typed by the user is assigned to
the user variable.
We can refer to the value of user in the
program without knowing what it will be.
Activity 3
Python recap
print("Best film ever?") Locate the fragment(s) of code
film = input()
that:
print(film, "is my favourite too!") ● Display a message for the
user on the screen
● Retrieve what the user types
on the keyboard
● Assign a value to a variable
Summary
In this lesson, you... Next lesson, you will...
Defined what algorithms and Use arithmetic expressions to
programs are calculate values
Wrote and executed your first Trace the values of variables as a
Python programs (and weeded program is executed
out syntax errors)
Your programs involved displaying messages,
Write programs that receive
assigning values to variables, and receiving input numerical input from the
from the keyboard
keyboard