Register to get access to free programming courses with interactive exercises

Logic Python Basics

In addition to arithmetic operations, there are comparison operations in mathematics, such as 5 > 4 or 3 < 1. They also exist in programming. For example, when we visit a website, the username and password are compared with those in the database. If they match, we can authenticate. In this lesson, we will learn more about comparison operations.

Programming languages have adapted all mathematical comparison operations unchanged, except for the equality and inequality operators. The regular equal sign= is used for this in mathematics, but it is less common in programming.

Many languages use the = symbol to assign values to variables. That is why we use == for comparison in Python.

Here is the list of comparison operations:

  • < — less than
  • <= — less than or equal to
  • > — more
  • >= — greater than or equal to
  • == — equals
  • != — is not equal to

These operations are not limited to numbers. For example, you can use the equality operator to compare strings. The password == text comparison works with the string values.

The logical data type

A logical operation like 5 > 4 or password == text is an expression. Its result is a specific value — True or False.

It is a bool — a new data type for us:

result = 5 > 4
print(result)  # => True
print('one' != 'one')  # => False

Along with strings, integers, and rational numbers, the bool is one of the primitive data types in Python.

Let us try to write a simple function that takes the age of children as input and determines whether they are babies or not. We define babies as children under the age of one:

def is_infant(age):
    return age < 1

print(is_infant(3))  # => False

Each operation is an expression, so the only line of the function we write means "return the value that results from the comparison age < 1". Depending on the argument passed, the comparison will be True or False, and return will return that result.

Now run the check on a child who is six months old:

print(is_infant(0.5))  # => True

The result of the operation is True. So the child is a baby.

Predicates

The is_infant() function is a predicate or question function. A predicate answers a "yes or no" question by returning a boolean value. Predicates in any language usually have convenient names to make them easy to parse. Python predicates start with the prefixes is or has:

  • is_infant() — is it an infant?
  • has_children() — do they have children?
  • is_empty() — is it empty?
  • has_errors() — are there any errors?

A function is a predicate function if it returns the boolean values True or False.

Let us write another predicate function. It takes a string and checks if it is the word 'Castle':

def is_castle(string):
    return string == 'Castle'

print(is_castle('Sea'))

Combining operations and functions

Logic operations are expressions, meaning we can combine them with other ones. For example, if we want to check whether a number is odd or even. The approach used in programming is to check the remainder of a division by two:

  • If the remainder is 0 — the number is even
  • If the remainder is not 0 — the number is odd

Division remainders are a simple but essential concept in arithmetic, algebra, number theory, and cryptography. You need to divide the number into several equal groups. If anything remains at the end, it is the remainder of the division.

Here we split some candies equally among individuals:

  • 7 candies, 2 people: 2 x 3 + remainder 1 - 7 not a multiple of 2
  • 21 candies, 3 people: 3 x 7 + remainder 0 - 21 multiples of 3
  • 19 candies, 5 people: 5 x 3 + remainder 4 - 19 not a multiple of 5

The % operator calculates the remainder of a division:

  • 7 % 21
  • 21 % 30
  • 19 % 54

Let us combine the equality check == and the arithmetic operator % into one expression and write a function that checks if a number is odd or even:

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

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

Arithmetic operators have higher priority than logical ones. So, there are three steps. The program:

  • Calculates the arithmetic expression number % 2
  • Compares the result with zero
  • Returns the result of the equality check

Now we will write a function that takes a string and checks if it starts with the letter a.

Here is the algorithm:

  1. Get the initial character of the string and assign it to the variable
  2. Compare whether the symbol is equal to the letter a
  3. Return the result

And here is the code:

def is_first_letter_an_a(string):
    first_letter = string[0]
    return first_letter == 'a'

print(is_first_letter_an_a('orange'))  # => False
print(is_first_letter_an_a('apple'))   # => True

Try to say what is going on in the same way we decoded the process in the is_even() example to understand what is happening here.

You now know that programmers use comparison operations alongside arithmetic operations. But remember that we indicate equality with ==. This way, you will not confuse this operation with assigning a value to a variable.

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