BugHunt

'is' returns False for equal strings

`is` checks whether two names point to the same object; `==` checks whether the values are equal. Use == for strings and numbers.

PythonLogic errors

What it means

Two strings with identical letters can be two different objects, for example one built by .lower() and one written as a literal. `is` then returns False even though == is True. It seems to work in quick tests because Python reuses some small strings and integers, which makes the bug intermittent. Python 3.8+ warns: SyntaxWarning: "is" with a literal.

Common causes

1. Comparing strings with is

lower() returns a new string object.

Breaks

if role.lower() is "admin":

Works

if role.lower() == "admin":

2. Comparing numbers with is

Small ints are cached; large ones are not.

Breaks

if count is 1000:

Works

if count == 1000:

How to find it in your own code

Use == for every value comparison. Keep is for identity checks with singletons: x is None, x is True, x is False.

Still not sure why yours breaks?

Paste it into the visualizer and watch it run line by line, with every variable at every step. Free, and it runs in your browser.

Other common errors