0:01
Hey friends, welcome back to this
0:02
channel. This Raja Lakmi Singum. If
0:05
you're learning Python step by step,
0:07
today's topic is super important but
0:09
often misunderstood by beginners, which
0:12
is identity operators. Now, don't worry.
0:15
I'll break it down in the simplest way
0:17
possible. By the end of this video,
0:19
you'll know exactly how they work. So,
0:26
First, let's clear this up. In Python,
0:30
we have two different types of
0:32
comparisons. One is value comparison
0:36
where we check if two things have the
0:38
same content or not. And we have
0:42
identity comparison where we check if
0:45
two things are literally the same object
0:51
And for identity comparison, Python
0:54
gives us two special operators
1:00
and is not. Let's look at an example.
1:25
okay and we have a value c which where
1:28
we are assigning a into that that means
1:35
now we'll give this identity operator
1:38
which is nothing but is like if a is b
2:32
Okay. So what we are doing here A is B.
2:37
We'll check if A and B have the same
2:39
identity in the memory or not.
2:42
Okay. And we also have one more
2:44
condition which is if a is c
2:58
have the same identity.
3:04
but we are just checking it with A and C
3:19
and so after looking this code based on
3:22
the knowledge you have based on the
3:24
basics that we have learned what you'll
3:26
be thinking will be the output of this
3:28
particular thing I want you to pause at
3:32
Take a moment, look at the code, observe
3:34
everything and see and just comment down
3:38
below. I'm going to see it
3:40
and let's see how much you understood.
3:44
So what I'm guessing is most of them
3:46
will be thinking that A and B are same.
3:49
So it'll be printing A and B have the
3:51
same identity and also C equal to A.
3:54
That means A and C also have the same
3:57
identity. So you'll get the output as a
4:00
and b have the same identity and a and c
4:03
have the same identity. So I think you
4:06
guys are guessing this. So people who
4:09
actually know the concept of identity
4:11
operators will not say that as the right
4:14
answer. Let's check what will be the
4:17
actual output of this.
4:22
So the output we got is A and B do not
4:25
have the same identity. But A and C have
4:30
the same identity. What's going on here?
4:33
A and B have the same values. So A equal
4:39
But C is also equal to A basically.
4:43
But what's going on here?
4:48
both look the same because they have the
4:50
same values 1 2 and 3. A A have 1 2 3 B
4:54
have 1 2 3. A is a list and B is also a
4:57
list. But what's making Python think
5:02
So here is the twist. Python actually
5:06
stored them in different memory
5:08
locations. So A have some memory, B have
5:11
some memory. So when we check A is B,
5:15
Python says no. they don't have the same
5:19
identity. That identity is nothing but
5:21
the memory location number. So they
5:24
don't have same memory location. So
5:27
that's why A is not B. So that is what
5:31
it is conveying to us. Most of them will
5:33
misunderstood because of the reason that
5:37
they don't think about the memory
5:41
Think like a computer. So when you're
5:44
thinking like a computer so computer
5:46
will have multiple memory locations. So
5:49
whenever you create a variable and
5:52
whenever you give it some value you are
5:55
what you are doing you are telling
5:57
computer to create a memory called a and
6:01
store the value 1 2 3 inside that
6:04
memory. So 1 2 3 is stored somewhere.
6:08
You want computer to get the value 1 2
6:10
3. So how computer will get it to you?
6:14
By calling this value which is nothing
6:16
but the variable name. It is nothing but
6:19
a memory location where you're naming it
6:22
as a and the value is 1 2 3. But
6:28
obviously it will store the values in
6:31
ones and zeros. So it will give it some
6:34
memory number. It could be anything.
6:36
Computer will always find a space to
6:38
store the values. Okay.
6:41
So, computer stored somewhere in 1.1 and
6:45
the computer also stored 1 2.
6:50
So, computer also stored the value of B
6:53
one in 10 or two location like for
6:55
example because we don't know the exact
6:57
memory. So, where it is storing, how it
7:00
is storing, why it is storing for that
7:02
you have to learn operating systems and
7:04
that's a complete different story.
7:07
I'm just telling you to understand this
7:09
concept of identity. All you have to
7:12
understand is A is stored in a separate
7:15
memory location and B is stored in a
7:18
separate memory location. But when you
7:20
assign C equal to A that means when you
7:24
assign the value of A to C that means
7:27
you are also giving the memory location
7:34
So identically both are same. So that is
7:37
why it is giving this output. A and C
7:40
have the same identity because they have
7:42
the same memory numbers. But A and B do
7:46
not have the same identity because they
7:48
are treated as different variables and
7:50
they are stored in a different places.
7:53
That is why. So we created two list A
7:57
and B. Both look the same because they
7:59
have 1 2 3 but Python actually stored
8:04
them in different memory locations. So
8:06
when we check A is B, Python says no
8:10
they don't have the same identity.
8:12
Identity is nothing but the memory
8:16
But look at C equal to A. This one. So
8:20
here C is literally pointing to the same
8:23
object as A. So when we check A is C,
8:28
Python says yes they are the same object
8:35
So that is the difference.
8:38
Okay, I hope I got you. I helped you in
8:41
this concept. Please comment down below
8:44
if you want an explanation on pen and
8:46
paper. I'm going to do it for sure. Now
8:48
let's talk about is not operator.
8:53
For example, we have two variables x=
9:30
obviously the opposite of this
9:36
So X and Y have the same identity.
9:42
Now let's check is not operator. So here
9:46
we gave X value 10 and Y value 20.
9:50
Clearly these are two different objects
9:52
in memory. So X is not Y. So, Python
9:58
obviously say true, they are not the
10:01
same thing. It's as simple as that. So,
10:04
X and Y do not have the same identity.
10:08
So, here is the golden rule. Always use
10:12
double equal to when you want to compare
10:14
values like for example
10:16
x= to y. And also use is and is not when
10:22
you want to compare identities in
10:24
memory. So when you want to compare
10:26
values, use this operator. When you want
10:29
to compare the identities, use identity
10:33
operator to compare identities in memory
10:36
when you're dealing with the objects.
10:39
All right, friends. That's it for
10:41
today's lesson on identity operators in
10:43
Python. If this video helped you finally
10:46
understand the difference, do me a
10:49
little favor. Imagine you're writing
10:51
Python code and your subscribe button is
10:53
just like is operator. It needs to be
10:56
the same identity every time you come
10:58
back. So go ahead and click that
11:01
subscribe button right now. And hey, if
11:03
you enjoyed this, drop a like and share
11:05
it with the beginners. You know, I'll
11:08
see you in the next video with more
11:10
Python basics explained the easy way as
11:13
much as I can. Till then, keep
11:15
practicing and keep coding.