Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

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; 1030, the result is false.
  • We have six different types of comparison operators.
  1. ==  Equal to.
  2. != not equal to.
  3. >  Greater than.
  4. >= Greater 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 operator using equal to the operator for string type.

  1. x='AA'
  2. y='aa'
  3. print(x==y)
output: 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

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

  1.  x="a"
  2. y="A"
  3. print(x
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

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



This post first appeared on Java Tutorial - InstanceOfJava, please read the originial post: here

Share the post

python comparison operators

×

Subscribe to Java Tutorial - Instanceofjava

Get updates delivered right to your inbox!

Thank you for your subscription

×