Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

The is Operator

jamesbrown68 on March 31, 2020

About the interned numbers (am I spelling that right?) I noticed that the id’s for ‘x = 20’ was the same, even after I exited Python and started a new REPL. I was expecting the assignments to occur when the REPL started, but I suppose not. So what’s determining the id’s for -5 to 256? My OS?

Liam Pulsifer RP Team on April 3, 2020

@jamesbrown68 hmm, that’s really interesting! I don’t get that behavior with my REPL, and interning is generally only preserved over each invocation of Python, so I’m not sure what’s causing that to happen. Out of curiosity, what OS and REPL are you running?

jamesbrown68 on April 4, 2020

I’m on Windows 10, Python 3.8.1.

python
>>> x = 20
>>> id(x)
1617684704
exit()
python
>>> x = 20
>>> id(x)
1617684704

Liam Pulsifer RP Team on April 9, 2020

Wow @jamesbrown68, I have to admit this one stumps me. I wonder if there’s some system setting in your OS that’s causing Python to run in exactly the same way every time?

Vedang Joshi on April 10, 2020

Wondering why I’m getting this output?

[root@vedang]# python
Python 2.7.5 (default, Aug  7 2019, 00:51:29)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a = "hello"
>>> b = "hello"
>>> a is b
True
>>> a == b
True
>>> id(a)
140575040349312
>>> id(b)
140575040349312
>>>

Vedang Joshi on April 10, 2020

the interning feature seems random?

Dan Bader RP Team on April 10, 2020

“Interning is an implementation-dependent optimization that depends on many factors. It can be interesting to understand how it works, but never depend on it working any particular way.” (Source)

lordcommandermay on April 30, 2020

i have not been able to recreate the above example.

a='bro!'
b='bro!'
print(a is b)

a='bro '
b='bro '

print(a is b)
True
True

[Program finshed]

lordcommandermay on April 30, 2020

nevermind

a='bro'
b='bro'
print(a is b)

a=a+'!'
b=b+'!'

print(a is b)
True
False

[Program finished]

lordcommandermay on April 30, 2020

nevermind

a='bro'
b='bro'
print(a is b)

a=a+'!'
b=b+'!'

print(a is b)
True
False

[Program finished]

lordcommandermay on April 30, 2020

is this use case safe?

if a is True:

Ricky White RP Team on May 1, 2020

Hi @lordcommandermay,

Whether it’s safe, depends on what you’re doing within in if statement. But checking if an object is True is common in Python. However, if you are checking to see if the presence of the object a, then you can shorten it to just: if a:. If a is a boolean, and you want to check it’s truthiness, then you should use == instead of is. Hope that helps.

Become a Member to join the conversation.