All jobs / Backend developer / Backend Languages
Backend developer interview question · 0–1 years
What is the difference between == and is?
The strongest answer
== asks whether two values are equal; is asks whether they are the very same object — use is only for None, True, False and sentinels
Why it works
Exactly. Identity versus equality, and is belongs to singletons.
What a good answer covers
- == compares values, is compares identity.
- Use is None, not == None.
- Equal objects need not be the same object.
- Caching makes is look right by accident.
Answers that fall short
- Close, but not the strongest: They are the same; is just reads better
They differ, and small-integer caching makes the difference hide in a REPL until it bites. - Weak: is is the strict version of ==, like === in JavaScript
Not strictness — identity. Two equal strings can be different objects. - Red flag: == is for numbers and is is for strings
Type has nothing to do with which one you use.
Who gets asked this
- Backend developer
- Full-stack developer
- Automation developer
- DevOps / SRE / cloud
- Data analyst / BI
- Data engineer
- Data science / ML / AI
- AI engineer
More Backend Languages questions
- When do you use an array rather than a List<T>?
- You want a value from a dict, or a default if the key is missing, without raising. What do you use?
- You remove items from a List<T> inside a foreach over it and get an exception. Why?
- if items: — what exactly is being tested?
- When is var a good idea?
- Building a long string in a loop with s += piece is slow on big inputs. Why?
- Why expose a property rather than a public field?
- What does xs[1:4] give you, and what does xs[:] do?