python comparison operators

  • In python, the comparison operator is used to compare one value with the other value.
  • A comparison operator can be known as a relational operator as it shows the relation between the two values.
  • It returns the result in bool type i.e True or False.
  • It is applicable for string, boolean, numbers(int type).
  • In the comparison, if one comparison is true then the result will be true, and if one comparison is false all the results will be false.
  • Eg; 10<20>30, the result is false.
  • We have six different types of comparison operators/ relational operators
  1. ==  Equal to.
  2. != not equal to.
  3. >  Greater than.
  4. <   Less than.
  5. >= Greater than or Equal to.
  6. <= Less than or equal to.
  1. Equal to(==): If the values on both sides of operands are equal then the result is true, else false.
        Equal operator(==)  always check the content comparison. 
 syntax: (a==b).

Example: write a python code for comparison/relational operator using equal to the operator for string type.

  1. x='AA'
  2. y='aa'
  3. print(x==y)
output: false


2.     Not Equal to (!=): If both values in operands are not equal it results in true, else false.
syntax: (a!=b).

Example: write a python program using the not equal operator for an int data type.

  1. x=10
  2. y=20
  3. if(x!=y):
  4. print("x not equal to y")
output: x not equal to y.



3. Less than(<): If the value left operand is less than the right operand we use less than.
syntax: (a<b).

Example: write a python program using less than operator for string type.

  1.  x="a"
  2. y="A"
  3. print(x<y)
output: false.



4. Greater than(>):  If the value of the left operand is greater than the right operand then the result is true. 
syntax: (a>b).

Example: write a python program for string data type using greater than the operator.

  1. x="a"
  2. y="A"
  3. print(x>y)
output: true

5.Less than or equal to(<=): If the value in the left operand is less than or equal to right to operand it returns a true value.
syntax:(x<=y).

Example: write a python code for int data type using less than or equal to operator.

  1. x=100
  2. y=200
  3. if(x<=y):
  4. print("x less than or equal to y")
output: x less than or equal to y.


6. Greater than or equal to(>=): If the values in the left operand are greater than or equal to the right operand then the result is true else false.
syntax: (a>=b).

Example: write a python program for int data type using >= operator.

  1. 1000
  2. 200
  3. if(x>=y):
  4. print("x is greater than or equal to y")
output: x is greater than or equal to y.

Logical operators in python

  • In python, we have the logical operator.
  • "Logical operator" used to perform the different logical operations on the value of variables.
  • The logical operators are applicable for boolean and non-boolean types.
  • Boolean types are:
  • True, False
  • non-boolean types are :
  1. 0
  2. 1
  3. empty string. 
  • Here True means 1 and Flase means 0.      
  • There are three different types of logical operators, they are :
  1. And
  2. or
  3. Not
  •   And operator: And operator is used when both arguments are True then the result is True.
  • syntax:  x and y

Example: write a python code for And operator.

1.a=10
2.b=20
3.print(a>30 And b<30)
4.False



  •  or operator: or operator is used when at least any one of the arguments is true then the result will be True.
  • syntax: x or y

Example: Write a python code using the or operator.

  1. a=2
  2. b=5
  3. print(a<3 or b>)



  • not operator: not operator is the opposite to the result of an argument, if it is true then the result is False and vice-versa.
  • syntax: x not y

Example: write a python code using the not operator.

  1. x=5
  2. y=10
  3. print(not x==y)
  4. True

Assignment operator in python

  •  In python, we have the assignment operator
  • Definition: The assignment operator is used to assigning a value to variables.
  • eg:x=100
  • Here we just assign a value of 100 to the x variable.
  • we use the '"="  symbol to assign the values to the variables.
  • After assigning the values to the variables it performs the different types of operations.
  • The assignment operator mixed with another operator is called compound operator.
  • The different types of compound operators are:
  1. +=       
  2. -=      
  3. *=     
  4. /=      
  5. %= 
  6. //=    
  7. **=   
  8. &=
  9. |=
  10. ^=
  11. >>=
  12. <<=

Example: write a python program  using assignmnet operators.

  1. x=5
  2. x+=5
  3. print(x)
  4. 10
  5. x=2
  6. x-=2
  7. print(x)
  8. 0
  9. x=2
  10. x*=12
  11. print(x)
  12. 144
  13. x=10
  14. x/=5
  15. print(x)
  16. 2.0
  17. a=4
  18. a%=2
  19. print(a)
  20. 0

















Identity operator in python

  • Identity operator, before going to identity operator let's have a small idea about its background.
  • We have multiple operators from them special operator is the one, in that special operator we are having two types of operators :
  • 1.Identity operator.
  • 2.Membership operator.
  • Now let's talk about the identity operator,
  • "Identity operator" is used to compare whether the value in the first argument may be or may not be the same in the second argument.
  • In identity operator we have two  types, they are:
  1. is
  2.  is not
  • "is" operator is used to saying that the value is present in the corresponding argument.
  • If the value is present in the corresponding argument then it returns True, else False.
  • is operator always checks the address comparison
  • "is not " operator is used to saying that the value is not present in the corresponding argument.
  • If the value not presents then it returns the True otherwise False.

Example: write a python program using is identity operator.

  1. a="apple"
  2. b="apple"
  3. print(a is b)
  4. True


#2.write a python program using isnot operator.

  1. a="Apple"
  2. b="apple"
  3. print(a isnot b)
  4. True









































Python power operator

     let us know in detail about the python power operator.

  1. Power operator  is one of the arithmetic operator .. 
  2.  In python the power operator  can  be used  to calculate the  power of a number or  exponential .  
  3.  The power  operator in python is denoted by " ** " symbol. 
  4.  It always returns the float types values  only.
  5.  The power operator is having the  second highest  priority in the operator precedence as first priority goes to parenthesis. 
  6.  The power operator can be calculated in two ways :
  7.             1.  using **

                2.  using pow()function.

    • For two arguments we write the power as (a,b) i.e  a to the power of b
    • For three it is written as (a,b,c) i.e x to the power of b, modulus c.
    • The syntax for 3 arguments is (a,b,c).

    Now let us  go to  some practical examples:

    #1.write a python program to calculate the power of a number

    1. x=12**2
    2. print(x)
    3. 144


    #2.write a python program to calculate the power of a number.

    1. x=1**2
    2. print(x)
    3. 1










    Python not equal operator

    • Let's discuss python, not equal operators.  
    •  In python, there are different types of operators available,in that we have not equal operators.
    • Not equal to operator comes under the comparison operator as we compare the one variable with the other.
    • Python not equal to the operator is used for the same data type values.
    • When we compare one variable with another variable by using not equal operators we will get the boolean type values. bool type is True or False. if the values are not equal then it returns True value and if the values are equal then it returns the False value.
    • The syntax for not equal to is !=  mostly used in the python 3 versions.

    #1. write a python program to describe not equal to operator using integer

    1. a=2.5
    2. b=1.0
    3. c=5.0
    4. print(a!=b)
    5. true
    6. print(b!=c)
    7. true
    8. print(a!=b!=c)
    9. true

    python not equal operator

    #2. write a python program to describe not equal to an operator using string

    • Python, not equal operator  using string
    1. x='apple'
    2. y='ball'
    3. print(x!=y)
    output: true


    Select Menu