dcsimg
LinkedIn
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Jul 8, 2021

Python Logical Operators

This Python tutorial discusses how to work with logical operators in Python. In particular, we will look at the and, or, and not operators and how to use them with Boolean - or TRUE and FALSE - values.

In a previous article we covered Python Comparison Operators https://www.devx.com/tips/python-comparison-operators.html. You may wish to read that article before continuing with the code examples in this article.

How to Use Python Logical Operators

Below, we will discuss the three types of logical operators Python has to offer and practice using them with some simple sample code. We will start with the logical operator not and move on from there.

Using the Python not Operator

The Python not operator is used to evaluate whether a value is not "X". It is a tricky operator to get used to using because it always returns a Boolean value of True or False no matter what value you use it upon. The tricky part to using it, really, is the fact that if the input "X" has a value of True, then not will return False. Likewise, if the input "X" has a value of False, then it returns True. Confused yet? Me too.

To better understand its use, consider the following Python code demonstrating the notoperator:

x = 100
print(not x < 200)

In this code, we assign the variable x with the value of 100. We then create a print() function, in which we encase a not evaluation that states, essentially: not x < 200. Since x is less than 200 - or in a Boolean case, True - this program returns the result:

False

Remember, not always returns the opposite result. To see this in action, modify the above code so that we are instead evaluating whether x is greater than 200.

x = 100
print(not x > 200)

Since this would normally evaluate to False - because 100 is not greater than 200 - we know that the outcome will actually be the opposite, thanks to the not operator. So, if you run this code, you get the result:

True

One final note about the not/i> operator: when working with strings, using not not returns True if the string is empty and False if the string is not empty.

Using the Python or Operator

The next Python logical operator we will learn is the or operator. This operator is much easier to understand than the not operator. It works by testing whether either of two conditions are True. If either are not, then it evaluates to False. A simpler ay to view this is to say that or evaluates to True if either "X" OR "Y" is True. If neither "X" or "Y" are True, then it evlautes to False.

Here is an example code showing how to use the or operator in Python:

x = 100

print(x >200 or x <200)

The code example above assigns the value of 100 to the variable x. We then create a print() function and enclose an or evaluation within it. This or checks to see if the value of x is greater than 200 OR less than 200. If either of these two checks are True, then it will output "True" to the screen. If neither of the checks is True, then it will print "False" to the screen.

As you might suspect, the result of running this code is:

True

Using the and Logical Operator in Python

Our final logical operator we will be reviewing is the and operator. It is similar to the or operator in that it checks whether two conditions are True. In the case of of and, it evaluates to True if BOTH "X" and "Y" are True. If either "X" or "Y" are not True, then it evaluates to False.

To better understand how this works, try out the code below, which shows how to use the and operator in Python:

x = 100

print(x <200 and="" x="" 400="" pre="">

The above code example creates a variable called x and assigns it the value of 100. We then check to see if the value of x is both less than 200 AND less than 400. If both of these conditions are True, then the program will print "True" to the user's screen. If either of these conditions is not True, then the program will print "False" to the user's screen.

When we run this program, we get the expected result of:

True

To see what happens if either of the conditions does not evaluate to True, try running the following code in your IDE or code editor:

x = 100

print(x <200 and="" x="" 100="" pre="">

The above code results in the output of "False", because only one of the conditions evaluates to True.



James Payne
 
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date