Can someone explain this to me please? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Can someone explain this to me please?

a = b = [1] c, d = [1], [1] print(a is b) print(c is d) Output: True False Thank you!

16th Nov 2019, 12:43 PM
Daniel Tande
Daniel Tande - avatar
5 Answers
+ 6
a and b are pointing to one value, c and d have each their own value. is checks, whether both variables reffer to one value, or not. Try using id() to understand: a = b = [1] print(id(a)) print(id(b)) # Prints the same id. print(a == b) # Returns true, bcs values are same
16th Nov 2019, 12:50 PM
Asman-H
Asman-H - avatar
+ 4
Here 'is' is telling if both a, b are pointing to same list.... In first case a, b is pointing to Same list but c, d are 2 different lists with same value
16th Nov 2019, 12:48 PM
Saurabh B
Saurabh B - avatar
+ 3
Thanks for help! It was really useful!
16th Nov 2019, 12:55 PM
Daniel Tande
Daniel Tande - avatar
16th Nov 2019, 4:07 PM
✳AsterisK✳
✳AsterisK✳ - avatar
0
https://www.sololearn.com/Discuss/2072312/?ref=app
17th Nov 2019, 5:28 PM
Abdul Wahab Chattha
Abdul Wahab Chattha - avatar