In this article, you’ll study a stimulating problem in programming. This program will seem simple to you just for the matter of it really being simple. However, deep-diving into it can help us learn some fundamentals about Boolean operators. And even a few other data types in a really cool way; one that we have not yet imagined or learned about. By exploring the methods mentioned in the subsequent sections, you’ll want to explore more. So, without wasting any extra moment let me begin with the topic. So, today’s topic of discussion is How to test multiple variables against a single value Python?

How to test multiple variables against a single value Python?

For the topic mentioned above, we have multilateral approaches that we can follow. Let us consider the various approaches and the different methods, one by one. So, let’s begin with the first method in the upcoming section.

Using the OR operator

a = input(int("Enter a number : "))
b = input(int("Enter another number : "))
if a == 9 or b == 25 :
      print('Hurray! Your value is present")
else:
      print('Alas! The value you are looking for is not present')

# Output 1 :
Enter a number : 24
Enter another number : 29
Alas! The value you are looking for is not present

# Output 2 :
Enter a number : 9
Enter another number : 26
Hurray! Your value is present

Here’s your guide to declaring variables in Python !!!

Using the ‘in’ keyword

The ‘in’ keyword is generally used to look for sequences or patterns in a list. But, we can also use this ‘in’ keyword to test multiple variables against a single value in Python. So, let’s see its application below :

a = 20
if a in [10, 20, 901, -29, 1001]:
      print('Hurray! Your value is present")
else:
      print('Alas! The value you are looking for is not present')

# Output :
Hurray! Your value is present

Now, let us check the inverse case of the ‘in’ keyword that is the case of ‘not in’. For this let us take another example as given below :

a = 10
if a not in [11, 25, -50]:
       print('Hurray! Your value is indeed not present")
else:
      print('Alas! The value you are looking for is present')

# Output :
Alas! The value you are looking for is present

Using the comparison (==) operator

This method can be used to check multiple variables with a single value.

a = 5
b = 5
if a == b == 5 :
      print('Hurray! Your value is present")
else:
      print('Alas! The value you are looking for is not present')

# Output :
Hurray! Your value is present

# Suppose the value of a is assigned as 10 while b remains or is changed. Then the output will be.

# Output 2:
Alas! The value you are looking for is not present

Miscellaneous Problems in Python

# Check if the values in multiple variables are greater than say 5
x = 5
b = 4
comparison = a > 5 or b > 5   # It gives a boolean result as output

# Output :
TRUE

# Now, let us use an and comparison operator to check if both the values satisfy the above condition.
comparison = a > 5 and b > 5

# Output :
FALSE

SUMMING UP !!

This work is a result of extensive research and multiple experiments. Hoping that this guide goes a long way in clearing your various doubts and resolving your queries. The methods given in this reference are pretty simple to use and straightforward to understand and get habitual with. We see the usage and application of the different comparison operators. The ‘in’ and the ‘not in’ keywords too. Thus, until next time, see- you !! Take care and stay happy and blessed. Goodbye !!

Categorized in: