Python
Lesson Objectives
Lesson Objectives
• Understand which data type a variable is.
• Understand why a computer needs to know what the data type
is for a given input/variable.
• Learn how to change the data type of a variable.
Lesson Outcomes
• Carry out a number of programming tasks to practice
programming simple arithmetic.
Literacy – Key Words
Input Values which get sent from the user into the computer
Variable The place where inputs get stored by the program
Output The values which get sent from the computer to the user
Programming Inputs & Storing Inputs in
PYTHON
In python:
Commenting
# Starting a line with a hash will turn your text into a comment.
(It will be ignored by the program)
Today:
Data Types and
Arithmetic
What is a data type
• What different types of data are there?
Strinsgany
an
This me keyboard Real
b i na ti on of m e a n s a n y decimal
com
te r s ( letters, Integer This
umber
chara c
s y m bols) (fractional) n
r s This means
numbe any whole
number. If
the comput
knows the d er
ata is an
integer it ca
n do maths
with the da
ta
The need to tell the computer what data type
the variable will store
• Computers store values in variables
• In order for the computer to understand our inputs, it needs to know
what type of data our input is.
• If a variable is to hold a word, we must tell the computer that the
variable is to contain a string.
• If a variable is to hold a whole number, we must tell the computer
that the variable is to contain an integer – so it can do calculations.
• If we don’t do this the program will not work.
Doing Arithmetic
• If we want our program to work with numbers we must
tell the program that certain variables will hold numbers.
• When we input a number into our program
num1 = input(“please enter a number”)
python automatically sets the variable as a string (a word,
for example twenty instead of 20)
• If we want to do maths with the ‘inputs’ we need to tell
the computer that the variable contains a number (integer)
Converting Data Types
• To do this we need to convert the variable to an ‘integer’ or
‘real’ data type.
Above you can see that two numbers have been entered and stored
in variables num1 and num2.
As python stores inputs as strings we need to convert these
variables into integers.
What’s going on?
Twent
Num1
y Nine
I’m telling the
computer that
this variable
29
29
contains a
Twent
number not a
word! y Nine
Variable Variable
Mr Int.
Once they have been converted we’re
ready to do maths!
Fiv Nin
e e
Num Num
1 2
5 9
Fiv Nin
e e
Num Num
1 2
14
Answer 5 + 9
14
Answer
14
Tasks
• Work your way through the worksheet sheet.