- Python Comparison Operators
- Introduction to Python comparison operators
- Less than operator (<)
- Less than or equal to operator (<=)
- Greater than operator (>)
- Greater Than or Equal To operator ( >= )
- Equal To operator (==)
- Not Equal To operator (!=)
- Summary
- Python If . Else
- Example
- Indentation
- Example
- Elif
- Example
- Else
- Example
- Example
- Short Hand If
- Example
- Short Hand If . Else
- Example
- Example
- And
- Example
- Or
- Example
- Not
- Example
- Nested If
- Example
- The pass Statement
- Python Comparison Operators
- Comparison Operators
- Types of Comparison Operators in Python
- 1. Less Than ( < )
- 2. Greater Than ( > )
- 3. Equal To ( == )
- 4. Not Equal ( != )
- 5. Less than or Equal to (<=)
- 6. Greater than or Equal to (>=)
- Python Comparison Operators Example
- Summary
Python Comparison Operators
Summary: in this tutorial, you’ll learn about Python comparison operators and how to use them to compare two values.
Introduction to Python comparison operators
In programming, you often want to compare a value with another value. To do that, you use comparison operators.
Python has six comparison operators, which are as follows:
- Less than ( < )
- Less than or equal to (
- Greater than ( > )
- Greater than or equal to ( >= )
- Equal to ( == )
- Not equal to ( != )
These comparison operators compare two values and return a boolean value, either True or False .
And you can use these comparison operators to compare both numbers and strings.
Less than operator (<)
left_value < right_value
Code language: Python (python)
The following example uses the Less Than ( < ) operator to compare two numbers:
>>> 10 < 20 True >>> 30 < 20 False
Code language: Python (python)
It’s quite obvious when you use the less-than operator with the numbers.
The following example uses the less than operator ( < ) to compare two strings:
>>> 'apple' < 'orange' True >>> 'banana' < 'apple' False
Code language: Python (python)
The following example shows how to use the less-than operator with variables:
>>> x = 10 >>> y = 20 >>> x < y True >>> y < x False
Code language: Python (python)
Less than or equal to operator (<=)
The less than or equal to operator compares two values and returns True if the left value is less than or equal to the right value. Otherwise, it returns False :
left_value
Code language: Python (python)
The following example shows how to use the less than or equal to operator to compare two numbers:
>>> 20 20
True >>> 10 20 True >>> 30 30 TrueCode language: Python (python)
And this example shows how to use the less than or equal to operator to compare the values of two variables:
>>> x = 10 >>> y = 20 >>> x True
>>> y FalseCode language: Python (python)
Greater than operator (>)
The greater than operator ( > ) compares two values and returns True if the left value is greater than the right value. Otherwise, it returns False :
left_value > right_value
Code language: Python (python)
This example uses the greater than operator ( > ) to compare two numbers:
>>> 20 > 10 True >>> 20 > 20 False >>> 10 > 20 False
Code language: Python (python)
And the following example uses the greater than operator ( > ) to compare two strings:
>>> 'apple' > 'orange' False >>> 'orange' > 'apple' True
Code language: Python (python)
Greater Than or Equal To operator ( >= )
The greater than or equal to operator ( >= ) compares two values and returns True if the left value is greater than or equal to the right value. Otherwise, it returns False :
left_value >= right_value
Code language: Python (python)
The following example uses the greater than or equal to operator to compare two numbers:
>>> 20 >= 10 True >>> 20 >= 20 True >>> 10 >= 20 False
Code language: Python (python)
And the following example uses the greater than or equal to operator to compare two strings:
>>> 'apple' >= 'apple' True >>> 'apple' >= 'orange' False >>> 'orange' >= 'apple' True
Code language: Python (python)
Equal To operator (==)
The equal to operator ( == ) compares two values and returns True if the left value is equal to the right value. Otherwise, it returns False :
left_value == right_value
Code language: Python (python)
The following example uses the equal to operator ( == ) to compare two numbers:
>>> 20 == 10 False >>> 20 == 20 True
Code language: Python (python)
And the following example uses the equal to operator ( == ) to compare two strings:
>>> 'apple' == 'apple' True >>> 'apple' == 'orange' False
Code language: Python (python)
Not Equal To operator (!=)
The not equal to operator ( != ) compares two values and returns True if the left value isn’t equal to the right value. Otherwise, it returns False .
left_value != right_value
Code language: Python (python)
For example, the following uses the not equal to operator to compare two numbers:
>>> 20 != 20 False >>> 20 != 10 True
Code language: Python (python)
And the following example uses the not equal to operator to compare two strings:
>>> 'apple' != 'apple' False >>> 'apple' != 'orange' True
Code language: Python (python)
Summary
- A comparison operator compares two values and returns a boolean value, either True or False .
- Python has six comparison operators: less than ( < ), less than or equal to ( ), greater than or equal to ( >= ), equal to ( == ), and not equal to ( != ).
Python If . Else
Python supports the usual logical conditions from mathematics:
- Equals: a == b
- Not Equals: a != b
- Less than: a < b
- Less than or equal to: a
- Greater than: a > b
- Greater than or equal to: a >= b
These conditions can be used in several ways, most commonly in "if statements" and loops.
An "if statement" is written by using the if keyword.
Example
In this example we use two variables, a and b , which are used as part of the if statement to test whether b is greater than a . As a is 33 , and b is 200 , we know that 200 is greater than 33, and so we print to screen that "b is greater than a".
Indentation
Python relies on indentation (whitespace at the beginning of a line) to define scope in the code. Other programming languages often use curly-brackets for this purpose.
Example
If statement, without indentation (will raise an error):
Elif
The elif keyword is Python's way of saying "if the previous conditions were not true, then try this condition".
Example
In this example a is equal to b , so the first condition is not true, but the elif condition is true, so we print to screen that "a and b are equal".
Else
The else keyword catches anything which isn't caught by the preceding conditions.
Example
a = 200
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")
In this example a is greater than b , so the first condition is not true, also the elif condition is not true, so we go to the else condition and print to screen that "a is greater than b".
You can also have an else without the elif :
Example
Short Hand If
If you have only one statement to execute, you can put it on the same line as the if statement.
Example
Short Hand If . Else
If you have only one statement to execute, one for if, and one for else, you can put it all on the same line:
Example
One line if else statement:
This technique is known as Ternary Operators, or Conditional Expressions.
You can also have multiple else statements on the same line:
Example
One line if else statement, with 3 conditions:
And
The and keyword is a logical operator, and is used to combine conditional statements:
Example
Test if a is greater than b , AND if c is greater than a :
Or
The or keyword is a logical operator, and is used to combine conditional statements:
Example
Test if a is greater than b , OR if a is greater than c :
Not
The not keyword is a logical operator, and is used to reverse the result of the conditional statement:
Example
Test if a is NOT greater than b :
Nested If
You can have if statements inside if statements, this is called nested if statements.
Example
if x > 10:
print("Above ten,")
if x > 20:
print("and also above 20!")
else:
print("but not above 20.")
The pass Statement
if statements cannot be empty, but if you for some reason have an if statement with no content, put in the pass statement to avoid getting an error.
Python Comparison Operators
Operators are symbols in programming languages that perform the operation on the operands and decide the operation type. For instance, the equal to ( == ) operator checks for equal values and returns True if they are equal, and False otherwise, whereas the not equal ( !=) operator does the exact opposite.
In Python, operators can be divided into several categories based on their functionality such as Arithmetic Operators to perform mathematical operations, Comparison Operators to compare two values, Logical Operators to combine multiple conditions and evaluate whether they are True or False, Bitwise Operators to perform operations on the individual bits of a binary number, etc.
In this tutorial, we will learn about all types of Comparison Operators and demonstrate them with examples to understand their working.
Comparison Operators
Python comparison operators, also known as relational operators, are used in comparing two values and applying conditions respectively.
Here there can be two possible outputs, either boolean True or False .
There are several types of comparison operators, each performing a unique operation, let’s look at them one by one.
Types of Comparison Operators in Python
There are 6 types of comparison operators in Python:
- Less Than ( < )
- Greater Than ( > )
- Equal To ( == )
- Not Equal ( != )
- Less Than or Equal To (
- Greater Than or Equal To ( >= )
1. Less Than ( < )
It is used to check for the smaller value or variable containing a smaller value as compared with the other number or variable. It will return True if the provided number or a variable is smaller than the given number or variable, otherwise, it will return False.
2. Greater Than ( > )
Python Greater Than operator is used to check for the greater value or variable containing greater value as compared with the other number or variable. If the provided number or a variable is greater than the given number or variable then it will return True, else, it will return false.
a = 10 if (a > 10): print("True") else: print("False")
3. Equal To ( == )
Python Equal To operator checks for equal values. It compares elements and if they are equal then it will return True else False.
a = 10 b = 20 if (a == b): print("True") else: print("False")
4. Not Equal ( != )
Python Not Equal operator is denoted by != , this does the exact opposite of the equal to operator. It returns True if the values on either side of the operator are unequal.
5. Less than or Equal to (<=)
This operator executes to True only if the value on the left is less than or equal to that on the right.
b is either less or equal to a
6. Greater than or Equal to (>=)
This operator executes to True only if the value on the left is greater than or equal to that on the right.
a = 5 b = 15 if(b >= a): print("b is either greater or equal to a")
b is either greater or equal to a
Python Comparison Operators Example
Let’s write the code to demonstrate each comparison operator we have seen earlier.
a = 10 b = 5 c = 0 if a == b: print("a is equal to b") else: print("a is not equal to b") if a != b: print("a is not equal to b") else: print("a is equal to b") if a < b: print("a is less than b") else: print("a is not less than b") if a >b: print("a is greater than b") else: print("a is not greater than b") a = 6; b = 15; if a = a: print("b is either greater than or equal to a") else: print("b is neither greater than nor equal to a")
a is not equal to b a is not equal to b a is not equal to b a is not less than b a is greater than b a is either less than or equal to b b is either greater than or equal to b
Summary
During this tutorial, we have discussed 6 operators that programmers used to compare values, also called Python comparison operators, let us summarize them one by one.
- Less than ( < )returns True if the value of the left operand is lesser than the value of the right operand.
- Greater than ( > ) returns True if the value of the left operand is greater than the value of the right operand.
- Equal to ( == ) returns True if the value of two operands is equal.
- Not equal ( != ) is the exact opposite of the equal to operator.
- Less than or equal To ( <=)returns True if the value on the left is less or equal to that on the right.
- Greater than or equal to (>=) returns True if the value on the left is greater than or equal to that on the right.
Hope you now have a clear understanding of Python comparison operators.