Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

The is operator does not match the values of the variables, but the instances themselves.

What does it really mean?

I declared two variables named x and y assigning the same values in both variables, but it returns false when I use the is operator.

I need a clarification. Here is my code.

x = [1, 2, 3] 

y = [1, 2, 3] 

print x is y #It prints false!

1 Answer

0 votes
by (106k points)

In Python, the operator is only returns true if they're actually the same object. If they were the same, a change to one would also show up in the other.

See the example below:-

>>> x = [1, 2, 3] 

>>> y = [1, 2, 3] 

>>> print x is y 

False 

>>> z = y 

>>> print y is z 

True 

>>> print x is z 

False 

>>> y[0] = 5 

>>> print z 

[5, 2, 3]

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Feb 14, 2021 in Python by laddulakshana (16.4k points)
0 votes
1 answer
asked Oct 5, 2019 in SQL by Tech4ever (20.3k points)
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...