- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- 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.
Advertisements