AppDividend
Latest Code Tutorials

Python Comparison Operators Tutorial With Example

0

In this demo, we will see Python Comparison Operators Tutorial With Example. The Comparison operator determines 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 Tutorial

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.

 

Python Comparison Operators Tutorial

The general comparison operators in Python Programming Language is following.

  1. <
  2. >
  3. =

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.

 

Python Comparison Operators

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

Inside the if statement, we have compared the three variables as well as used the logical operators as well.

The output of the above code is following.

 

Logical and Comparison Operators in Python

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

Compare Characters in Python

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'))

The output is following.

 

Find a Character's ASCII Value in Python

Total Comparison Operators in Python are following.

Operator Description Example
== 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 Tutorial With Example is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.