Data types
Category type | Python data type | Example |
---|---|---|
Text/string | str | "hello" , 'world' |
Numeric (integer, decimal) | int , float | 1 , 1.3455 |
Sequence/array (a store of multiple values in an organised way) | list , tuple , | [1,2,3,4] , (1,2,3,4) |
Map (a store of multiple values in an unordered way) | dict | {"greeting": "hello", "place": "world"} |
Boolean | bool | True , False |
>>>message = "Hello World"
>>>message[4]
'o'
because that character is in position 4 in the string message
; the first character in a string is always position number 0.You can use the type()
function to find out what data type something is. Enter the code below in your REPL, and press Enter:>>>type(message)
message
is a string. 'Hello'
and "I am learning Python"
are also strings. (Single quotation marks '
and double quotation marks "
are the same when working with strings in Python.)Python recognises various data types and will only allow certain operations on certain types of data.- Enter the example below in your REPL by typing the print lines, followed by Enter:
>>> 1 + 2
3
>>> '1' + '2'
12
>>> [1,2] + [3,4]
[1, 2, 3, 4]
1 + 2
, the numbers were integers, so Python added them together and output 3
.In
'1' + '2'
, the data was two strings, so Python concatenated these strings.In
[1,2] + [3,4]
, the data was two lists, so Python combined the lists.You can change data types in Python using type casting. The most common examples of type casting are changing numbers to strings and strings to numbers.Here a variable is being declared with a value of '3'
instead of 3
:>>> my_num = '3'
>>> type(my_num)
<class 'str'>
>>> my_num = '3'
>>> my_num + 4
Traceback (most recent call last):
File "<pyshell#14", line 1, in <module>
my_num + 4
TypeError: must be str, not int
my_num
in calculations, it must first be type cast to an integer using the int()
function:>>> my_num = '3'
>>> int(my_num) + 4
7
int
of my_num
:>>> my_int_num = int(my_num)
>>> type(my_int_num)
<class 'int'>
my_int_num
with a string, it needs to be type cast to a string using the str()
function:>>> print("My new number is " + str(my_new_num))
My new number is 3
Challenge
You learnt about getting user input in the silly story exercise from last week. For example:num = input('Give me a number ')
- Asks the user for the real age of a dog
- Converts this to an integer
- Calculates the dog’s age in ‘dog years’ by multiplying its age by seven
- Turns this result back into a string
- Outputs the result so the user can see how old the dog is in dog years

Scratch to Python: Moving from Block- to Text-based Programming

Our purpose is to transform access to education.
We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.
We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.
Learn more about how FutureLearn is transforming access to education