Register Login

== Operator in Python

Updated Jan 28, 2022

Everyone associated with computer science knows about the Python programming language. Operators play a significant role in Python. But many people find it confusing to understand the use of == operator. According to a survey in 2020, more than 8 million python developers are there in the world. This article explains the relational operator / comparison operator '==' (equal to).

What does == mean in Python?

In python, == is a comparison operator. '==' returns a Boolean statement – true or false after comparing the values of two operands. If both the r-value and the l-value remains the same, the, it returns true, otherwise returns false. The operand can be any number (integer or floating point), string, single character, etc. Below are some examples:

Code:

A = 3
B = 3
print("A == B:", A == B )
a = 2
b = 4
print("a == b:", a == b )
str1 = "man"
str2 = "man"
print("str1 == str2 :", str1 == str2)
S1 = "man"
S2 = "woman"
print("S1== S2 :", S1 == S2)
lista= [2, 5, 4, 0, 1, 5, 3, 2, 0, 2, 5, 7, 8, 6, 8]
listb= [2, 5, 4, 0, 1, 5, 3, 2, 0, 2, 5, 7, 8, 6, 8]
print("lista==listb: ", lista == listb) 
ListA= [2, 5, 4, 0, 1, 9, 3, 9, 0, 2, 9, 7, 8, 6, 8]
ListB= [2, 5, 4, 0, 1, 0, 3, 2, 0, 2, 5, 7, 8, 6, 8]
print("ListA==ListB: ", ListA == ListB)

Output:

Explanation:

First we have initialized two variables A and B and then use the print() to show whether A==B. Since, both the value is 3, hence it will result in true. Next we will take 2 more variables a and b and then use the print() to show whether a==b. Since, both of them have different values, so the == operator will result to False. The same thing has been displayed using string where the == operator will check character by character to determine whether both the variables hold the same set of characters or not. When it comes to list, both the lists (lista and listb) get checked comparing each element one by one.

Difference between '=' and '==' operator.

Not only in Python, but in every programming language, a single equal symbol '=' is called an assignment operator, and double '==' is called a comparison operator. '=' assigns values to the variables, whereas the '==' compares the values of two variables or operands and returns a Boolean statement. Below is an example for better clarification:

Code:

A = 1
print("A is assigned a value:", A)
B = 'a'
print("B is assigned a value:", B)
C =" Hello, my name is Python"
print("C is assigned a value:", C)
A = 3
print("Here, the '==' operator compares A and B and returns a boolean statement: ", A == B )
ListA = [2, 5, 4, 0, 1, 9, 3, 9, 0, 2, 9, 7, 8, 6, 8]
ListB = [2, 5, 4, 0, 1, 0, 3, 2, 0, 2, 5, 7, 8, 6, 8]
print("Here, the '==' operator compares ListA and ListB and returns a boolean statement: ", ListA == ListB)
lista = [2, 5, 4, 0, 1, 5, 3, 2, 0, 2, 5, 7, 8, 6, 8]
listb= [2, 5, 4, 0, 1, 5, 3, 2, 0, 2, 5, 7, 8, 6, 8]
print("Here, the '==' operator compares lista and listb and returns a boolean statement: ", lista == listb)

Output:

Explanation:

Here, we will initialize the variable A, B, and C with two different types of values. This is where we are showing how to initialize using the = operator. Then in a context, we have initialized a variable A and compare it with B which is having a single character. Since, both are not same, therefore, the == will return false. When it comes to list, both the lists (lista and listb) get checked comparing each element one by one.

Conclusion:

Python is rich in operators. In every programming language and in Python as well, the double equal '==' compares two operands and returns true or false. This results to True if the operands (R-value and L-value) are same or else returns false. '=' is different from '=='. '=' is for assigning values.


×