Register to get access to free programming courses with interactive exercises

Logical operators Python Basics

We already know how to write functions that check individual conditions. We will learn how to build compound conditions n this lesson.

Suppose a site needs a password to be longer than eight characters and shorter than twenty characters when registering. You see 8 < x < 20 in mathematics, but you cannot do that in many programming languages.

Let us try to write two separate logical expressions and connect them with the operator AND:

The password is longer than 8 characters AND the password is shorter than 20 characters

Here is a function that takes the password and tells you whether it matches the conditions (True or False):

def is_correct_password(password):
    length = len(password)
    return length > 8 and length < 20

print(is_correct_password('qwerty'))                   # => False
print(is_correct_password('qwerty1234'))               # => True
print(is_correct_password('zxcvbnmasdfghjkqwertyui'))  # => False

In mathematics, this is called a conjunction. It has operands — compound expressions.

The whole expression is true if each operand is true. In other words, AND means both.

The priority of this operator is lower than that of comparison operators. So, the expression length > 8 AND length < 20 works correctly without parentheses.

In addition to AND, we also have OR. It is a disjunction, meaning one or the other or both. The a or b expression is true if one or both operands are true. Otherwise, it is false.

We can combine operators in any number and any order. You can use parentheses to specify the priority when and and or occur in the code.

Here is an example of an advanced function that determines if a password is correct:

def has_special_chars(str):
    # We check whether the string has special characters or not


def is_strong_password(password):
    length = len(password)
    # Brackets set priority, so it is clear what refers to what
    return (length > 8 and length < 20) and has_special_chars(password)

Suppose we want to buy an apartment that meets these conditions: at least 100 square meters on any street OR at least 80 square meters but on Main Street.

We will write a function to check an apartment. It takes two arguments:

  • The area as a number
  • The street name as a string

Here is the code:

def is_good_apartment(area, street):
    return area >= 100 or (area >= 80 and street == 'Main Street')

print(is_good_apartment(91, 'Queens Street'))  # => False
print(is_good_apartment(78, 'Queens Street'))  # => False
print(is_good_apartment(70, 'Main Street'))    # => False

print(is_good_apartment(120, 'Queens Street'))  # => True
print(is_good_apartment(120, 'Main Street'))    # => True
print(is_good_apartment(80, 'Main Street'))     # => True

The area of mathematics that deals with logical operators is Boolean algebra.

Below, you will see truth tables. You can use them to determine what result you get if you apply the operator.

The AND operator

The operator AND on the example of daddy

Here is the truth table:

A B A and B
True True True
True False False
False True False
False False False

The OR operator

OR operator on the example of daddy

Here is the truth table:

A B A or B
True True True
True False True
False True True
False False False

The negation

Along with the logical operators AND and OR, there is also an operation called negation. It changes the logical meaning to the opposite. In programming, negation corresponds to the unary operator not:

not True   # False
not False  # True

For example, if there is a function that checks if a number is even, then you can use negation to check if a number is odd:

def is_even(number):
    return number % 2 == 0

print(is_even(10))      # => True
print(not is_even(10))  # => False

In the example above, we added not to the left of the function call and got the opposite action.

Negation is a tool with which you can express intended rules in code without writing new functions.

If you write not not is_even(10), the code will still work:

print(not not is_even(10))  # => True

In logic, double negation means positive:

not not True   # True
not not False  # False

print(not not is_even(10))  # => True
print(not not is_even(11))  # => False

Now you know how to work with AND, OR, and NOT operators. They allow you to specify compound conditions with two or more logical expressions.


Recommended materials

  1. Boolean algebra
  2. Conjunction
  3. Disjunction
  4. De Morgan's Laws

Sign up

Programming courses for beginners and experienced developers. Start training for free

  • 130 courses, 2000+ hours of theory
  • 1000 practical tasks in a browser
  • 360 000 students
By sending this form, you agree to our Personal Policy and Service Conditions

Our graduates work in companies:

Bookmate
Health Samurai
Dualboot
ABBYY