Session 2
Variables, Input and Casting
Original Copyright Ocado Group 2024
What we are learning about today
At the end of today’s lesson, we will have covered:
● Storing data using variables
● Getting input from the user
● Convert values between different data types using casting
Original Copyright Ocado Group 2024
Starter
Why did the + operator do different things here?
Code Output
Original Copyright Ocado Group 2024
The use of the + operator
In the first example, the two operands are numbers so they are added together
In the second example, the two operands are strings so they are concatenated
(joined)
Code Output
Original Copyright Ocado Group 2024
The use of the + operators
Concatenation is the process of joining two strings together
Original Copyright Ocado Group 2024
The use of the + operator
You can’t mix operand data types when using the + operator in Python
Can you explain this error message?
We will come back to how to fix it later
Original Copyright Ocado Group 2024
Activity 1: Investigate
Original Copyright Ocado Group 2024
Activity 1
Investigate
Original Copyright Ocado Group 2024
Activity 1
Investigate
What effect does the comma have in the print statement?
Original Copyright Ocado Group 2024
Key Concept: Variables
A variable is a name given to a storage location in the computer’s memory
It holds some information, which we call its value
Variable names should be meaningful
Variable names should start with a lowercase letter with words separated by
underscores
❌ ✅
Original Copyright Ocado Group 2024
Key Concept: Variables
You might like to think of a variable as a box where you can store a value
The variable name is a label to help you identify the correct box to use
14
Python code: age = 14
age
Photo by Lia Trevarthen on Unsplash
Original Copyright Ocado Group 2024
Activity 2: Variables
Original Copyright Ocado Group 2024
Activity 2
Variable naming rules and conventions
Variable names must describe what you are using them for
first_name ✅ use underscores to separate words
2nd ❌ you can’t start a variable name with a number
users_first_name ❌ this is too long, you might make errors
Fill in t
f ❌ too short; avoid single-letter variable names h
worksh e
e et
First ❌ don’t start variable names with a capital letter gaps
first-name ❌ no punctuation other than an underscore is allowed
Original Copyright Ocado Group 2024
Activity 2
Variable naming rules and conventions
Check your work
Variable name example Okay? Explanation
high_score ✅
x ❌ Single-letter
Surname ❌ Starts with a capital letter
[Link] ❌ Has a full-stop in it
the_highest_value_scored ❌ Too long
Original Copyright Ocado Group 2024
Activity 3: Printing variables
Original Copyright Ocado Group 2024
Activity 3
Predict
1
Look at the examples
In pairs, write down what
you predict they will output 2
Do not run them yet
Original Copyright Ocado Group 2024
Activity 3
Run
1
Now run them
Did they do what you
expected? 2
Fill in the worksheet notes
Original Copyright Ocado Group 2024
Activity 3
Run
1
Original Copyright Ocado Group 2024
Input
The input command
allows you to ask the user
to enter some values
The value entered is
stored in the variable
specified
You can see this from the
output of this program
Original Copyright Ocado Group 2024
Activity 3
Investigate
What happens if we 1
change the code?
Try these examples one by 2
one
Original Copyright Ocado Group 2024
Activity 3
Investigate
1
What is causing this error?
How can we fix it?
Original Copyright Ocado Group 2024
Activity 3
Investigate
We must store a value in the name variable before we try to use it
Swap the lines over so the input line comes first
Original Copyright Ocado Group 2024
Activity 3
Investigate
2
What is causing this error?
How can we fix it?
Original Copyright Ocado Group 2024
Activity 3
Investigate
We must store the input in the hobby variable
Add hobby = to the start of the second input line
Original Copyright Ocado Group 2024
Activity 3
Investigate
3
What does this error message mean?
What is causing it?
Original Copyright Ocado Group 2024
Activity 3
Investigate
Python is case sensitive, so variables COUNTRY and country are
two different variables
Change the input statement to use the variable, country
Original Copyright Ocado Group 2024
Activity 3
Modify
Change this so it asks the
user for the name of a
game and their high score
instead
Change the print
statements appropriately
Original Copyright Ocado Group 2024
Activity 3
Modify - Example Solution
Original Copyright Ocado Group 2024
Activity 3
Make
Using this code as a
guide, write a program that
asks the user for the name
of their favourite animal,
where it lives and its
favourite food
Then output a message
about this.
Original Copyright Ocado Group 2024
Activity 3
Make - Example Solution
Write a program that asks the user for the name of their favourite animal, where
it lives and its favourite food
Then output a message about this.
Original Copyright Ocado Group 2024
Activity 4: Casting
Original Copyright Ocado Group 2024
Activity 4
Investigate
What happens if you run this code?
Original Copyright Ocado Group 2024
Activity 4
Investigate
Can you explain this?
Original Copyright Ocado Group 2024
Activity 4
Investigate
Did you remember that the + operator can concatenate strings?
When you use input, it always gives you a string back
Sometimes we will want to convert this into a number
Original Copyright Ocado Group 2024
Casting - changing datatype
You can use these functions to cast a variable from one datatype to another:
int() - convert to an integer
float() - convert to a floating-point number
Original Copyright Ocado Group 2024
Casting - changing datatype
Now do the exercises
Original Copyright Ocado Group 2024
Activity 5: Casting Exercises
Original Copyright Ocado Group 2024
Activity 5
Casting Exercises - Solutions
1. Contrary to popular belief, millipedes don’t have 1000 legs. Giant African
millipedes have around 160 legs.
Complete this code to output how many legs your millipedes have.
Original Copyright Ocado Group 2024
Activity 5
Casting Exercises - Solutions
2. Ask the user how tall they are in centimetres and output their height in metres.
Original Copyright Ocado Group 2024
Activity 5
Casting Exercises - Solutions
3. To convert degrees Celsius to Fahrenheit, you multiply by 1.8 and add 32.
Fix this code to ask the user to enter a temperature in Celsius as a floating-point
number and output the temperature in Fahrenheit.
Original Copyright Ocado Group 2024
Plenary
We have covered:
● Storing data using variables
● Getting input from the user
● Converting between data types using casting
EXIT
T
TICKE What is wrong with this line of Python code?
Original Copyright Ocado Group 2024
Original Copyright Ocado Group 2024