Why false output? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
18th Jun 2017, 7:56 AM
looper
looper - avatar
3 Answers
0
because f is not s.. if you write print(f == s), the result is true..
18th Jun 2017, 8:16 AM
Mugfirfauzy Siregar
Mugfirfauzy Siregar - avatar
+ 1
This has less to do with the variables, and more to do with the comparison you're using. While 'is' and '==' appear to be the same, they're not. 'is' compares immutable identities (objects) in the system memory. '==' compares values. 'is' will look in the system memory and compare one immutable object to another. It can compare small mutable values and still return True, but don't rely on it. If you were for example: a = 36834639891 b = 36834639890 +1 print(a is b) print(a == b) # FALSE # True The above doesn't work because b changed and became mutable. But if you for example compared: a = 36834639891 b = 36834639891 print(a is b) print(a == b) # True # True The above will output True because both objects are immutable and the same. Then you'll probably ask me, (why then can I do "aa" is "a" + "a"? How come that works then Sapphire?) Well, there's an easy answer to that. Strings are immutable all the time. When you add one string into another, it doesn't actually add anything to the original, what ends up happening is python creates a new string, with the original, plus the new string. So it remains immutable, and thus, 'is' still returns True. So, going back to the code. Both lists aren't the same, because they're both mutable, and not the same objects in the system memory. Beyond this explanation, is out of my area.
18th Jun 2017, 8:32 AM
Sapphire
0
Because s and f are cointainers (list, dictionary, set, tuple) and one cointainer is never totaly the same as another one... but it can be equal(hope you understand)
18th Jun 2017, 8:31 AM
Tim Thuma
Tim Thuma - avatar