## Introduction

If you've been following along, you're probably tired of hearing about lists right about now. Well, this guide has nothing to do with lists! Remember back when you first leaned about variables; how there was one that just held `True` or `False` called a Boolean? There hasn't been a guide on them because Booleans are actually that simple. However, there are symbols called Boolean Operators that are used to evaluate whether a statement is true or false. They compare statements and return either true or false. It sounds simple, and in a way it is, but it can get more complex when more comparisons get added.

## And

The first comparison operator is `and`. You can use `and` to test in one statement `and` another statement are both true.
```is_it_true = (3 * 4 > 10) and (5 + 5 >= 10)
print(is_it_true)
```
The code prints out `True` because both `3 * 4 > 10` and `5 + 5 >= 10` are true.

Try one out where one of the statements is true and the other is false.
```is_it_true = (3 * 4 > 10) and (5 + 5 > 10)
print(is_it_true)
```
Because `5 + 5 > 10` is not true, the code prints out `False`. In order for an `and` expression to return `True`, both statements on either side of `and` must be true.

You can have multiple Boolean Operators in one statement.
```is_it_true = (3 * 4 > 10) and (5 + 5 >= 10) and (4 * 4 > 15)
print(is_it_true)
```
It doesn't matter that there are multiple statements. Each statement must be true in order for the whole to evaluate to `True`.

There can be any number of statements. Python is always going to look at what two things are on either side of `and` and return `True` if the are both true or `False` if any or all of them are false.
```is_it_true = (3 * 4 > 10) and (5 + 5 >= 10) and (4 * 4 > 15) and (5 + 4 < 10)
print(is_it_true)
```

## Or

The `or` operator also compares the statements on either side of it. In the case of `or` it evaluates if one statement or the other is true. If either one is, the entire expression will evaluate to `True`. In order for an `or` expression to return `False`, both statements must be false.
```is_it_true = (3 * 4 > 10) or (5 + 5 > 10)
print(is_it_true)
```
The expression is `True` because even though `5 + 5 > 10` is not true, `3 * 4 > 10` is.

Like with `and`, these can also be combined.
```is_it_true = (3 * 4 < 10) or (5 + 5 > 10) or (4 * 4 > 15) or (5 + 4 > 10)
print(is_it_true)
```
Even though only one of the statements is true, the expression as a whole is true. Using `or` only requires that one statement be true for the entire expression to also be true.

## Not

The `not` operator checks if something is not true. If the expression that it is evaluating is not true, `not` will evaluate `True`. That might seem weird, so here's an example.
```is_it_true = not (3 * 4 > 10)
print(is_it_true)
```
Since the statement that `not` is evaluating is true, it returns `False`.

## Combining Them

You can combine different Boolean Operators into the same expression. Using them in conjunction with one another allows you to create finer grained control over the logic in your programming, but it also adds a new degree of complexity.
```is_it_true = (3 * 4 > 10) or (5 + 5 > 10) and (4 * 4 > 15) or (5 + 4 > 10)
print(is_it_true)
```

It evaluated `True` even though there were false statements. Python compared what was on either side of the `or` operators first. Because `or` only requires one of the statements that it's evaluating to be true, each `or` evaluated to `True`. Then, `and` checked if the statements on either side of it were true. The `or` expressions were on either side of the `and`, and they were both true, so the `and` and the expression as a whole are also true.
```is_it_true = (2 * 6 <= 10) and (32 / 8 >= 4) or not (5 ** 2 < 25)
print(is_it_true)
```
Again, this one came back `True`. `(2 * 6 <= 10) and (32 / 8 >= 4)` is false because `2 * 6 <= 10` is false. `5 ** 2 < 25` is false, but `not` evaluates `True` when given a false statement. So, with a false statement on one side of the `or` and a true one on the other, `or` will evaluate `True` along with the entire expression.

## Conclusion

Boolean Operators operate based on logic. That's probably the best thing to keep in mind when dealing with them. Think through exactly how statements are compared with on another in a logical procedure.

You can also think of the operators in very simple terms. `and` means both must be true. `or` means that one must be true. `not` just evaluates to the opposite.

It's good practice to come up with as complex of these expressions as you can and try to figure out how they will ultimately evaluate. It takes some getting used to, but the more that you do it, the more familiar you will become with Boolean Operators and Boolean Logic.

ARE YOU LOOKING FOR A LINUX JOB?
Get extra help by visiting our LINUX FORUM or simply use comments below.

### You may also be interested in:

GDPR permission: I give my consent to be in touch with me via email using the information I have provided in this form for the purpose of news and updates.

### Write For Us

LinuxConfig is looking for a technical writer(s) geared towards GNU/Linux and FLOSS technologies. Your articles will feature various GNU/Linux configuration tutorials and FLOSS technologies used in combination with GNU/Linux operating system.

When writing your articles you will be expected to be able to keep up with a technological advancement regarding the above mentioned technical area of expertise. You will work independently and be able to produce at minimum 2 technical articles a month.

### Contact

web ( at ) linuxconfig ( dot ) org