# [Introduction to Python] How to use the Boolean operator (and ・ or ・ not)

There are various operators in Python that are used in all calculations and syntax. Among them, there is a Boolean operator as an operator that is often used mainly in conditional expressions of if statements. Boolean operators, also known as logical operators, are used when writing complex conditional expressions in conditional branching of if statements.

This time, I will explain how to use the Boolean operator.

and is a Boolean operator, also known as AND. The basic syntax of and is as follows.

``````Conditional expression 1 and conditional expression 2
``````

and takes conditional expressions on the left and right, and returns true only if the result of both conditional expressions is True. If either is false, it returns false. By using and, you can write a short if statement for complicated conditions.

For example, suppose you want to retrieve only numbers from 100 to less than 200 from a list of numbers. If and is not used, the if statement will be as follows.

``````list = [30, 256, 125, 167, 45, 401]

for number in list:
if(number >= 100):
if(number < 200):
print(number)
``````

Execution result

125 167

This example has a short list and isn't that complicated, but it still uses the if statement twice. The more complicated the conditions, the more if statements, and the harder it is to read the code. If you use and in such a case, you can write it very short.

``````list = [30, 256, 125, 167, 45, 401]

for number in list:
if(number >= 100 and number < 200):
print(number)
``````

Execution result

125 167

In this case, and returns True only when the number is 100 or more and less than 200, and the contents of the if statement are executed. By using and, the if statement has been reduced by one and the code has been refreshed.

or is a Boolean operator, also known as OR. The basic syntax of or is as follows.

``````Conditional expression 1 or conditional expression 2
``````

and is true for the entire expression only when both of the two conditional expressions are True, while or is true for the entire expression if either of the two conditional expressions is True.

Using the previous example, it is used when displaying numbers less than 100 or more than 200.

``````list = [30, 256, 125, 167, 45, 401]

for number in list:
if(number < 100 or number >= 200):
print(number)
``````

Execution result

30 256 45 401

In this example, if either the number is less than 100 or the number is 200 or more is True, the entire expression is True and the contents of the if statement are executed.

not is a Boolean operator, also known as negation. The basic syntax of not is as follows.

``````not conditional expression
``````

not means that if the conditional expression is True, the entire expression will be False. Using the previous example, it is used when outputting only non-even numbers.

``````for number in list:
if(not number % 2 == 0):
print(number)
``````

Execution result

125 167 45 401

## Combine and, or, not

So far, we have introduced three types of Boolean operators, and, or, and not, but you can also combine these operators to create more complex conditional expressions.

For example, the conditional expression that the number is less than 300 and is not divisible by 3 is as follows.

``````list = [30, 256, 125, 167, 45, 401]

for number in list:
if(number < 300 and not number % 3 == 0):
print(number)
``````

Execution result

256 125 167

## Boolean operator precedence

Operators in the Python language have precedence. For example, if + (sum) and * (product) are present at the same time, * has priority.

``````answer = 2 * 3 + 7
``````

Execution result

13

Boolean operators have similar precedence. Of the Boolean operators, not has the highest priority, followed by and, or. Therefore, when using a combination of Boolean operators, if you do not pay attention to the priority, it may not work as expected.

For example, suppose you want to retrieve "less than 100 or more than 200" and "even".

``````list = [30, 256, 125, 167, 45, 401]

for number in list:
if(number < 100 or number >= 200 and number % 2 == 0):
print(number)
``````

Execution result

30 256 45

The conditional expression seems to be correct at first glance, but for some reason 45, which is not an even number, is also output. This is because and has a higher priority than or, so the conditional expression changes to "a number greater than or equal to 200 and even or less than 100". In order to realize this expression correctly, use () to change the priority.

``````list = [30, 256, 125, 167, 45, 401]

for number in list:
if( (number < 100 or number >= 200) and number % 2 == 0):
print(number)
``````

Execution result

30 256