Pages

Monday, August 12, 2019

Operators in Python


Operators in Python

In python, following operators are generally used:
1.      Comparison Operator
2.      Logical Operator

1.      Comparison Operator:
In Python, there are following types of comparison operators:

                    i.            Less than (< ) Operator
                  ii.            Greater than (>) Operator
                iii.            Less than or Equal to (<= ) Operator
                iv.            Greater than (>=) Operator

i.                    Less than (< ) Operator:
·         It checks left value is less than that of right value. If it is less then output is true otherwise output is false.
·         For example:
>>> 1 < 2
      Output is true, because 1 is less than 2.

      Suppose consider another example:
>>> 4 < 2
     Output if false, because 4 is not less than 2.

ii.                  Greater than (>) Operator:
·         It checks left value is greater than that of right value. If it is greater then output is true otherwise output is false.
·         For example:
>>> 2 > 1
      Output is true, because 2 is greater than 1.

      Suppose consider another example:
>>> 2 > 4
     Output if false, because 2 is not greater than 4.

iii.                Less than (<= ) Operator:
·         It checks left value is less than or equal to that of right value. If it is less or equal then output is true otherwise output is false.
·         For example:
>>> 1 < = 4
      Output is true, because 1 is less than 4.

      Suppose consider another example:
>>> 2 <= 2
     Output if true, because 2 is equal to 2.

iv.                Greater than (>= ) Operator:
·         It checks left value is greater than or equal to that of right value. If it is greater or equal then output is true otherwise output is false.
·         For example:
>>> 4 > = 1
      Output is true, because 4 is greater than 1.

      Suppose consider another example:
>>> 2 >= 3
     Output if false, because 2 is not greater or equal to 3.


2.      Logical Operator:
Following table describes the Logical Operators in Python:

Sr. No.
Operator
Meaning
Example
1
AND
If both the operands are true then condition
becomes true otherwise false.
(x and y) is
False.
2
OR
If any of the two operands are non-zero then condition becomes true otherwise false.
(x or y) is
True.
3
NOT
Used to reverse the logical state of its operand.
Not(x and y)
is True.

Consider the following output for Logical Operator in Jupyter tool:




No comments:

Post a Comment