03 – Data Types
Lesson Notes
from the
Teach Python Programming With Confidence Masterclass
By Nichola Wilkin Ltd
Data types
There are three data types that you need to introduce the pupils to quite early. They
are:
• String: Text that you cannot calculate with
• Integer: Whole numbers that can be used in a calculation
• Floating-point: Numbers with a decimal point that can be used in a
calculation. AQA and OCR refer to these as “real” numbers
Setting a new value to a string
If you do not specify a data type when using an input statement then Python
assumes it is a string.
In this example Python will assume name should be stored as a string as no data
type has been specified.
Setting a new value to an integer
These are probably the most common types of data that pupils use to specify a
number.
In the example above, we ask the user to input two whole numbers (integers). It will
then add them together and display the answer.
We are using int() to surround the input statements. This tells Python that we want the
answer of whatever they type in for the input to be treated as an integer.
03 – Data Types Page 1
© Nichola Wilkin Ltd 2020
www.nicholawilkin.com
Setting a new value to a floating-point number
Instead of using an integer we could use a floating-point number that allows the user
to input decimal places.
This is the same program but allows the user to input floating-point numbers by using
float() around the input statement.
Casting data types
Casting is when you change a data type once it has already been created. It is
sometimes necessary to use a number as part of a calculation but later you may
want to concatenate that with a string in which case you must convert it to a string
to do that.
In lesson 1 we discussed the difference between using “,” and using “+” to join things
together. We used them in the print statement to combine a piece of text “Hello” to
your name.
One of the big differences is that the “,” will automatically add a space between
the two values and the “+” will not. However, there is also another difference.
The “,” will allow you to combine multiple different value types together within the
same statement, however the “+” will only allow you to join strings together.
Therefore, if you wanted to join a £ to a number without a space you would need to
specify that number is a string.
Here str() is used around the value to cast it as a string.
str() Converts a value into a string
int() Converts a value into an integer
float() Converts a value into a floating-point number
03 – Data Types Page 2
© Nichola Wilkin Ltd 2020
www.nicholawilkin.com