Looking at the following IPython (Python 3.7) session:
In [1]: id(\'hello\')
Out[1]: 140300950123104
In
Two objects with non-overlapping lifetimes may have the same id() value.
This is an interesting question, but it's well explained in wtfpython.
When a and b are set to "hello" in the same line, the Python interpreter creates a new object, then references the second variable at the same time. If you do it on separate lines, it doesn't "know" that there's already
hello
as an object(because "hello" is not implicitly interned as per the facts mentioned above). It's a compiler optimization and specifically applies to the interactive environment.
One thing that differs is that in IPython, things might be a bit different from direct Python REPL, so this explains the difference in id
in your first two inputs.