[Python] return A [or / and] B

return A or B ? The other day, I was confused when I saw this code.

python


def hoge:(self)
    return A or B

I've never seen or or and in the return syntax, so I'll look it up.

What?

Quote from official documentation

The expression x and y first evaluates x; if x is false, its value is returned; otherwise, y is evaluated and the resulting value is returned. The expression x or y first evaluates x; if x is true, its value is returned; otherwise, y is evaluated and the resulting value is returned.

I don't know unless I write it.

or_and_in_return.py


def and_in_return1():
    return 1 == 1 and 1 == 1

def and_in_return2():
    return 1 == 0 and 1 == 1

def and_in_return3():
    return 1 == 1 and 1 == 0

print(and_in_return1()) # True
print(and_in_return2()) # False
print(and_in_return3()) # False

def or_in_return1():
    return 1 == 1 or 1 == 1

def or_in_return2():
    return 1 == 0 or 1 == 0

def or_in_return3():
    return 1 == 1 or 1 == 0

print(or_in_return1()) # True
print(or_in_return2()) # False
print(or_in_return3()) # True

It was the same as the action used in if ...

The page that I used as a reference

Recommended Posts

[Python] return A [or / and] B
Python a + = b and a = a + b are different
Connect a lot of Python or and and
Python3> round (a --b, 7)
python memo-"if not A and B" was "if (not A) and B"
Python bitwise operator and OR
Python or and and operator trap
ABC127 A, B, C Explanation (python)
It would be wise to write like boolean and "A" or "B" [Python] [But]
Create code that outputs "A and pretending B" in python
ABC128 A, B, C commentary (python)
ABC126 A, B, C Explanation (python)
[Python3] "A // B" and "math.floor (A / B)" are not always the same! ??
I made a simple circuit with Python (AND, OR, NOR, etc.)
Specify or create a python folder and then save the screenshot.
Solve ABC175 A, B, C in Python
[Python] Use and and or when creating variables
A memo with Python2.7 and Python3 on CentOS
python> keyword arguments> hoge (** {'a': 1,'b': 2,'c': 3})
[Python] [LINE Bot] Create a parrot return LINE Bot
Solve ABC165 A, B, D in Python
A story about Python pop and append
Python early return
Organize python modules and packages in a mess
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
[Python] Which should be used, return or return None
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
C language to see and remember Part 3 Call C language from Python (argument) c = a + b
[python] Compress and decompress
[Python for Hikari-] Chapter 06-04 Functions (arguments and return value 3)
A * algorithm (Python edition)
Python and numpy tips
[Python] pip and wheel
[Python] Take a screenshot
Python: Creating a virtual environment (venv), starting and stopping
[Python of Hikari-] Chapter 06-02 Function (argument and return value 1)
Try creating a compressed file using Python and zlib
Create a Python module
Batch design and python
Python iterators and generators
Building a Docker working environment for R and Python
A python lambda expression ...
Python from or import
python autotest or sniffer
Python packages and modules
Vue-Cli and Python integration
Ruby, Python and map
Let's make a simple game with Python 3 and iPhone
Visualize B League goals and goals on a heat map
Daemonize a Python process
A quick comparison of Python and node.js test libraries