Reputation: 2019
How do I achieve the effect of the ===
operator in Python?
For example, I don't want False == 0
to be True
.
Upvotes: 16
Views: 9898
Reputation: 1
'===' operator will compare both value and type of variable.
You can try this in Python:
x == y and type(x) == type(y)
to achieve similar effect.
Upvotes: 0
Reputation: 36
Answering in context of JS and Python(3.11.4):
In JS:
'=' is a simple assignment operator, can be used to assign values to variables outside a block(expression like an if else statement) and also within an expression.
let a = 3;
let b = 5;
if (a = b){
console.log(a, b);}
The above code will work fine and value of both the variables will be assigned as 5
'==' This operator simply checks operand value and return Boolean result. NOTE: It converts the operand to the same type by default and then compares them. e.g. when comparing '3' and 3, it will convert both operand to same type(number or string), then compare the value, The result for 3 == '3' will be TRUE.
'===' This operator checks the value and also the type of the operand. and returns Boolean result. So, for comparison of '3' === 3 , the result will be FALSE
Now consider Python:
'=' This operator is simply used to assign standalone values to variables. Not within an expression (unlike JS)
a = 3
b = 2
if a = b:
print('True')
This code will not work and throw 'SyntaxError: invalid syntax'
ANSWER YOU ASKED ===>
NOTE : Above said, I would also like to mention that, due to difference in Data Types in Python and JS or any other programming languages, the operators tend to differ from one another.
For example,
In Python, Integers and Floats(Decimal Numbers) are two seperate Data Types and in JS they are simply termed as Numbers. Hence the functioning of the operators will vary.
`
Upvotes: 0
Reputation: 1977
There is a lot of misinformation in the answers and comments. is
, ==
and ===
(the strict equality operator) are all different. In Python, as in many other programming languages, checking for strict equality is a frequent and essential practice.
is
operatoris
is for checking object identity.
>>> a = 3.4
>>> b = 3.4
>>> a is b
False
==
operator==
is for checking the values while enabling type coercion.
>>> 3.0 == 3
True
Here it performed an implicit type conversion.
===
operatorThe strict equality operator (implemented as
===
in some mainstream languages), would return false in the above example. It checks for both the types and the values.
As of today, Python does not implement a single operator to do the string equality comparison.
One common way of implementing it is in Jeremy's answer. This solution works fine if the types and values both match. It does not cover the derived types however.
x == y and type(x) == type(y)
If you want to extend it to the derived types, you should do the following.
x == y and (isinstance(x, type(y)) or isinstance(y, type(x)))
Upvotes: 0
Reputation: 69021
Going with the Mathematica definition, here's a small function to do the job. Season delta
to taste:
def SameQ(pram1, pram2, delta=0.0000001):
if type(pram1) == type(pram2):
if pram1 == pram2:
return True
try:
if abs(pram1 - pram2) <= delta:
return True
except Exception:
pass
return False
Upvotes: 2
Reputation: 129735
If you want to check that the value and type are the same use:
x == y and type(x) == type(y)
In Python, explicit type comparisons like this are usually avoided, but because booleans are a subclass of integers it's the only choice here.
x is y
compares identity—whether two names refer to the same object in memory. The Python boolean values are singletons so this will work when comparing them, but won't work for most types.
Upvotes: 42
Reputation: 46607
You can use the is
operator to check for object identity. False is 0
will return False
then.
Upvotes: 1