Explore Courses Blog Tutorials Interview Questions
0 votes
in Python by (47.6k points)

In Python, are the following two tests for equality equivalent?

# Test one.

n = 5 

if n == 5:

  print 'Yay!'

# Test two. 

if n is 5: 

  print 'Yay!'

Does this hold true for objects where you would be comparing instances (a list say)?

Okay, so this kind of answers my question:

L = []


if L == [1]: 

print 'Yay!' 

# Holds true, but... 

if L is [1]: 

print 'Yay!' 

# Doesn't.

So == test value where are tests to see if they are the same object?

1 Answer

0 votes
by (106k points)
  • There is a simple rule that will tell you when to use == or is the keyword.

  • The ‘==’ keyword is used for checking the value equality. You can use it when you would like to know if two objects have the same value.

  • The ‘is’ keyword is used for reference equality. You can use it when you would like to know if two references refer to the same object. In other words ‘is’ keyword determines whether the two values are an exact same object and equal.

  • Here’s an example that validates the above definition:-

a = 500 

b = 500

a == b 


a is b


Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers


94k users

Browse Categories