How to compare two variables in an if statement using Python?


You can compare 2 variables in an if statement using the == operator. 

example

a = 10
b = 15
if a == b:
   print("Equal")
else:
   print("Not equal")

Output

This will give the output −

Not Equal

You can also use the is the operator. 

example

a = "Hello"
b = a
if a is b:
   print("Equal")
else:
   print("Not equal")

Output

This will give the output −

Equal

Note that is will return True if two variables point to the same object, == if the objects referred to by the variables are equal.

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 05-Mar-2020

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements