Python / Core Python Fundamentals Interview Questions
What is None in Python, and when should you use 'is' versus '=='?
None is Python's null value — a singleton object of type NoneType. It represents the absence of a value: default function returns, uninitialised optional variables, missing dict values. There is exactly one None object in any Python process.
The key distinction for comparisons:
==tests equality: do the two objects have the same value? It calls__eq__and can be overridden.istests identity: are the two names pointing to the exact same object in memory (sameid())?
# Correct: test for None with 'is'
result = some_function()
if result is None:
print('No result returned')
# Why not ==?
class Weird:
def __eq__(self, other):
return True # lies — claims equality with everything
w = Weird()
print(w == None) # True — because __eq__ lies
print(w is None) # False — identity check cannot be fakedPEP 8 says explicitly: use is and is not when comparing against None or the boolean singletons True/False. The is check is also marginally faster because it does not invoke any dunder method — it is a direct pointer comparison.
A related trap: CPython caches small integers (typically -5 to 256) and short strings, so a = 256; b = 256; a is b is True. But a = 1000; b = 1000; a is b may be False. Never rely on is for value comparisons — only use it for singletons like None.
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
