Free Python course. Sign Up for tracking progress →

Python: Logical operators

We already know how to write functions that check single conditions. And in this lesson we will learn how to build compound conditions.

Suppose that the registration site requires that the password be longer than eight characters and contain at least one capital letter. Let's try to write two separate logical expressions and connect them with the special operator "AND":

Password is longer than 8 characters And password contains at least one capital letter

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

def has_capital_letter(str):
    # Checks the content of a capital letter in the string

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

print(is_correct_password('Qwerty'))                   # => False
print(is_correct_password('Qwerty1234'))               # => True
print(is_correct_password('qwerty1234'))               # => False

and means "AND". In mathematical logic this is called a conjunction. The whole expression is true if every operand - each of the compound expressions - is true. In other words, and means both. The priority of this operator is lower than that of comparison operators. Therefore, the expression has_capital_letter(password) and length > 8 works correctly without brackets.

In addition to and, the operator "OR" (disjunction) - is often used. It means "either or both". The expression a or b is true if at least one of the operands or all of them simultaneously are true. Otherwise, the expression is false.

Operators can be combined in any number and in any sequence. If and and or occur simultaneously in the code, the priority is given by parentheses. Below is an example of an extended function that determines the correctness of a password:

def has_capital_letter(str):
    # Checks the content of a capital letter in the string

def has_special_chars(str):
    # Checks the content of special characters in the string

def is_strong_password(password):
    length = len(password)
    # The brackets set the priority. It is clear what relates to what.
    return (length > 8 and as_capital_letter(password)) and has_special_chars(password)

Now let's imagine that we want to buy an apartment that meets these conditions: an area of 100 square meters or more on any street OR an area of 80 square meters or more, but on Main Street.

Let's write a function that will check the apartment. It takes two arguments: the area is a number and the street name is a string:

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 in which logical operators are studied is called Boolean algebra. Below you will see true tables - you can use them to determine what the result will be if you apply the operator:

and

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

or

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

Instructions

Implement a method is_leap_year() that determines whether a year is a leap year or not. A year will be a leap year if it is a multiple of 400 (i.e. divisible without a remainder), or if it is both a multiple of 4 and not a multiple of 100. As you can see, the definition already contains all the necessary logic, all we need to do is to translate it into code:

is_leap_year(2018) # false
is_leap_year(2017) # false
is_leap_year(2016) # true

The multiplicity can be checked as follows:

# % - returns the remainder of the left operand divided by the right operand
# Check that the number is a multiple of 10
number % 10 == 0

# Check that the number is not a multiple of 10
number % 10 != 0
The exercise doesn't pass checking. What to do? 😶

If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:

  • Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
In my environment the code works, but not here 🤨

Tests are designed so that they test the solution in different ways and against different data. Often the solution works with one kind of input data but doesn't work with others. Check the «Tests» tab to figure this out, you can find hints at the error output.

My code is different from the teacher's one 🤔

It's fine. 🙆 One task in programming can be solved in many different ways. If your code passed all tests, it complies with the task conditions.

In some rare cases, the solution may be adjusted to the tests, but this can be seen immediately.

I've read the lessons but nothing is clear 🙄

It's hard to make educational materials that will suit everyone. We do our best but there is always something to improve. If you see a material that is not clear to you, describe the problem in “Discussions”. It will be great if you'll write unclear points in the question form. Usually, we need a few days for corrections.

By the way, you can participate in courses improvement. There is a link below to the lessons course code which you can edit right in your browser.

Tips

Definitions

  • Logical operators. the AND (and), OR (or) operators, which allow you to create compound logical conditions.