Looking at Immutable Built-in Data Types: Numbers & Booleans
00:00 Let’s start exploring Python’s built-in immutable data types. In this lesson, you’ve already seen a tuple and will come across the tuple again soon. But let’s start with something even simpler.
00:12
For example, number
is equal to 314
let’s say. It’s an integer, it’s immutable. The same applies for float or even complex numbers.
00:22
So all number
data types in Python are immutable. number
is just a variable name. It’s pointing to an object, which in this case is an integer.
00:31
So we can find the value is 314
. We can see that when we show the object. We can find the id
of number
and as a reminder, we’re not finding the id
of the variable name because the variable name doesn’t have an id
, it’s the object it’s pointing to.
00:47
In this case, the integer 314
that has an identity. This is the memory location. And you can also confirm that the type of number
is int
, and these are the three characteristics we talked about in the previous lesson.
01:00
The value 314
, the identity, which is the number you can see returned by the id()
function, and the type
, which in this case is an integer.
01:10
As you’ve seen in the previous lesson, if you change that, the value has changed, but so has the id
.
01:17
So it’s a different object. number
refers to a different object in this case. So what happens if you try to say number
is equal to number + 1
?
01:28
This is a common idiom you’ll find often in programming where you need to increment a value, in this case by 1
, it could be by any other value.
01:37
First of all, the value of number
has changed. That’s good. That’s what we expected to do. The value is 8
. But what about id
?
01:45
Is this the same object or is a different one? When you say number equals number plus one, you are not changing the value of the object. You are creating a new object, in this case, the object with value 8
.
01:58
And you know it’s a new object because the identity is different. The value returned by id
is different.
02:05
The same happens if you use the +=
operator, which is similar to number = number + 1
. So in this case, number
is now 9
because we’ve added 1
again.
02:16
But if you check the identity of number
, it’s yet again a different value. So this is now yet another object, the integer with the value 9
.
02:26
And we can finish this section with another example. Let’s say number = another_number
02:33
and both of them are equal to let’s say, 200
. So both number
, another_number
are 200
but are they the same object or different objects?
02:42
Let’s find out. The identity of number
is that number which ends in 0144
and the identity of another_number
so it’s a different variable name but the question is, is it pointing to the same object or to a different object?
03:00 It’s pointing to the same object.
03:03
Let’s finish this lesson by looking at another built-in data type. Let’s create has_permission
a variable name and make it equal to the Boolean value True
. So you can check the type of has_permission
.
03:19
It’s a Boolean. Its value is True
. So you can find the identity of has_permission
. A reminder: we’re not looking for the identity of the variable name, which doesn’t have a permission.
03:30
Let me spell it properly first. We’re looking at the identity of the object that has_permission
refers to so we are looking at the identity of the Boolean value True
which happens to be that one.
03:42
And you can change what has_permission
is pointing to. We can say, well now it’s equal to False
but that’s a different object. id
of has_permission
is now a different value.
03:54
And in fact, in Python, there’s only ever one object that’s True
and one object that’s False
. Because they are immutable, you don’t need to have lots of objects that are equal to True
or lots of objects that are equal to False
.
04:08
All you need is one of each. And every time you have a variable that’s referring to True
, it’s always referring to the same object. You can do this because the objects are immutable.
04:18
So you know that that object, let’s take False
as an example, the object whose identity ends in 0704
, there’s only one object that’s False
and that’s the one.
04:30
And every time you want to refer to False
in this program, it’ll use that same object.
04:37
And in fact, Booleans are a subclass of integers. We can check isinstance
to True
04:47
is an instance of Boolean, of course it is, it’s a Boolean, but it’s also an instance of int
. So Booleans are the integers, 0
for False
and 1
for True
.
04:59 Therefore, if integers are immutable, so are Booleans.
Become a Member to join the conversation.