I know that other languages like Javascript
have
a == and ===
operator also they have a != and !==
operator does Python also has a === and !==
(ie. a single operator that checks the type and compares the value at the same time like === operator) and if not how can we implement it.

- 11
- 5
-
1Does this answer your question? [How to compare type of an object in Python?](https://stackoverflow.com/questions/707674/how-to-compare-type-of-an-object-in-python) – Guy Dec 03 '19 at 05:31
-
There is no "abstract comparison" operator in Python, i.e. Javascript's `==`. Python `==` is similar to Javascript `===` but bear in mind, that Python and Javascripts data models are different, so make sure to read the python documentation for specifics. Python is generally a *strongly* typed language, comparisons between distinct types will throw an error instead of attempting implicit type conversions like Javascript. – juanpa.arrivillaga Dec 03 '19 at 05:32
-
there is only `isinstance(variable, int)`, `isinstance(variable, str)`, etc. which you can use together with `==` – furas Dec 03 '19 at 05:36
-
Also note, in Python, there is no notion of objects and non-objects, *everything* is an object. By default, `==` will compare object *identity* in user-defined classes, unless the user defines the behavior for that type otherwise (by overriding `__eq__`). To compare any objects by identity, you can use the `is` operator. – juanpa.arrivillaga Dec 03 '19 at 05:36
-
@furas I don't *thing* this is what the op is asking. In Javascript, there are two "equality" operators, `==` and `===`. The former will try to coerce types if they are different, so, for example, `42 == '42'` will evaluate to `true` in JS. There isn't an equivalent notion in Python. – juanpa.arrivillaga Dec 03 '19 at 05:37
-
@juanpa.arrivillaga I know that this in not exactly what OP ask - so I put it as comment, not answer. Comments are for this. – furas Dec 03 '19 at 05:45
-
Please check this link, I hope this is what you're looking for https://stackoverflow.com/questions/707674/how-to-compare-type-of-an-object-in-python – Juhil Somaiya Dec 03 '19 at 05:51
5 Answers
No, and you can't really implement it yourself either.
You can check the type of an object with type
, but if you just write a function that checks type(x) is type(y) and x == y
, then you get results like [1]
and [1.0]
showing up as equivalent. While that may fulfill the requirements you stated, I've never seen a case where this wasn't an oversight in the requirements.
You can try to implement your own deep type-checking comparison, but that requires you to know how to dig into every type you might have to deal with to perform the comparison. That can be done for the built-in container types, but there's no way to make it general.
As an aside, is
looks vaguely like what you want if you don't know what is
does, but it's actually something entirely different. is
checks object identity, not type and value, leading to results like x = 1000; x + 1 is not 1001
.

- 260,549
- 28
- 431
- 505
This will answer both of your question:
a=5
b=6
#To check value
print(a==b)
#output - False
#To check type
print(type(a)==type(b))
#output - True

- 490
- 7
- 18
-
-
You can use `and` operator then. e.g `if a==b and type(a) == type(b):` – Sapan Zaveri Dec 03 '19 at 06:05
-
@Neeraj, If you got your solution then you may tick it. Or I am here to help you. Thanks!! :) – Sapan Zaveri Dec 03 '19 at 06:59
-
This works I've tried it already, just wanted to ask is there any single operator to do so like === in Python. By the way thanks for replying. – Neeraj Dec 03 '19 at 07:08
-
@Neeraj single operator will be used to assign reference in python, while there is no `===` operator – Sapan Zaveri Dec 03 '19 at 07:20
To check type
type(VariableName)
For comparison it is obvious we use == , If you are going for some specific comparison of any specific data type then answer can vary

- 600
- 2
- 11
- 32
For just checking type of variable we use type
x = 10
type(x) # int
If you want to check whether a variable is of particular type we use isinstance
x = 2.0
isinstance(x, int) # False
isinstance
can also be used to check for group of types
x = 10
isinstance(x, (int, float)) #True
For checking equality and type you can use ==
operator and any of the two type()
or isinstance()
in combination to get your desired result.

- 589
- 3
- 12
You can use the 'is' operator. For example:
a = 1
b = True
print(a == b)
print(a is b)
OUTPUT:
> True
> False

- 5
- 1
-
2No, that's not what `is` does. `is` checks object identity, not type and value. – user2357112 Dec 03 '19 at 05:52
-
For example, `'abc' is 'abcd'[:3]` [evaluates to `False`](https://ideone.com/XBZ1my). – user2357112 Dec 03 '19 at 05:54