- 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
- == Equal to.
- != not equal to.
- > Greater than.
- < Less than.
- >= Greater than or Equal to.
- <= Less than or equal to.
- 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.
- x='AA'
- y='aa'
- 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.
- x=10
- y=20
- if(x!=y):
- 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.
- x="a"
- y="A"
- 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.
- x="a"
- y="A"
- 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.
- x=100
- y=200
- if(x<=y):
- 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).
No comments