Python for Beginners: And/or Operators
We’re back to Python. It’s almost as though we never left. So far, we’ve covered a lot of ground with lists, saving input to a file, functions and if/else statements, accepting input from users, and more. We’re going to continue our journey with this fantastic and easy-to-learn language with yet another very popular feature found in many languages… the and/or operators.
Remember last time, when we talked about the if/else statement that went something like:
if X is true then Y else Z?
In other words, if X = 2, then X is an even number, else it’s an odd number.
If/else statements are absolutely crucial to understand, as they make it possible for functions to execute, depending on the outcome of a condition.
We can do something similar with operators, which actually makes it possible to create more complex statements. Instead of being limited to an if/else statement like if X = 2 then X is an even number, else it’s an odd number, we can do something like if X = 10 and Y = 12, then both X and Y are odd numbers, else they might not be.
That’s a rather silly example because X could be 10 and Y could be 20, both of which are even numbers. To make this a bit more clear, we could do something like:
if X <= 2 and Y <= 2, then the sum of the two numbers is less than 5.
You use these logical operators on conditional statements that are either true or false. Both the and/or operators make it possible to test conditions and decide which execution path your program will take. This is straight-up Boolean logic, which allows you to create expressions that evaluate if something is true or false.
It’s important to understand the following Boolean concepts with Python:
- A Boolean is a value that can be either true or false.
- Boolean values are either True or False (capitalization matters).
- Boolean variables are variables that can be either True or False and are used as flags to indicate if a specific condition exists.
- Boolean expression is an expression that returns either True or False.
- Boolean context can be if conditions and/or while loops used to evaluate a Boolean value.
- Operands are objects within an expression connected by an operator.
- Boolean logical operators are AND, OR, and NOT.
In their most basic form, the operators work like this:
- X or Y
- X and Y
- X not Y
It’s then important to talk about true and false because it might seem a bit odd at first. With Python, if one subexpression (in our example above, subexpressions are X and Y) is True, then the expression is True. For an expression to evaluate to False, both expressions must be False. Here’s how that works.
- 10 < 20 and 10 > 5 = True
- 10 < 20 and 10 > 20 = True
- 10 < 5 and 10 > 30 = False
Let’s move that to Python code, shall we? Instead of writing a program out of the gate, let’s open the Python console with the command:
You should now see:
Type the following:
exp1 = 10 < 20
Python should print out:
Why? Because 10 is less than 20.
Okay, now type:
exp2 = 10 > 5
exp2 and Python will once again print out
exp3 = 10 > 20
Follow this with:
Python should print out:
What we now have is the following:
- exp1 = True
- exp2 = True
- exp3 = False
Let’s use our operators. Type:
exp1 or exp2
That should print out
True because both subexpressions are true. Next, type:
exp1 or exp3
This will still print out
True because one of our subexpressions is True.
Let’s create a fourth subexpression with:
exp4 = 10 < 5
exp3 or exp4
Guess what Python reports? False. Why? Because both subexpressions are false.
Now, let’s migrate this into a Python application. Exit the Python console with:
Create a new file with:
We’re going to create a simple program that will print out if one or both expressions are true. Most of this should already be familiar to you. Our program looks like this:
You should already know the outcome of this program because although the value for value_y isn’t less than 20, the value for value_x does equal 10. And because one of the subexpressions is True, the results are true. Save and close the file and run it with:
Let’s allow the user to enter values for both our variables. We do this with the lines:
x = int(input("Type a value for x "))
y = int(input("Type a value for y "))
Next, we use an if/else statement like so:
Our entire program looks like:
Save and close the file. Run the program with:
You will be asked to input 2 numbers. Once you do that, the program will compare the numbers and print “If either x is greater than or equal to 10 or y is less than or equal to 20, the expression is True” if one subexpression is true or “The expression is false” if both subexpressions are false.
The following inputted numbers would reveal the following results:
- 10 10 = True
- 1 10 = True
- 1 30 = False
But what if we change or to and? Does that change our outcomes? Yes. Our new application would be:
With the above program, our results would be:
- 10 10 = True
- 1 10 = False
- 1 30 = False
Why the change? Because the and operator returns True only if both operands are true, whereas the or operand returns True if both operands are true.
And there you go, the and/or operators. You’ll want to play around with these for a while until you grasp how they work. But knowing how these functions will be important as you advance deeper into the realm of Python.