Level Five: Boolean Logic in Python

Boolean logic in Python, as with other programming languages, is crucial for enabling our code to compare data.

For instance, it allows us to compare a user-entered password with one stored in a database, or to assess video game players’ ranks to ensure they’re matched with similarly skilled opponents.

There are countless examples of where comparisons like these are beneficial, making boolean logic an incredibly valuable tool in programming.

Let’s dive into Boolean Logic and learn how to code it!

Comparison Operators

When we compare numbers, we typically use symbols like > (greater than), < (less than), = (equal), and others. These symbols also work in Python, but they look a bit different:

  • Greater than: >
  • Less than: <
  • Equal: ==
  • Not equal: !=
  • Greater than or equal: >=
  • Less than or equal: <=

You might have noticed that, in Python, instead of using a single equals sign (=) for equality, we use a double equals sign (==).

The reason we need a double equal sign is so Python doesn’t confuse it with an attempt to assign a value to a variable.

In Python, these symbols are called comparison operators. They’re essential for allowing our program to compare different types of data, such as numbers, strings, variables, and more.

Boolean Values: True and False

Boolean logic has two logical values: True and False.

True – when the condition is true. And False – when the condition is false.

Now let’s play a game.

I will compare numbers, and you will answer in your mind – True, if it is true, or False, if it is false.

Shall we?

2 > 4
10 < 20
3 == 3
5 != 7

Now let’s do the same excercise in Python. To do this, we will create a variable – result, and assign it the value 2 > 4.

Then we will print the result to the screen using the print function. This is how it will look:

result = 2 > 4

Enter this code into the compiler and run it.

#write code here

If the value of the variable is True, the program will return True. If the value is False, the program will return False.

So, what does the program return?

If that makes sense so far, substitute the rest of the values from the list, one-by-one, run the code and observe the result that the program returns. This is how Boolean logic works in Python.

But what if we try to compare strings? Try comparing apples to oranges using the following value in the variable:

result = "apples" == "oranges"

Write this code into the compiler and run it.

#write code here

Now let’s compare apples to apples:

result = "apples" == "apples"

Do you see the result?

Now you know that Boolean logic works not only with numbers, but also with other data formats.

True and False in Variables

As you have already noticed, we can store the results of comparisons that return True or False in variables.

Let’s look at it in more detail with the following example:

result = "apples" == "bananas"

Enter this code into the compiler and run it.

#write code here

In this code, we created a variable with the name “result”, and assigned it a Boolean value from the comparison of the two strings “apples” and “bananas”.

Then we went down one line, and printed the value of the variable “result” to the screen, passing the variable name in parentheses to the print function. Which we created before.

Pretty easy, eh?

Now change the code to indicate that apples and bananas are not equal. You already know how to do this.

Done? What does the program return now?

Comparison of Variables in Python

We can compare not only numbers and strings, but also entire variables!

Let us look at this example:

game = "Dota"
result = game == "FIFA"

But, before you write this example into the compiler and run it, think about what result will it return? True or False?

Have you decided? Now let’s check if you were right 😉

As you can see, first we created a variable called game and assigned it the string value “Dota”.

Then we went down one line and created a second variable named result.

After that, we assigned the variable result the Boolean value from the comparison of our first variable – game, with the string value FIFA.

Then we went down one more line and printed the value of the variable result to the screen, using the print function.

And since the value of our variable game is equal to the string Dota, and not the string FIFA, the program returned False.

So, did your solution match the program’s answer?

Now let’s reinforce the example we just covered. Take our code and write your own version of the program to create and compare two variables based on it.

#write code here

Change anything you want: variable names, their values. And of course comparison operators.

Password-checking Program

Finally, let’s get to work on a very simplified real-life example: A program that checks the correctness of the entered password:

password = "swordfish"
greeting = password == "swordfish"

Enter this code into the compiler and run it.

#write code here

Let’s break it down:

On the first line, we created a variable – password, and assigned it the value – swordfish.

Then on the second line, we created a new variable named greeting. And we assigned it the Boolean value that compares the value of the password variable and the string “swordfish”.

And finally, on the third line, we printed the result of the logical comparison from the second line to the screen.

Since we used the == (Equal) as the comparison operator, and the value of the password variable is indeed equal to the string “swordfish”, then the greeting variable returned True.

Thus, we can imagine that:

  • “swordfish” from the first line is the password stored in the password database.
  • “swordfish” from the second line is the password that the user enters to log in to their personal account.
  • greeting = password == from the second line is the code that compares the user’s password with the password from the database.
  • And print(greeting) from the third line is just a function that outputs the result of the comparison to the screen. True, if the passwords match, or False, if the passwords do not match.

Now change “swordfish” from the first or second line to any other word or phrase, and run the program. The program should return False, since the passwords no longer match.

This example is a simplified representation of how programmers use Boolean logic and comparison operators in Python for tasks related to user authorization in applications.

Level Five Quiz

You have to pass the quiz to complete Level Five!

Level Five Quiz

1 / 3

Boolean logic has two values. What are they? (Multiple choice)

2 / 3

What will this code return: result = "oranges" != "apples"

3 / 3

What will this code return: result > "oranges" != "apples"

Your score is

The average score is 100%


Wrapping Up Level Five

In the Level Five, you accomplished the following:

  • Learned what Boolean logic is in Python;
  • Learned how to apply comparison operators;
  • Got acquainted with the Boolean values True and False;
  • Learned how to use Boolean values in variables;
  • Learned how programmers use Boolean logic for user authorization.

Congratulations! You are making great progress! Now lets apply Boolean logic in more complex examples.

We will do this in the next lesson – Conditional Statements in Python.

Python Online is an online compiler where you can write, run, and check your code without installing any software.