• 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.

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu