01 Python Continued - Lesson 1
01 Python Continued - Lesson 1
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.
Output
1
Computer Science FPS Key Stage 3 Computing
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
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
>>>
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
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?
What does the int() function It would round the number down to the nearest number
do?
4: Modify
Modify the code so that it Screenshot /Explanation
requests 3 numbers to be
inputted and multiplies
them instead of adding
them.
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