Basic Data Types

Data Types

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

Idea of a Variable

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.

  • You should think of variables as named shoe-boxes.
  • Storing a value in a variable is called assigning a value
  • Reading a value from a variable is called retrieval.
  • Changing the value of a variable is called re-assigning.
x = 42
y = 42

The boxes (variables) are named 'x' and 'y'. Both variables contain the value '42'

Assigning Some Data

You are now going to store some values into some variables and we'll make sure you get the correct data types.

  • In the variable name you should store a string value.
  • In the variable number_of_pens you should store an integer.
  • In the variable length_in_metres you should store a real (decimal)

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')


  File "<ipython-input-19-1635055e65cc>", line 1
    name =
           ^
SyntaxError: invalid syntax

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.

Optional: More Information

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)