Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Relational Operators in Python

Last Updated on January 31, 2023 by Prepbytes

In this article, we will discuss the relational operators in python, types of relational operators in python, Some examples of relational operators in python, code implementation and its explanation, and one quiz on the relational operator.

What are Relational Operators in Python

Relational operators in python are the mathematical symbol, used for comparing the operand values, they return a boolean value(either True or False) according to the condition. They are also known as Comparison operators. mainly they are of 6 types.

>
<
==
!=
>=
<=

Types of Relational Operators in Python

        Operator         Description       Syntax
            > Greater than: return true if the value of the left operand is greater than the right a>b
            < Less than return true if the value of the left operand is lesser than the right a< b
            == Equal: return true if both operands are equal a==b
            != Not equal to return true if the operands are not equal a!=b
            >= Greater than equal to return true if the value of the left operand is greater than or equal to the right a>=b
          <= less than equal to: return true if the value of the left operand is less than or equal to the right a<=b

Example 1: Greater than(>)

Syntax:

a>b

Code:

a=20
b=13
#on output screen
print(a>b)

Output:

True

Explanation
In the above python program we have two variables a and b . the value of is 20 and the value of b is 13. So as we discuss earlier relational operator gives a boolean result, So here we use ‘>’ greater than (relational operator) it returns true if the left operand is greater than the right operand, and we know 20 is greater than 13, so in the output screen, we have [True] as a result.

Example 2: Less than(<)

Syntax:

 a

Code:

a=20
b=13
#on output screen
print(a

Output:

False

Explanation
In the above python program we have two variables a and b . the value of is 20 and the value of b is 13. So as we discuss earlier relational operator gives a boolean result, So here we use ‘<’ less than (relational operator) it returns true if the left operand is less than the right operand, and we know 20 is not less than 13, so in the output screen, we have [False] as a result.

Example 3: Equal to(==)

Syntax:

a==b

Code:

a=20
b=13
#on output screen
print(a==b)

Output:

False

Explanation
In the above python program we have two variables a and b . the value of is 20 and the value of b is 13. So as we discuss earlier relational operator gives a boolean result, So here we use ‘==’ equal to (relational operator) it returns true if the left operand is equal to the right operand, and we know 20 is not equal to 13, so in the output screen, we have [False] as a result.

Example 4: Not equal to (!=)

Syntax:

a!=b

Code:

a=20
b=13
#on output screen
print(a!=b)

Output:

True

Explanation
In the above python program we have two variables a and b . the value of is 20 and the value of b is 13. So as we discuss earlier relational operator gives a boolean result, So here we use ‘==’ not equal to (relational operator) it returns true if the left operand is not equal to the right operand, and we know 20 is not equal to 13, so in the output screen, we have [True] as a result.

Example 5: Greater than equal to (>=)

Syntax:

a>=b

Code:

a=20
b=13
#on output screen
print(a>=b)

Output:

True

Explanation
In the above python program we have two variables a and b . the value of is 20 and the value of b is 13. So as we discuss earlier relational operator gives a boolean result, So here we use ‘>=’ greater than or equal to (relational operator) it returns true if the left operand is greater than or equal to the right operand, and we know 20 is greater than 13, so in the output screen, we have [True] as a result.

Example 6: Less than equal to(<=)

Syntax:

a<=b

Code:

a=20
b=13
#on output screen
print(a<=b)

Output:

False

Explanation
In the above python program we have two variables a and b . the value of is 20 and the value of b is 13. So as we discuss earlier relational operator gives a boolean result, So here we use ‘<=’ less than equal to (relational operator) it returns true if the left operand is less than or equal to the right operand, and we know 20 is not less than or equal to 13, so in the output screen, we have [False] as a result.

Quiz:

Which of the following is not a relational operator?

=
<=
!=
!

Option D is right ans.
! is a logical operator.

Logical operators !,|,||,&,&&
Relational operator >,<,<=,>=,==,!=

Summary
A relational operator in Python is a symbol or keyword used to compare two values and determine the relationship between them. The most commonly used relational operators in Python are

< (less than)
> (greater than)
<= (less than or equal to)
>= (greater than or equal to)
== (equal to)
!= (not equal to)

These operators can be used in conditional statements and loops to control the flow of a program. For example, the following code uses the < operator to check if a variable x is less than 10

Leave a Reply

Your email address will not be published. Required fields are marked *