Wednesday, 15 March 2017

Python Operators: Comparison(Relational) and Logical Operators

Comparison(Relational) operators:


  • Comparison operators are used to perform the comparison of the operands data. 
  • It either returns True or False according to the condition.



Example1: int values


x = 10

y = 12
print('x > y  is: ',x>y)
print('x < y  is: ',x<y)
print('x == y is: ',x==y)
print('x != y is: ',x!=y)
print('x >= y is: ',x>=y)
print('x <= y is: ',x<=y)

Output:

x > y  is:  False
x < y  is:  True
x == y is:  False
x != y is:  True
x >= y is:  False
x <= y is:  True

Example2: Float values

x=1.0
y=1.2
print('x > y  is: ',x>y)
print('x < y  is: ',x<y)
print('x == y is: ',x==y)
print('x != y is: ',x!=y)
print('x >= y is: ',x>=y)
print('x <= y is: ',x<=y)

Output:

x > y  is:  False
x < y  is:  True
x == y is:  False
x != y is:  True
x >= y is:  False
x <= y is:  True

Example3: Bool values

x=True
y=False
print('x > y  is: ',x>y)
print('x < y  is: ',x<y)
print('x == y is: ',x==y)
print('x != y is: ',x!=y)
print('x >= y is: ',x>=y)
print('x <= y is: ',x<=y)

Output:

x > y  is:  True
x < y  is:  False
x == y is:  False
x != y is:  True
x >= y is:  True
x <= y is:  False

Example4.1: Complex values

x=3+4j
y=4+3j
print('x > y  is: ',x>y)
print('x < y  is: ',x<y)

Output:

Traceback (most recent call last):
  File "E:/python_practice/examples/relational.py", line 30, in <module>
    print('x > y  is: ',x>y)
TypeError: unorderable types: complex() > complex()

Example4.2: Complex values

x=3+4j
y=4+3j
print('x == y is: ',x==y)
print('x != y is: ',x!=y)

Output:

x == y is:  False
x != y is:  True

Example4.3: Complex values

x=3+4j
y=4+3j
print('x >= y is: ',x>=y)
print('x <= y is: ',x<=y)

Output:

Traceback (most recent call last):
  File "E:/python_practice/examples/relational.py", line 30, in <module>
    print('x >= y is: ',x>=y)
TypeError: unorderable types: complex() >= complex()

Logical Operators:

  • Logical operators are used to perform the mathematical logical operators.
  • Logical operators can be applied on Boolean type operands.


  • Logical and: both operands are True,the result will be True,otherwise return False.
                           Example:

                                                    print(True and True)
                                    print(True and False)
                                    print(False and False)
                                    print(False and True)

                        Output:

                                                   True
                                    False
                                    False
                                    False
  • Logical or: any one operand is True or both operands are True,the result will be True, otherwise return False.
                            Example:

                                                       print(True or True)
                                      print(True or False)
                                      print(False or False)
                                      print(False or True)

                          Output:

                                                      True
                                     True
                                     False
                                     True


  • Logical not: if operand is True,the result will be False.if operand is False,the result will be True.
                                 Example:

                                                                print(not True)
                                            print(not False)


                                 Output:

                                                        False
                                           True

Example1:

x = True
y = False
print('x and y is: ',x and y)
print('x or y is: ',x or y)
print('not x is: ',not x)

Output:

x and y is:  False
x or y is:  True
not x is:  False

Example2:

print(1 and True)
print(1 and 1)
print(1 and 0)
print(1 and False)

Output:

True
1
0
False

Note:
  • Here Python interpreter take 1 as True and 0 as False value


No comments:

Post a Comment