is keyword in python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

is keyword in python

What exactly does the keyword 'is' do? Could you give me some examples with explanation? Thank you for answers.

10th Feb 2018, 8:39 AM
michal
3 Answers
+ 10
It means the same point of reference, the same memory block pointer. Two variables can be equal but not identical, occupying different memory locations. But when two variables point to the same memory location, they are identical and changing one affects the other, too. A good example is with lists: a = [1, 2, 3] b = [1, 2, 3] a is equal to b, a a==b returns True. But they are just two separate lists with equal values inside. If you change a[0] = 0, b[0] will stay equal to 1. Now: a = b a was assigned the same memory location, as b. If you change a[0] = 5, print(b[0]) will also display 5. So not only a == b, but even more: a is b
10th Feb 2018, 10:34 AM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
0
but what means identity?
10th Feb 2018, 10:20 AM
michal
0
Identity it can be said that it is the 'id' of the variable for example: a = [] b = [] print( id(a) ) # Outputs: 20134128 print( id(b) ) # Outputs: 20134168 print( a is b ) # Outputs: False, different ids print( a == b ) # Outputs: True, equal values, a = [], b =[] As Kuba says, two variables can be equal but not identical, occupying different memory locations. I hope you understand.
11th Feb 2018, 2:53 AM
ADan
ADan - avatar