Written by:
In Python, the double equal sign ==
is a relevant operator that is used to compare two variables or values and determine whether they are equal or not. We will explore the utility of this operator in Python and how can be used in different situations. We prepared an article as an Intro to Python that we recommend to check before reading the following piece of content. Let’s start with a quick example so we can understand its functionality.
Let’s suppose that we have 2 variables called a
and b
with the following values assigned to:
1a = 5 2b = 10
Now, we want to verify if a
is equal to b
by using the double equal sign operator:
1if a == b: 2 print("a is equal to b") 3else: 4 print("a is not equal to b")
On the previous example, we declared 2 variables a
and b
with values 5 and 10 respectively. Then, we used the double equal sign operator in an if-else block to compare a
and b
values. Finally, if a
is equal to b
, our code will print "a is equal to b"
, otherwise it will print "a is not equal to b"
.
This operator in Python shows a comparison used to verify if a value is equal to another one. In this way, Python compares values on both sides of the operator and returns True
if they are equal and False
if they are not. Now, let's see different conditions' use cases.
1num1 = 15 2num2 = 15 3 4if num1 == num2: 5 print("Both numbers are the same") 6else: 7 print("Numbers are different")
In the previous example, we have two variables with the same numbers as values. When using the double equal sign ==
, the program will print "Both numbers are the same"
since both variables have the same number.
1name_1 = "John" 2name_2 = "Alexa" 3 4if name_1 == name_2: 5 print(“Names are the same”) 6else: 7 print("Names are different")
In this case, we have two variables that contain strings as values. Due to both strings being different, our code will print "Names are different"
.
Let's see our final example:
1truthy_variable = True 2falsy_variable = False 3 4if truthy_variable == falsy_variable: 5 print("Both values are the same") 6else: 7 print("Both values are different")
Finally, in this last example, we are comparing two boolean variables and because they are different, our code will print "Both values are different"
as expected.
The double equal sign operator in Python is completely essential when we want to compare values and evaluate conditions with if statements. This allows us to check if values are the same or not, which leads to different outputs depending on the values we have on variables.
We hope you can understand the use and importance of the double equal sign operator in Python. Now, you can enhance your programming skills with this knowledge and take better decisions in your apps. You can check the 4Geeks Blog for more amazing content.