python memo-"if not A and B" was "if (not A) and B"

Introduction

I was quietly addicted to the if not syntax, so I'll leave a note.

What is the not operator?

Reference: https://www.sejuku.net/blog/65070

The not operator in the if statement returns true if the conditional expression is false. This is just an example sentence because I don't understand a little.

Example sentence


x = 10
if not x == 20:
    print(True)
else:
    print(False)

Execution result


True

Actual usage example


if not len(sys.argv[1:]):
    usage()

Looking at this script, "If the argument is not 1 or more (= no argument), use usage () is called." Well, I don't know this.

Case I didn't understand

I was addicted to the following sentence. A combination of and and not. I'm not sure ... if (not listen) and len(target) and port > 0: Is it? if not (listen and len(target) and port > 0): I was worried because I didn't know what it was.

Example sentence


if not listen and len(target) and port > 0:
    buffer = sys.stdin.read()
    client_sender(buffer)           

How to read when including the and / or operator

In conclusion, I agree with this script. not only hangs on listen.

Example sentence


if (not listen) and len(target) and port > 0:
    buffer = sys.stdin.read()
    client_sender(buffer)           

I don't know if this is all, so I wrote some example sentences and confirmed it.

x = 20
y = 20

#Case 1: The result is "No"
if not x == 10 and y == 20
    print("Yes")
else:
    print("No")

#Case 2: When the evaluation order of the conditional expression is clarified by adding parentheses, the result is "No" as in Case 1.
if (not x == 10) and y == 20
    print("Yes")
else:
    print("No")

#Case 3: not==not!=I tried it. The result is "No" as in Case 1.
if x != 10 and y == 20
    print("Yes")
else:
    print("No")

in conclusion

It's pretty embarrassing to leave such an article on Qiita I posted it because I wanted to remember what I was addicted to at that time. In my case, I'm addicted to something like this over and over again.

Recommended Posts

python memo-"if not A and B" was "if (not A) and B"
Python a + = b and a = a + b are different
[Python] return A [or / and] B
[Python3] "A // B" and "math.floor (A / B)" are not always the same! ??
Python3> round (a --b, 7)
python> check NoneType or not> if a == None:> if a is None:
Python list is not a list
Consider If Programming Was An Anime in Python and C
Create code that outputs "A and pretending B" in python
ABC127 A, B, C Explanation (python)
a () and a.__ call__ () are not equivalent
ABC128 A, B, C commentary (python)
ABC126 A, B, C Explanation (python)
I thought a Python class variable was an instance variable and died
Solve ABC175 A, B, C in Python
A memo with Python2.7 and Python3 on CentOS
Connect a lot of Python or and and
Python / dictionary> setdefault ()> Add if not in dictionary
[Django] A memo when log was not
Solve ABC165 A, B, D in Python
A story about Python pop and append
What happens if you do "import A, B as C" in Python?
C language to see and remember Part 3 Call C language from Python (argument) c = a + b
Building a python environment with virtualenv and direnv
A nice nimporter that connects nim and python
ffmpeg-Build a python environment and split the video
I wrote a class in Python3 and Java
Why I'm a Java shop and start Python
Create a web map using Python and GDAL
Launch a web server with Python and Flask
Let's write a Python program and run it
Create a Mac app using py2app and Python3! !!
A story about modifying Python and adding functions
[Python] A rough understanding of iterators, iterators, and generators
A discussion of the strengths and weaknesses of Python
python Boolean operation does not return a Boolean value
Python if statement
It would be wise to write like boolean and "A" or "B" [Python] [But]
[Python] if statement
Check if the string is a number in python
A story that Seaborn was easy, convenient and impressed
[Python] Boolean operator (or / and) does not return Boolean value
Try creating a compressed file using Python and zlib
Building a Docker working environment for R and Python
Build a python virtual environment with virtualenv and virtualenvwrapper
Why not create a stylish table easily with Python?
Let's make a simple game with Python 3 and iPhone
Visualize B League goals and goals on a heat map
A quick comparison of Python and node.js test libraries
Implementing a generator using Python> link> yield and next ()> yield
I made a LINE BOT with Python and Heroku
It's not easy to write Python, it's easy to write numpy and scipy
If you encounter a "Unicode Decode Error" in Python
Building and enabling a python virtual environment, etc. (venv)
I wondered if Python 3.4 was faster, but it was slower
A Java programmer studied Python. (for, if, while statement)
Solve Atcoder ABC176 (A, B, C, E) in Python
Create a decent shell and python environment on Windows
A story that was terrible if SELinux was properly disabled
Python: I tried a liar and an honest tribe
[Python] Start a batch file from Python and pass variables.