GuidesWorking with Python Logical Operators

# Working with Python Logical Operators

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

This Python tutorial delves into the basics of logical programming and is part of a series that will cover logical operators, decision making, and Python conditionals. When combined, these Python principles will help you create powerful applications that act – and react – based upon input received from a user or when certain conditions are met.

Before we get to that point, however, we must first learn about a handy set of operators known as logical operators.

## What are Logical Operators in Python?

Logical operators exist in most major programming languages. They are often called logical operators, but can also be known as Boolean operators because they return a Boolean result. Boolean values include Yes or No in theory, but in reality, are True and False. They are used in logical operations to compare or perform evaluations on the values held in variables and other data types.

Read: Python Math Operators: Complete Guide

## How to Use Python Logical Operators

Below, we will discuss how to work with the three main types of logical operators. These include the not, or, and the and operators.

### Python not Operator

The Python not Operator is a tricky operator to understand at first glance, primarily because the logic behind it seems flawed. The not operator returns a BooleanTrue or False – no matter what value you check against it. The way it works is that the not operator checks if the input “X” contains a value of True, then it will return False. On the flip side, if the input “X” contains a value of False, then True is returned.

Here is some sample code showing how to use the not operator in Python:

```
age = 1000
print(not age < 2000)

```

In this example, we start off by creating a variable named age and assign it the value of 1,000. We then use the less than comparison operator to test if the statement age is less than 2000 is True or False. Since age is only equal to 1000, and therefore is indeed less than 2,000, the comparison would result in True. This is where the tricky part comes into play.

Remember, the not operator will return a False if an evaluation is True and a True if an evaluation is False. So, in our example, the comparison of age being less than the value 2,000 is True, so the output of our program would be:

```False
```

If we flip or comparison operator and make it a greater than comparison, the evaluation of is age greater than 2000 would be False, since 1000 is not greater than 2000. Again, however, we are using the not operator, so since our evaluation returned a False, then the output of the following code would result in a True. Here is the code. Run it in your code editor or IDE and see the result:

```age = 1000
print(not age > 2000)
```

The output:

```True
```

Note that, when we work with strings versus integers, the not logical operator checks to see if a string is empty or null. If so, a True is returned. If the string contains any value at all, however, a False is returned.

### Python or Operator

The Python or operator is used to test whether either of two or more conditions evaluate to True. If neither of the conditions are True, then the condition is considered False. Another way to look at the or logical operator is that it evaluates to True if either “A” or “B” is True. If neither “A” nor “B” are True, then it returns a “False”.

To better understand this, the following code sample shows how to use the or operator in Python:

```age = 1000

print(age > 2000 or age < 2000)
```

In the above code snippet, we assign the variable age the value of 1000. Then place an evaluation in our print() function that checks to see if either of these two conditions are met: is age greater than 2000 OR is age less than 2000.

Even though the first part of our condition is not met – age is not greater than 2000 – the second condition is met, because age is, in fact, less than 2000. So, since one of the conditions is met, the output of this program would be:

```True
```

### The and Logical Operator in Python

Python’s logical and operator functions in a similar manner to the or operator. It also compares whether two or more values are True. The difference is that, with an and operator, both or all conditions must be met for a True to be returned.

Another way to look at it is to say that with an and both “A” and “B” must be True, otherwise a “False” will be returned.

Here is an example of how to use the and operator in Python:

```
# Create three variables to compare
a = 10
b = 20
c = 30

# An IF statement to compare variables

if a < c and b < c:
print("Both 'a' AND 'b' are less than 'c'")
else:
print("Either 'a' or 'b' or BOTH 'a' and 'b' are greater than 'c'")

```

In this code example we create three variables, a, b, and c, respectively. We assign each variable a value and then make an IF/Else block where we check to see if both a and b are less than c. If both of those conditions are met, then the text, “Both ‘a’ AND ‘b’ are less than ‘c’ “ will print to the users screen. However, if either condition – or both conditions – are not True, then the program skips to the else clause and the text, “Either ‘a’ or ‘b’ or BOTH ‘a’ and ‘b’ are greater than ‘c’ “ will appear on the output.

In this case, since a and b are, in fact, both less than c, the result of the program is:

```
Both 'a' AND 'b' are less than 'c'

```

## Other Python Tutorials

Looking for other Python tutorials? In addition to our coverage of logic operators here and our article on Python number operators highlighted above, we also recommend readers interested in Python check out these tutorials and guides:

Subscribe to Developer Insider for top news, trends & analysis