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!
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
+ 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
+ 3
Thanks for help! It was really useful!
+ 1
check this out by HonFu https://code.sololearn.com/ceg91PiC9O0H/?ref=app
0
https://www.sololearn.com/Discuss/2072312/?ref=app