Python Equal Operator "=="
Python Equal - Comparison Operator
Python Equal is a Comparison Operator used to check if two values are equal.
The symbol used for Python Equal operator is ==
.
Equal Operator is mostly used in boolean expressions of conditional statements like If, If-Else, Elif, While, etc.
Syntax
Following is the syntax of Python Equal Comparison Operator.
result = operand_1 == operand_2
where operand_1
and operand_2
are values of any datatype. Equal operator returns true if operand_1
and operand_2
are equal values, else it returns false.
Example 1: Equal Comparison Operator
In this example, we shall take two integers, and check if they are equal using ==
.
Python Program
a = 10
b = 12
c = 12
print(a == b)
print(b == c)
Output
False
True
Example 2: Equal Operator with IF Statement
In the following example, we shall use equal operator in IF condition.
Python Program
a = 10
if a%2 == 0 :
print(a, "is even number.")
Output
10 is even number.
a%2 == 0
returns true for a=10
.
Example 3: Equal Operator with Strings
In this example, we shall use Equal Operator to check if two strings are equal.
Python Program
a = "python"
b = "python"
if a == b :
print("a and b have same value.")
Output
a and b have same value.
Example 4: Equal Operator in While Condition
You can use equal operator in while loop condition.
Python Program
a = 20
while int(a/6) == 3:
print("hello")
a += 1
Output
hello
hello
hello
hello
Summary
In this tutorial of Python Examples, we learned what Python Equal Comparison Operator is, how to use it to find if two values are equal, with the help of well detailed example programs.