Python if the equal condition | Example code

Python Comparison equal is used to compare the values on either side of them and decide the relation among them.

If the equal condition is true then if block executes. For that the values of two operands must be equal, then the condition becomes true.

a = 10
b = 10
if (a == b):
   #code

If the equal condition (if with ==) example code

If equals test in Python: The equals (==) operator tests for equality. It returns True if both values are the same otherwise returns False.

age = 18

# See if the age variable equals 18
if age == 18:
    print("You can do voe!")

Output:

Python if the equal condition

Is there a difference between “==” and “is”?

Answer: Yes there is a difference between an equal operator and “is” the keyword. “is” will return True if two variables point to the same object, “==” if the objects referred to by the variables are equal.

>>> a = [1, 2, 3]
>>> b = a
>>> b is a 
True
>>> b == a
True

# Make a new copy of list `a` via the slice operator, 
# and assign it to variable `b`
>>> b = a[:] 
>>> b is a
False
>>> b == a
True

Source: stackoverflow.com

Do comment if you have any doubts and suggestions on this Python if condition code.

Note: IDE: PyCharm 2021.1.3 (Community Edition)

Windows 10

Python 3.7

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.