Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

double equals vs is in python [duplicate]

I run the following in the Python interpreter:

>>> foo = 10 >>> dir(foo) == dir(10) True >>> dir(foo) is dir(10) False >>>  

Why is this?

like image 559
ben Avatar asked Feb 21 '13 17:02

ben


People also ask

Is vs double equal Python?

Difference between == and = in Python. In Python and many other programming languages, a single equal mark is used to assign a value to a variable, whereas two consecutive equal marks is used to check whether 2 expressions give the same value .

Is is the same as == in Python?

== is for value equality. It's used to know if two objects have the same value. is is for reference equality. It's used to know if two references refer (or point) to the same object, i.e if they're identical.

What is double == in Python?

Although similar to one another, the double equals ( == ) and the is keyword are used for different comparison purposes and yield different results. The main difference between the two is that the is keyword checks for reference equality while the double equals ( == ) operator checks for value equality.

Why is == used instead of?

== is always for testing equality. in most cases used as a drop-in replacement for <- , the assignment operator. used as the separator for key-value pairs used to assign values to arguments in function calls.

What is the difference between equal and identical in Python?

There’s a difference in meaning between equal and identical. And this difference is important when you want to understand how Python’s is and == comparison operators behave. The == operator compares by checking for equality: If these cats were Python objects and we’d compare them with the == operator, we’d get “both cats are equal” as an answer.

How do you use the double equal sign in Python?

The double equal sign in python is a type of comparison operator. It is used for comparing whether or not the two data are equal. So this is how you use the double equal sign in python. For tutorials on different types of operators do watch my video. I hope this helps… What does the sign T [1:] in Python mean?

What is the difference between == and is in Python?

The “==” operator compares the value or equality of two objects, whereas the Python “is” operator checks whether two variables point to the same object in memory. In the above example, we should have got an output True, but instead, the output we received was False.

What is the difference between ==== and equals?

== is a reference comparison/address comparison, i.e. both objects point to the same memory location .equals () evaluates to the comparison of values in the objects s1 and s2 both ponts to same reference so answer is true. What is the difference between Double and double in C#?


1 Answers

is checks that 2 arguments refer to the same object, == checks that 2 arguments have the same value. dir() returns a list which contains the same data for both foo and 10, but the actual list instances for the 2 things are different.

like image 115
Silas Ray Avatar answered Sep 24 '22 19:09

Silas Ray