Python supports several different types of data values. These values can be stored and re-used in variables.
Type | Python | Purpose | Examples |
---|---|---|---|
Integer | int | store whole numbers | no_of_pupils hours height_centimetres fluffy_bunny_count cost_in_pence |
Real | float | store decimal numbers | pi cost_in_pounds height_metres |
Text | str | store strings of characters, aka text | name car_registration telephone_number |
The computer needs away of storing information (integers, reals, strings) so that a program can process this information. Often the data are held long-term on databases, but for a programs need to use variables to keep the information available whilst the program is running or executing.
x = 42
y = 42
You are now going to store some values into some variables and we'll make sure you get the correct data types.
In the following code, store the correct type of information and you will get everything marked correct. Don't worry about the if or type parts of the program just now.
In [19]:
name =
number_of_pens =
length_in_metres =
if type(name)==type(''): print('You got the correct string type')
if type(number_of_pens)==type(1): print('You got the correct int type')
if type(length_in_metres)==type(1.0): print('You got the correct float type')
If you've done it right, your output should look like this:
You got the correct string type
You got the correct int type
You got the correct float type
Extra: You might like to know that you can also do type comparison like
type(name)==type(str()) #str() is an empty string ''
type(number_of_pens)==type(int()) #int() is the integer 0
type(length_in_metres)==type(float()) #float() is the real number 0.0
You can now complete programming tasks 5-12.
Python has several super-cool data types and data structures like lists, dictionaries, tuples and sets.
You can get more information on Python 3 data-types from these links:
http://www.python-course.eu/python3_variables.php (easy)
https://en.wikibooks.org/wiki/Python_Programming/Data_Types (medium)
https://docs.python.org/3/library/datatypes.html (gratuitous)