We know most of you keep wondering- why does ‘a ==x or y or z’ always evaluate to true? So, let us discuss and learn why this happens?

Certainly, different operators in Python are used for performing different operations on variables and values. Most importantly, the operators are just standard symbols that have a specific purpose. == operator is one of those operators.

It is a relational operator that can compare the values and returns False or True as per the given condition. Above all, we can make multiple projects using the == operator of Python. 

And it has been seen that many students make a security system using Python programs during their academic courses. This system gives access to the authorized person only. Let’s dive deep and find out why does ‘a ==x or y or z’ always evaluate to true?

Understanding == operator

Firstly, the program for a security system can easily be built using the Python == operator. But, when the students use the program as:

Input:

Enna

Output

Hi. Please enter your name: Enna

Access granted

What is this!!! Why does it happen?? As you can see that the access must be granted to only Enna, John, and Ali. But, whatever names we give as inputs, it gives access to all. In other words, even if the statement changes as if “Enna” or “John” or “Ali” == name, still it will show the same result.

Are you facing the same issue? Unable to find the necessary solution? Don’t worry, we have provided the solution to this query with three alternative methods. Moreover, they are too simple to be messed with! So, let’s explore a new concept of Python with ease.

First, let’s understand what is getting executed in the above program!

In certain cases, Python behaves and looks similar to that of natural English. But in this kind of situation (as mentioned in the above program), Python does not take abstracts. 

You might be using the context clues that help you to know whether the “John” and “Ali” are objects or not. Therefore the program logical works to as:

if (name == “Enna”) or (“John”) or (“Ali”)

For Bob, it is equal works as:

if (False) or (“John”) or (“Ali”):

Note: The or operator is selecting the first argument as the positivity truth-value.

That is to say,

if “John”:

As the “John” has the positive truth value, the block of the if statement will be executed. This results in “Access granted” regardless of the given name.

Now, the expression if “Enna” or “John” or “Ali” == name, the value of “Enna” will be true. This also results in the execution of the if block statement.

What is the best solution to the above program?

There are three methods to solve the issue of the above program. Let’s check each of them one by one.

1. Use the “==” operator multiple times

Python users can use the “==” operator multiple times to explicitly check the value of each input.

How to use the “==” operator?

Input:

Bob

Output

Hi. Please enter your name: Bob

Access denied.

Input:

Enna

Output

Hi. Please enter your name: Enna

Access granted.

NOTE: The multiple “==” operator might be a slow process as you have to declare each name-value individually. 

2. Using elif condition:

The “elif” refers to else if. This condition allows the users to check the multiple expressions. In the case of FALSE if condition, it analyzes the condition given in the next elif condition. 

That is why we can say that when the conditions are FALSE, then the else body statement will be executed.

How to use the elif condition?

Input:

Enna

Output:

Hi. Please enter your name: Enna

Access granted

Input 2:

Bob

Output:

Hi. Please enter your name: Bob

Access denied

NOTE: This condition is also considered slow because it will take time to define each name-value individually. Moreover, it makes your program lengthy as it takes unnecessary codes to define each value.

3. Use the “in” operator

The “in” operator in Python uses for checking whether a value exists within the sequence or not. The statement of the “in” operator is evaluated to be TRUE when it finds a variable within the given sequence, otherwise, it results as FALSE.

How to use the “in” operator?

Input:

Bob

Output:

Hi. Please enter your name: Bob

Access denied

Input:

Enna

Output:

Hi. Please enter your name: Enna

Access granted

NOTE: This process considers being much faster and less time-consuming. In the “in” operator, the Python user needs to define the names in the “in” statement, and that it. It will execute the code value and give the result as per the statement. 

Let’s wrap our understanding towards- Why Does ‘a ==x or y or z’ Always Evaluate To True!!

It has been seen that the “==” operator works differently for a different condition. With the “or” logical operator it executes the first condition and considers others as POSITIVE VALUE. That is why you might be facing a similar issue with your code.

Earlier, we have defined the three alternate methods to solve this issue. Python users can use any of these methods as per their needs. But there is one thing that you have to keep in mind that is the speed and the length of the code.

Hope we have provided a valid explanation for your issue. But still, there is any confusion and anything that is not clear to you, comment it in the comment section. We will be glad to help you as my only and only aim to help you with your Python queries.  Master Python and other programming language courses from the Experts. 

Keep in touch with our Python blogs to enhance your conceptual knowledge about Python.”

 

Here are some interesting questions to practice your just acquired knowledge concerning why does ‘a ==x or y or z’ always evaluate to true?

1. What type of operator is “==”?

(A) Relational operator

(B) Comparison operator

(C) Assignment operator

(D) (A) & (B) Both

Correct Answer: (D) We use the “==” operator to compare the two operands. And the comparison operator is also known as a relational operator as “==” finds out the relation among both operands. This provides an answer to the most asked question- why does ‘a ==x or y or z’ always evaluate to true?

2. What is the output of the following program:

p = 20

q = 9

r = 1

if ( p == q ):

   print (“p is equal to q”)

else:

   print (“p is not equal to q”)

(A) p is equal to q

(B) p is not equal to q

(C) Error

(D) None

Correct Answer: (B) As you can see that p is not equal to q. That is why the “==” operator compares both operands and takes them as FALSE. So, the else condition will be followed.

3. Check whether the statement is TRUE or FALSE.

The code with the “==” operator will execute the result as TRUE if and only if the expression is equal, otherwise it shows FALSE.

(A) True

(B) False

Correct Answer: (A) This is true that the “==” operator shows the output as TRUE if the expression is equal. That said, now you know why does ‘a ==x or y or z’ always evaluate to true?

4. Can a Python user use the “==” operator to check the identity of the two objects?

(A) Yes

(B) No

Correct Answer: (A) The “==” operator can compare the identities of two objects. Python users can use it as id(x) == id(y).

5. Check whether the statement is true or false.

The in operator checks if the specified values are a constituent item of the sequence such as an array, tuple, string, or list or not.

(A) False

(B) True

Correct Answer: (B) This statement is true as the “in” operator can check the sequence values.
 

So, we are positive that you got a solution to the question- Why Does ‘a ==x or y or z’ Always Evaluate To True? In the same vein, to master Python with the help of more detailed blogs like this, stay tuned with GUVI