Types#
We have already seen that there are various types of data availabe in Python. Thus far, we have categorised our data in a very simple manner: numbers and letters. In reality, the situation is slightly more nuanced than this and it is worth being aware of all of the basic data types that you can play with.
Numeric types#
Python provides an built-in function for determining the type
of some data. For example:
type(2)
int
Here, we call the type
function on the number \(3\), and Python informs us that this data is of type int
: it is an integer. What if we try a number which is not an integer?
type(3.5)
float
As referenced earlier, numbers with a decimal point are floats, Python tells us as much in the code above. There is also a third basic numeric type:
type(3 + 2j)
complex
The complex data type represents complex numbers. The j
in the code above represents the imaginary component which is usually written with an “i” on pen and paper: \(3 + 2i\). For the purposes of this course, it is very unlikely that you will need to manipulate complex data, but it is worth noting that Python does have this capability.
It has not been explicitly highlighted before, but Python will automatically change an int
to a float
if necessary:
an_integer = 5
a_float = 2.2
sum_of_two_numbers = an_integer + a_float
print(sum_of_two_numbers)
type(sum_of_two_numbers)
7.2
float
In this example, we are adding a float
to an int
which means that the result must also be a float
.
Strings#
Strings are clearly very different from the numeric data discussed above; this is reflected by the type
function:
type("Look at me, I'm a string!")
str
Hopefully this further clarifies some of the examples you were asked to work through in the Strings exercise:
an_integer = 7
a_string = 'Strings are not numbers!'
an_integer + a_string
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[6], line 4
1 an_integer = 7
2 a_string = 'Strings are not numbers!'
----> 4 an_integer + a_string
TypeError: unsupported operand type(s) for +: 'int' and 'str'
Error
This code yields a TypeError, which means that we are trying to operate on different data types in a way that doesn’t make any sense. Here we are trying to “add” an int
(integer) to a str
(string): it is unclear how we might do this.
Of course, there are situations in which we might want to add an integer to a string. Consider the following:
'This is an integer: ' + 10
This code will raise an error much like the previous example, but as human beings we can see how it might make some sense. To add the int
to the str
, we have to cast it to a str
:
an_integer = 10
a_string = 'This is an integer: '
a_string + str(an_integer)
'This is an integer: 10'
Here we have used the in-built str
function to cast the int
to a str
. In other words, we have converted the type of an_integer
and turned it into a string. This process is, in a simplified manner, what happens automatically when we use f-strings with numbers: the relevant variable is formatted into a string:
an_integer = 10
f'This is an integer: {an_integer}'
'This is an integer: 10'
Booleans#
The final basic data type we have yet to cover is the Boolean. This type of data represents two possible logical values: True or False:
type(True)
bool
type(False)
bool
We have yet to see how this type of data can be useful, but we will come to this in due course. True
and False
are the first reserved keywords that we have come across: we cannot create variables with these names.
True = 5
Cell In[16], line 1
True = 5
^
SyntaxError: cannot assign to True
False = 'this will raise a SyntaxError!'
Cell In[17], line 1
False = 'this will raise a SyntaxError!'
^
SyntaxError: cannot assign to False