Skip to main content

3.5) Numbers: Integers And Floats


There are several data types you can play with in Python. The main numerical types are integers and floats.

To follow on from earlier, you can see what type the variable 'pi' is, using the function 'type()':

>>> pi=3.14
>>> type(pi)
<type 'float'>

So our variable pi is a floating number. Now try something else:

>>> n=3
>>> type(n)
<type 'int'>

The only difference between an integer and a float is that floats have a decimal point.

>>> type(3)
<type 'int'>
>>> type(3.0)
<type 'float'>

For the curious, the reason for differentiating floats and integers is that they are stored differently in the memory of computers. Integers are particularly useful for counting the number of iterations in a loop or lines in a text file, as we will see later on in this course.