- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
- Related Articles
- How to Swap Two Variables using Python?
- How to compare two strings using regex in Python?
- How to use IF statement in MySQL using Python?
- How to indent an if...else statement in Python?
- How to compare histograms of two images using OpenCV Python?
- How to compare two lists in Python?
- How to compare two images in OpenCV Python?
- Using variables with MySQL prepare statement
- Swap two variables in one line in using Python?
- How to use nested if statement in Python?
- How to compare two images using Java OpenCV library?
- How to set two variables in a stored procedure with a single MySQL select statement?
- How do we compare two lists in Python?
- How do we compare two tuples in Python?
- How do we compare two dictionaries in Python?

Advertisements