Currently set to Index
Currently set to Follow
Skip main navigation

What are the Data Types in Python?

You are hopefully familiar with the variable data types of ‘string’ and ‘integer’, which store text and whole numbers. As you become more experienced in Python you will encounter other data types, such as:
Every programming language has its own rules about how to handle different types of data, although Scratch does not have its own specific data types.

Python Data Types

You are hopefully familiar with the variable data types of ‘string’ and ‘integer’, which store text and whole numbers. As you become more experienced in Python you will encounter other data types, such as:
 
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
 

REPL

In this step, you are going to use the REPL (sometimes called a console and pronounced ‘repel’) in your IDE. A REPL is an interactive way to get instant feedback from entering code. The term REPL is an acronym for: read, evaluate, print and loop, which is what the REPL does when code is entered. Programmers often use the REPL to try out code before entering it into a program. There are different ways to access the REPL depending on your chosen IDE. You may have to refer to your IDE’s documentation, or perform an internet search to work out how to open it.
 
In your REPL, enter each line of code below, pressing Enter after each:
 
>>>message = "Hello World"
>>>message[4]
 
The REPL will print '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)
 
The output tells you that the variable 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]
 
In 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'>
 
This string can’t be used in calculations with integers and will return a type error:
 
>>> 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
 
To use 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
 
You could also create a new variable that is the int of my_num:
 
>>> my_int_num = int(my_num)
>>> type(my_int_num)
<class 'int'>
 
If you want to use 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_int_num))
My new number is 3
 

Challenge

 
The following Python string asks for user input:
 
num = input('Give me a number ')
 
This always saves the number that the user inputs as a string.
 
Your challenge is to write a program that:
 
 
    1. Asks the user for the real age of a dog
 
    1. Converts this to an integer
 
    1. Calculates the dog’s age in ‘dog years’ by multiplying its age by seven
 
    1. Turns this result back into a string
 
    1. Outputs the result so the user can see how old the dog is in dog years
 
 
Two dogs, an older one and a younger one.
 
There are a few ways to do this, but you might like to write a little pseudocode first to help you figure it out.
This article is from the free online

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

Created by
FutureLearn - Learning For Life

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