From 7f00161e60677b5301175be409c6b3ca33946fd5 Mon Sep 17 00:00:00 2001 From: chris13888 <43319231+chris13888@users.noreply.github.com> Date: Sat, 1 Dec 2018 18:55:20 -0800 Subject: [PATCH] Update index.md (#23905) --- guide/english/python/comparisons/index.md | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/guide/english/python/comparisons/index.md b/guide/english/python/comparisons/index.md index 52353298d4a..8f8993003c6 100644 --- a/guide/english/python/comparisons/index.md +++ b/guide/english/python/comparisons/index.md @@ -34,7 +34,7 @@ We can also chain `<` and `>` operators together. For instance, `3 < 4 < 5` will In Python, there are two comparison operators which allow us to check to see if two objects are equal. The `is` operator and the `==` operator. However, there is a key difference between them! -The key difference between 'is' and '==' can be summed up as: +The key difference between `is` and `==` can be summed up as: * `is` is used to compare identity * `==` is used to compare equality @@ -51,7 +51,7 @@ Next, create a copy of that list. myListB = myListA ``` -If we use the '==' operator or the 'is' operator, both will result in a True output. +If we use the `==` operator or the `is` operator, both will result in a True output. ```python >>> myListA == myListB # both lists contains similar elements @@ -84,3 +84,17 @@ False # both lists have different reference To sum up: * An `is` expression outputs `True` if both variables are pointing to the same reference * An `==` expression outputs `True` if both variables contain the same data + +However, interestingly, there are a few special cases in Python with `is`. +```python +>>> a = 5 +>>> b = 5 +>>> a is b +True +>>> a = 1000 +>>> b = 1000 +>>> a is b +False +``` + +This is due to how Python is implemented. In Python, small integers (from -5 up to 256, a fast test shows) are cached. You shouldn't rely on this detail – larger integers do not evaluate to `true` when you use `is`. For more information on this topic, view this StackOverflow thread.