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

[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.

table of contents 1 [About and](About ## and) 2 [About or](About ## or) 3 [About not](About ## not) 4 [Combine and, or, not](combine ## and, or, not) 5 Boolean operator precedence (## Boolean operator precedence)

About and

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.

About or

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.

About not

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
print(answer)

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

Recommended Posts

[Introduction to Python] How to use the Boolean operator (and ・ or ・ not)
[Introduction to Udemy Python 3 + Application] 36. How to use In and Not
[Introduction to Python] How to use the in operator in a for statement?
Technical English> you use the boolean operators [and, or, and not] to ...> Boolean Operations — and, or, not
[Python] Boolean operator (or / and) does not return Boolean value
[Introduction to Udemy Python3 + Application] 27. How to use the dictionary
[Introduction to Udemy Python3 + Application] 30. How to use the set
[Introduction to Python] How to use class in Python?
How to install and use pandas_datareader [Python]
How to use is and == in Python
How to use the C library in Python
[Introduction to Udemy Python3 + Application] 23. How to use tuples
[Algorithm x Python] How to use the list
[Python] How to use hash function and tuple.
[Python] How to use the enumerate function (extract the index number and element)
[Introduction to Python] How to use while statements (repetitive processing)
python3: How to use bottle (2)
How to use the generator
[Introduction to Python] How to iterate with the range function?
How to use the Raspberry Pi relay module Python
How to use Python argparse
[Python] How to use the graph creation library Altair
How to use the grep command and frequent samples
[Python] How to use checkio
Python bitwise operator and OR
How to use argparse and the difference between optparse
How to use the model learned in Lobe in Python
[Python] How to use input ()
How to use the decorator
[Introduction] How to use open3d
How to use Python lambda
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
How to use Python bytes
How to use the __call__ method in a Python class
[Hyperledger Iroha] Notes on how to use the Python SDK
Comparison of how to use higher-order functions in Python 2 and 3
I didn't know how to use the [python] for statement
Introduction of DataLiner ver.1.3 and how to use Union Append
[Introduction to Python] How to get data with the listdir function
How to use the zip function
How to use the optparse module
[Introduction to Python3 Day 1] Programming and Python
How to install and use Tesseract-OCR
[Python] How to use Pandas Series
How to use Requests (Python Library)
How to use SQLite in Python
[Introduction to Python] How to parse JSON
How to get the Python version
[Python] How to import the library
How to use .bash_profile and .bashrc
How to install and use Graphviz
[Python] How to use list 3 Added
How to use Mysql in python
How to use OpenPose's Python API
How to use ChemSpider in Python
How to use FTP with Python
Python: How to use pydub (playback)
How to use PubChem in Python
[Introduction to Python] Let's use pandas