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

01 Python Continued - Lesson 1

This document is a lesson plan for teaching Python programming to Key Stage 3 students, focusing on outputs, inputs, data types, and basic operations. It explains the use of print() and input() statements, the concept of strings and variables, and introduces casting for numerical operations. The document also includes practical tasks for students to apply their learning through coding exercises.

Uploaded by

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

01 Python Continued - Lesson 1

This document is a lesson plan for teaching Python programming to Key Stage 3 students, focusing on outputs, inputs, data types, and basic operations. It explains the use of print() and input() statements, the concept of strings and variables, and introduces casting for numerical operations. The document also includes practical tasks for students to apply their learning through coding exercises.

Uploaded by

y.n0zille
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Computer Science FPS Key Stage 3 Computing

Python Continued
Do Now: https://forms.office.com/r/T9C0gvWs7t
Python Editor: Coder – CSUK:Coder

Introduction
In this unit, we will to learn how to code, using the python programming language.

Programming Outputs
In python, we can output data to the screen using a print() statement.
The print() statement simply works by
outputting the contents of its brackets to the
display.
If we wish it to print some text, the text must
be surrounded by quotes, as shown in the
example image.

Programming Inputs & Storing Inputs


In python, we can ask the user to enter an input by using an input() statement and
assigning it to a variable.
Let’s have a look at what that means!

The ‘input’ statement allows


our program to ask the user to
enter an input.

The ‘answer’ variable, which


has been assigned to the input
statement, stores the input.

And we could use the answer


variable with a ‘print’
statement to output the
inputted value.

Output

1
Computer Science FPS Key Stage 3 Computing

Understanding the major difference between a print() and an input()


statement
When a print() statement is run by the computer, it will cause the computer to display
a message, and then the program will
immediately continue to run the next
line of code.
When an input() statement is run by the computer, it will
also cause the computer to display a message, however the
program will pause and wait for a user input!
So, it needs to have a variable assigned to
it if we want to store what the user types in.

What the user types in (e.g. their


name) will be stored in the assigned
variable.

Strings, Variables and Quotes


In programming, text is known as ‘strings’. A ‘string’ is a data type that consists of
various keyboard characters. And as we seen previously, variables are memory
locations (storage areas) which can contain strings (text).
When we wish to output strings in a print() or input() statement, we must use quotes.
For example, the following code would output the string Bob onto the
screen.
If we don’t use quotes, python will assume that we wish to output a variable of that
name. For example, the following code would cause the program
to look for a variable called Bob and output its contents. And if the
variable doesn’t exist, the program would output an error.

Concatenation
In python, if we wish to join a string and a variable (in a print() or input() statement),
so to form a long sentence, we can achieve this using the ‘+’ operator.

In the example code above, you can see that the program has a variable called
‘name’, which contains the string ‘Jimmy’.
In the print() statement underneath, the ‘+’ operator has been used to join
(concatenate) the string “Hello ”, with the contents of the variable, and with another
string “, it’s good to meet you!”, to form the sentence “Hello Jimmy, it’s good to meet
you!”.
When using the ‘+’ operator to form sentences, remember to add spaces in your
strings in the appropriate positions, otherwise two words may be joined together in
your sentence.

2
Computer Science FPS Key Stage 3 Computing

Data Types
Previously we discussed how text is known as ‘strings’ in the python programming
language.
A ‘string’ is a data type which consists of combinations of keyboard characters.
Programs need to know what type of data it is working with so that it can process the
data correctly.
There are of course other data types and we will now look at one of these (the integer
data type), so that we can create programs that work with numbers.

Programming a Calculator
You might expect that the following program code would ask the user to enter two
numbers and would then output their sum. And it makes sense to expect this because
the first two lines clearly ask the user to enter numbers and stores these inputs in
variables num1 and num2. And then the program clearly adds the contents of these
variables together and then outputs the result.

However, when the user types in 5 for the first input and 5 again for the second input,
the resulting output is actually 55! Surely it should be 10?

Output

Why is the answer not 10?


The reason is because the system has not been told what type of data has been
entered into the variables and so it assumes that they are strings. As we learnt
before, the ‘+’ operator will join together two strings. So when 5 and 5 were entered,
python recognised these as symbols and not numbers, and so joined them together to
form the string ‘55’.
How do we get our program to recognise numbers and perform calculations?

3
Computer Science FPS Key Stage 3 Computing

In order to get our programs to effectively work with numbers, we need to carry out
something known as casting. This is where we convert a variable’s data to another
data type.
The int() function allows us to cast data into integers.

As you can see, the program now has two new lines, which allows it to correctly
perform the correct calculation.
The int() function, effectively tells the system that the contents of the variable is to be
an integer and not a string.

So hopefully the above has given you a reminder of how we can program outputs and
inputs in Python. However, the most effective way of truly remember how
programming works, is to get stuck into some practical programming tasks. Below are
a series of tasks designed to guide you through the basics of programming in the
python programming language.

PRIMM TASKS

4
Computer Science FPS Key Stage 3 Computing

1: Predict

Code

What do you think the code will do?


It will add the two numbers given

Write the program’s output exactly as you think it will appear.

>>>
Predictio num1 = input ("Please enter a number: ")
ns num2 = input ("Please enter another number: ")

num1 = int(num1)
num2 = int(num2)
answer = num1 + num2
Number 1= 6
Number 2= 6
Answer= 12

2: Run

Now type in the code and run the program.


Show a screen shot of the actual output below.

Was it the same as your Yes No


prediction?
Were there differences? If so, show or describe these below.
No there were not any differences as shown below.

5
Computer Science FPS Key Stage 3 Computing

3: Investigate
What happens if you enclose It would round it to the nearest number.
both of the input statements
with the int() function?

(e.g. number1 = int(input(“Enter a


number”))

What does the int() function It would round the number down to the nearest number
do?

Explain the result of the code


alteration.

4: Modify
Modify the code so that it Screenshot /Explanation
requests 3 numbers to be
inputted and multiplies
them instead of adding
them.

Explain what your code is


doing.

5: Make
Create a program in python for each point below and add a screenshot of your code
in the boxes provided:
1) Displays a message of your choice to the screen.

6
Computer Science FPS Key Stage 3 Computing

2) Asks the user for their favourite colour and then comments on the colour entered (for
example, “**colour entered**, is a nice colour!”.

3) Asks the user for their age and then displays what their age will be in 50 years.

4) Asks the user for the dimensions for a box and then works out its volume.

5) Asks the user for their weekly pocket money, weekly phone bill, money spent on food each
week and money spent on seeing friends each week. The program is to then display how
much pocket money will be left when the week is over.

7
Computer Science FPS Key Stage 3 Computing

Exit Ticket: https://forms.office.com/r/mYgpKcYKTB

You might also like