Comparison operators help us to compare the values assigned to the variables and output a boolean value ( true or false) . Note that a value with any datatype ( integer , float , string…) can easily be compared.Let us quickly go through some basic comparison operators and than we will take some examples for each operator.

Operator | Description | Example |
---|---|---|

== | If the values of two operands are equal , than the condition becomes true. | (a==a) is true while (a==b) is false |

!= | If values of two operands are not equal , than condition becomes true. | (a!=a) is false while (a!=b) is true. |

> | If the value of left operand is greater than the value of right operand , than condition becomes true. | (4>5) is false while (5>4) is true. |

< | If the value of right operand is greater than value of left operand , than condition becomes true. | (5 <4) is false while (4<5) is true. |

>= | If the value of left operand is greater than or equal to the right operand , than the condition becomes true. | (5>=4) is true. |

<= | If the value of the left operand is less than or equal to the right operand , than the condition becomes true. | (5 <=4) is false |

`a = 10 b = 5 print(a>b) print(b>a) #let us compare strings print("abc" == "cba") print(10<=9) print(9>=10)`

`True False False False False`

A single comparison is often not enough and therefore the comparison operators are chained up with the logical operators to perform multiple comparisons in a single statement. Let us quickly go through the logical operators in Python.

Python has 3 logical operators :** and **, **or** and **not **operator. Of the three operators , ‘and’ and ‘or’ operator will be used quite frequently. For ‘and’ operator to output true boolean value , multiple comparisons must also return true boolean value. For ‘or’ operator to output true boolean value , atleast single comparison must return true boolean value.

**Truth value table for ‘and’ operator ( for statement ‘comparison 1 and comparison 2’ ) : **

comparison 1 | comparison 2 | returned value |
---|---|---|

True | True | True |

True | False | False |

False | False | False |

False | True | False |

**Truth value table for ‘or’ operator ( for statement ‘comparison 1 or comparison 2’ ) : **

comparison 1 | comparison 2 | returned value |
---|---|---|

True | True | True |

True | False | True |

False | False | False |

False | True | True |

Let us take a example where we chain multiple comparison operator using logical operators.

` # AND print(5>2 and 5>1) #true since both comparison return true print(5>2 and 5<1) # false since one comparison returns false # OR print(5>2 or 5<1) # true , since one comparison returns true`

`True False True`

Till now we discussed about using multiple comparison operators , let us go on to see an example where we use multiple logical operators in a single statement.

`print( (5>1) and ((5>1) or (5<1))) print((5<1) and ((5>1) and (5>3)))`

`True False`

The 'not' logical operator is not used as frequently as the 'and' and 'or' operators. However you will be using it when you want to reverse the logical state of the comparison. Before we go on to take an example of this operator, please note that :

- not ( True ) = False
- not ( False ) = True

`print(not(5>1)) print(not( 5>1 and 5<3))`

`False True`