Python Comparison Operators Example Tutorial
Python comparison operators determine the equality or difference between values. The expression ultimately returns a boolean value. The comparison operators compare the values on either side of them and decide the relation among them. They are also called Relational operators.
Python Comparison Operators Example
Let’s take a simple print statement and output some comparisons.
print(21 > 19) print(18 > 21)
The first print statement returns the True and second returns a False.
The general comparison operators in Python Programming Language is following.
Now, let’s use the comparison operator with the if statement.
# app.py if (21 > 19): print('Obviously big is greater than small') else: print('It\'s not always True ')
In the above code, we will see that if block will be executed because 21 is bigger then 19.
Let’s use the less than (<) operator inside the if statement.
# app.py if (18 < 19): print('Obviously small is less than big') else: print('It\'s not always True ')
The output will be apparent execution of if block.
Now, let’s combine Logical Operator and Comparison Operator.
Write the following code inside the app.py file.
# app.py fortnite = 100 pubg = 99 clashofclans = 90 if (fortnite > pubg and fortnite > clashofclans): print('Fortnite was the biggest hit of 2018') else: print('Pubg was the biggest hit')
In the above code, we have defined the three variables and assigned the values to these variables.
Inside the if statement, we have compared the three variables as well as used the logical operators as well.
See the output.
Compare Characters in Python
Now, let’s compare the characters in Python.
# app.py print('a' < 'b') print('z' > 'y') print('A' > 'a')
In the above code, we have made the three comparisons.
First will return a True because the character has 97 ASCII value and character b has 98. So 97 is less than 98 hence True.
Second also returns a True because character z has 122 ASCII value and character y has 121. So 122 is higher than 121. Hence True.
Third one returns a False because the value starts from A, which is 65 is less than value starts from a, which is 96.
You can find any character’s ASCII value by using ord() built-in function in Python.
print('The ASCII value of z character is:',ord('z'))
See the output.
Total Comparison Operators in Python are the following.
|==||If the values of two operands are equal, then the condition becomes true.||(a == b) is not true.|
|!=||If the values of two operands are not equal, then condition becomes true.||(a!= b) is true.|
|>||If a value of left operand is greater than a value of right operand, then condition becomes true.||(a > b) is not true.|
|<||If a value of left operand is less than a value of right operand, then condition becomes true.||(a < b) is true.|
|>=||If a value of left operand is greater than or equal to a value of right operand, then condition becomes true.||(a >= b) is not true.|
|<=||If a value of left operand is less than or equal to a value of the right operand, then condition becomes true.||(a <= b) is true.|
Finally, Python Comparison Operators Example Tutorial is over.