0% found this document useful (0 votes)
7 views6 pages

Difference Between == and is in Python

In Python, '==' checks for value equality while 'is' checks for object identity in memory. It is recommended to use 'is' when checking for None, as it ensures the variable refers to the singleton None object. Additionally, immutable objects may be interned, affecting the behavior of 'is' and '==' with certain values.

Uploaded by

vidhikapoor1298
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views6 pages

Difference Between == and is in Python

In Python, '==' checks for value equality while 'is' checks for object identity in memory. It is recommended to use 'is' when checking for None, as it ensures the variable refers to the singleton None object. Additionally, immutable objects may be interned, affecting the behavior of 'is' and '==' with certain values.

Uploaded by

vidhikapoor1298
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Difference Between == and is in Python

In Python, == and is are both comparison operators, but they serve different purposes.

1. == (Equality Operator)

 Compares values of two objects.

 Checks whether two objects have the same content.

 Does not check if they are the same object in memory.

Example:

a = [1, 2, 3]

b = [1, 2, 3]

print(a == b) # Output: True (values are the same)

print(a is b) # Output: False (different memory loca ons)

 a == b → True, because both lists contain [1, 2, 3].

 a is b → False, because a and b are two separate objects in memory.

2. is (Iden ty Operator)

 Checks whether two objects refer to the same memory loca on.

 Used to compare object iden ty, not values.

Example:

x = [10, 20, 30]

y = x # y now refers to the same object as x

print(x == y) # Output: True (values are the same)

print(x is y) # Output: True (same memory loca on)

 x == y → True, because they contain the same values.

 x is y → True, because y is just a reference to x, meaning they are the same


object.
3. Special Case: Immutable Objects (e.g., Integers, Strings, Tuples)

Python interns small integers and short strings for memory op miza on.
This means numbers between -5 to 256 and certain strings may share the same memory
address.

Example with Integers

a = 100

b = 100

print(a == b) # Output: True

print(a is b) # Output: True (interned)

x = 1000

y = 1000

print(x == y) # Output: True

print(x is y) # Output: False (not interned)

 Small numbers (-5 to 256) are interned, so they share memory.

 Large numbers are not interned, so different variables may have different memory
loca ons.

Example with Strings

s1 = "hello"

s2 = "hello"

print(s1 is s2) # Output: True (interned)

s3 = "hello world"

s4 = "hello world"

print(s3 is s4) # Output: False (not interned)

 Short strings are cached and may share the same memory.
 Longer strings are not always interned, so they may have different memory
loca ons.

4. When to Use == vs is

Operator Used for Example Output

== Value comparison [1, 2] == [1, 2] True

is Iden ty (memory) check [1, 2] is [1, 2] False

Checking if two variables refer to the same a = b then a is


is True
object b

True if x is
is None Checking if a variable is None x is None
None

5. Key Takeaways

 Use == when comparing values.

 Use is when checking if two variables point to the same object.

 Immutable objects (small integers, short strings) may be interned, so is can return
True.

 For checking None, always use is None, not == None.

Would you like to see examples of is and == in real-world scenarios?

Understanding is None in Python

In Python, None is a special singleton object that represents the absence of a value. It is
not the same as 0, False, or an empty string ("").

The correct way to check if a variable is None is by using the iden ty operator is, not ==.

1. Why Use is None Instead of == None?

 is checks iden ty, ensuring that the variable actually refers to the singleton None
object.
 == checks value, which can lead to unexpected behavior if an object overrides ==.

Correct way:

x = None

if x is None:

print("x is None") # Output: x is None

Incorrect but works in some cases:

x = None

if x == None: # Works but not recommended

print("x is None")

 Using == might work, but it is not recommended because a class can override == in
an unexpected way.

2. When Should You Use is None?

(i) Checking if a Variable Has No Value

def check_value(value):

if value is None:

print("No value provided")

else:

print(f"Value is {value}")

check_value(None) # Output: No value provided

check_value(10) # Output: Value is 10

(ii) Default Parameters in Func ons

Using None as a default argument is a common pa ern in Python func ons.

def fetch_data(data=None):

if data is None:

data = [] # Ini alize with an empty list if no argument is provided


data.append(1)

return data

print(fetch_data()) # Output: [1]

print(fetch_data()) # Output: [1] (new list created)

 Using data = None avoids using mutable default arguments like [], which can cause
bugs.

(iii) Filtering None Values in Lists

values = [1, None, 3, None, 5]

filtered_values = [v for v in values if v is not None]

print(filtered_values) # Output: [1, 3, 5]

 is not None helps remove None values while keeping 0, False, or "".

3. is None vs. == None in Custom Classes

If a class overrides ==, then using == None might not behave as expected.

Example:

class Example:

def __eq__(self, other):

return True # Overrides `==` to always return True

obj = Example()

print(obj == None) # Output: True (unexpected behavior)

print(obj is None) # Output: False (correct)

 Here, obj == None returns True incorrectly because __eq__ is overridden.

 obj is None correctly returns False, as obj is not actually None.

4. Key Takeaways
Always use is None for checking None values.
Avoid == None because it can be overridden.
Use is not None when filtering non-None values.
Using None as a default argument is a good prac ce in func ons.

Would you like an example of a real-world scenario where using is None prevents a bug?

You might also like