Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Basics (2015) Logic in Python Weighing Your Options

Arikaturika Tumojenko
Arikaturika Tumojenko
8,897 Points

Why is the "is" operator working with variables comprised of small numbers but not with lists?

Thing to understand the first example Kenneth used: a = 5 b = 5 a is b True.

I read that "is" tests if two variables point the same object, not if two variables have the same value. In this case, how come variables a and b are the same object?

1 Answer

Tom Slutsky
Tom Slutsky
12,453 Points

In Python everything is an object, in particular ints are objects as well. Therefor when assigning an int (for example 5) to a variable (a) we actually pointing a to the Int objects that containing the value 5. When we assign 5 to another variable (b); we pointing it also to the same Int object holding the value 5. Therefor both a and b pointing to the same object and a is b = true