Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

== and is in python [duplicate]

Tags:

python

Its been a couple of days since I started learning python, at which point I stumbled across the == and is. Coming from a java background I assumed == does a comparison by object id and is by value, however doing

 >>> a = (1,2)
 >>> b = (1,2)
 >>> a is b
 False
 >>> a == b
 True

Seems like is is equivalent of java's == and python's == is equivalent to java's equals(). Is this the right way to think about the difference between is and ==? Or is there a caveat?

like image 744
fo_x86 Avatar asked Jan 03 '13 18:01

fo_x86


People also ask

Is duplicate in Python?

duplicated() function indicate duplicate Series values. The duplicated values are indicated as True values in the resulting Series. Either all duplicates, all except the first or all except the last occurrence of duplicates can be indicated. Example #1: Use Series.

How do you use duplicates in Python?

Pandas DataFrame duplicated() MethodThe duplicated() method returns a Series with True and False values that describe which rows in the DataFrame are duplicated and not. Use the subset parameter to specify if any columns should not be considered when looking for duplicates.

How do you check if a column contains duplicates in Python?

To find duplicates on a specific column, we can simply call duplicated() method on the column. The result is a boolean Series with the value True denoting duplicate. In other words, the value True means the entry is identical to a previous one.


1 Answers

  • '==' checks for equality,
  • 'is' checks for identity

See also

Why does comparing strings in Python using either '==' or 'is' sometimes produce a different result?

like image 50
Andreas Jung Avatar answered Oct 31 '22 15:10

Andreas Jung