×

### Python Comparison Operators - Explained with examples Comparison operator is used to compare the values of two variables and based on that it will provide you the result either as true if the condition satisfied or false if the condition is not satisfied.

Here at LinuxAPT, we shall look into Python comparison operators and how to use them to compare two values.

#### What are the Comparison operators in Python ?

There are total 6 different comparators operators that are mentioned below which will be discussed in this article along with the syntax and examples. In other terms they are also known as relational operators because they are used to find the relation between two different variables.

1. Greater than (>)

The "greater than" operator is one of Python's comparison operators that is used to compare the values of two variables by putting a ">" between them. If the value of the variable which is on the left side is greater than on the right side, then the output is true otherwise it will be false.

For Example:

`x = 10y = 5x > y`

Output:

`True`

`x = 5y = 10x > y`

Output:

`False`

In the above example of Greater than operator we are using two variable "x" and "y" and assigning them values as x = 10 and y = 5. When the value of x is greater than the value of y the result is True otherwise it will show False in x = 5 and y = 10 case.

2. Less than (<)

The "Less than" operator is one of Python's comparison operators that is used to compare the values of two variables by putting a "<" between them. If the value of the variable which is on the left side is less than on the right side, then the output is true otherwise it will be false.

For Example:

`x = 5y = 10x < y`

Output:

`True`
`x = 10y = 5x < y`

Output:

`False`

As you can see example of less than operator, we are using two variable “x” and “y” and assigning them values x = 5 and y = 10. When the value of x is less than the value of y the result is True otherwise it will show False in case of x = 10 and y = 5.

3. Greater than or equal to (>=)

This operator combines two different operators together which are the greater than ">" and equals to "=". This means that if the value of the variable is either greater or equals to the value of the variable which is on the right side then the value will be true otherwise it will be false.

For Example:

`x = 10y = 5x >= y`

Output:

`True`

`x = 10y = 10x >= y`

Output:

`True`
`x = 5y = 10x >= y`

Output:

`False`

In above example of "greater than or equal to" since the values of variables x and y are equal, the answer returned is True or when value of x is greater than the value y, otherwise it returns False. As a practice, execute the above code with just the "greater-than" operator and see the answer.

4. Less than or equal to (<=)

The less-than equal-to operator combines the equal-to and less-than comparison operators in Python. The operator "less than equal to" return True if the value on the left right-hand side is smaller than or equal to the value on the right.

For Example:

`x = 5y = 10x <= y`

Output:

`True`
`x = 10y = 10x <= y`

Output:

`True`

`x = 10y = 5x <= y`

Output:

`False`

In above example of "less than or equal to" since the values of variables x and y are equal, the answer returned is True or when value of x is less than the value of y, otherwise it returns False. As a practice, execute the above code with just the "less-than" operator and see the answer.

5. Equal to (==)

The equal to operator in Python returns True only if two variables under consideration are equal, otherwise the result is False. Two equal marks, i.e., "==", signify the "equal to" operator.

For Example:

`x = 10y = 10x == y`

Output:

`True`
`x = 5y = 10x == y`

Output:

`False`

As expected, since x is equal to y (with values 10 and 10), the answer returned is True. Otherwise, it returns False in case of x = 10 and y = 5.

6. Not Equal to (!=)

The not equal to operator in Python returns True only if two variables under consideration are not equal, otherwise the result is False. "!=", signify the "not equal to" operator.

For Example:

`x = 10y = 5x != y`

Output:

`True`
`x = 10y = 10x == y`

Output:

`False`

Now in this example of "not equal to" operator in python only return True when the value of x is not equal to value of y. However, when both the value of x and y are same it will return False.

#### Conclusion

This article covers Python Comparison Operators explained with examples. In fact, Python Comparison Operators compare two operands and return a boolean value based on the comparison made.

#### Search by typing a one letter Word Term Here

• #### Install VidCutter on Linux Mint 21 "Vanessa" - Step by step guide

• #### Install TeXworks on Ubuntu 22.10 (Kinetic Kudu) - Step by step process

• #### DU Command in Linux - More about it ?

• #### Create a Network Bridge on CentOS 8 - Step by step guide ?

• #### What is CHOWN Command in Linux ?

• #### Check Ports in Use on Ubuntu 22.04 (Jammy Jellyfish)

• #### Install ZSH on Ubuntu 22.04 (Jammy Jellyfish) - Step by step guide ?

• #### How to use Ansible Loops ?

• #### Ansible 'When' Conditionals - More about it ?

• #### AWK Features Explained with Examples

• #### Bash "If String Equals" Comparison - Complete guide ?

• #### 6 Best Text Editors for Linux

• #### Disable the Network Interface on Boot in CentOS 8 - Step by step guide ?

• #### DMIDECODE Command in Linux - How to use it ?

• #### Compare Numbers in a Bash Script - How to do it ?

• #### Linux LSOF Command - Explained with Examples

• #### LVEXTEND Command in Linux - Explained with Examples ?

• #### PASSWD Command in Linux - Explained with Examples

• #### Replace a String in a File Using Ansible

• #### Rsync Command Options - Explained with Examples

• #### Execute Remote Command with SSH

• #### Linux sed Command Options - Explained with examples ?

• #### How Tags in Ansible Playbook works running Ubuntu 22.04 (Jammy Jellyfish) ?

• #### TAR Command Options in Linux - Explained with Examples

• #### Ansible "Debug" Module - Explained with Examples with Ubuntu

• #### ulimit Linux Command - Explained with Examples

• #### Working with Variables in Ansible running Ubuntu 22.04 (Jammy Jellyfish)

• #### Fix Inactive UFW Status on Ubuntu 22.04 (Jammy Jellyfish)

• #### Run Background Process in Ubuntu 22.04 (Jammy Jellyfish) Linux system

• #### UFW Allow Port on Ubuntu 22.04 (Jammy Jellyfish)

• #### UFW Remove Rule in Ubuntu 22.04 (Jammy Jellyfish) Linux system

• #### Python Compare Strings - How it works ?

• #### Which Flavour of Ubuntu Should You Choose ?

• #### Install pageedit editor on Ubuntu 22.04 (Jammy Jellyfish) - Step by step guide ?

• #### Install Webfs Server on Jammy Jellyfish Ubuntu 22.04 system

• #### Install kakoune Editor on Ubuntu 22.04 (Jammy Jellyfish) - Step by step guide ?

• #### Install Luakit browser on Ubuntu 22.04 (Jammy Jellyfish) - Step by step guide ?

• #### Install Gnome Edit on Ubuntu Jammy Jellyfish ( 22.04 ) - Step by step guide ?

• #### Install Pluma on Debian 11.4 (Bullseye) System - Step by step guide ?

• 