comparison operations in python

there are 8 types comparison operations in python

x < y
x <= y
x > y
x >= y
x == y
x != y
x is y
x is not y

< operation will return true if y is strictly greater than x. otherwise false.
<= operation will return true if y is greater than x or equal to x. otherwise false.
> operation will return true if y is strictly less than x. otherwise false.
>= operation will return true if y is less than x or equal to x. otherwise false.
and when you try to use above 4 operations, x and y have to be same data type. otherwise, TypeError will be raised.

== operation will return true if x and y are same that means the value of them will be evaluated not object itself.
!= operation will return true if x and y are not same. otherwise, false.

is operation will return true if x and y are same object. otherwise, false.
is not operation will return true if x and y are not same object. otherwise false.

I will show you example code to help you understand easier.



x = 1
y = 2

print("x < y: {}".format(x < y))
print("x <= y: {}".format(x <= y))
print("x > y: {}".format(x > y))
print("x <= y: {}".format(x <= y))

print("x == y: {}".format(x == y))
print("x != y: {}".format(x != y))

print("x is y: {}".format(x is y))
print("x is not y: {}".format(x is not y))
x = y
print("x is y: {}".format(x is y))






Comments

Popular posts from this blog

making menubar and menu on QML

QSizePolicy Fixed example in pyside2

let's start QML programming with PySide2