Booleans with numbers in Python

b_pcakes

I came across this implementation of gcd in Python:

def gcd(x,y): return y and gcd(y, x % y) or x

What I don't understand is how the boolean is working in the return? After trying some numbers in the interpreter I noticed that and always returns the number on the right, while or returns the number on the left. Why is this? Also, can you walk me step by step through a simple call of this function so I can understand what is happening?

Anand S Kumar

This is because of how and and or operators evaluate in Python.

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

They do not return True or False , they return the last evaluated value , and that is why we can write things like -

s = s or "Some default value"

To default the value of s if its None or empty string or empty list, or 0.


Basically, or returns the first non false-like value ( where false-like values are 0, or None or Empty string/list/tuple, etc ) Or the last false-like value if all the values are false-like. Example -

In [1]: 0 or 10
Out[1]: 10

In [2]: 5 or 0 or 10
Out[2]: 5

In [7]: 0 or '' or [] or ()
Out[7]: ()

And, and returns the first false-like value, or the last true-like value , if all the values are true-like. Example -

In [3]: 0 and 10
Out[3]: 0

In [4]: 5 and 10
Out[4]: 10

In [6]: 5 and 0 and 10
Out[6]: 0

In your case, it works as -

  1. if y is 0 it returns x (irrespective of the value of x) .

  2. otherwise it computes gcd(y, x%y) if that is non-zero returns it. (Though it would never really be 0)

  3. if the result of gcd(y, x%y) is 0, then it returns x .

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Only variable expressions returning numbers or booleans are allowed in this context

Counting the number of True Booleans in a Python List

Effect of tilde on booleans — why ~True is -2 & ~False is -1 in Python?

How are booleans formatted in Strings in Python?

python pandas get index boundaries from a series of Booleans

Python - Comparing strings and returning Booleans if they are equal

Overloading Bitwise Operators to Work with Booleans in Python

Python, effect of parentheses on booleans in print statement

How to use python dictionary values as integers, floats or booleans?

How to group blocks of identical booleans in Python

Creating an array of booleans from an array of numbers

Thymeleaf: Only variable expressions returning numbers or booleans are allowed in this context

What causes super small numbers in javascript to be recognized as different booleans?

In Dataweave 2 is there a (simple) way to convert String fields in a payload to Numbers (if numbers) and Booleans (if booleans)

Why is my Python code not evaluating booleans correctly?

Using booleans in Python

(Python) Equally Spacing Trues in a Series of Booleans

Dynamic list of booleans python

How to reverse all booleans in the list in python?

Booleans in If Statements in Python

Python 3.5 - Booleans in expressions

Are booleans overwritten in python?

How to change list of booleans to incremental numbers

Is there a way to pull the index of a Python list of booleans of where short circuiting occurs?

Json.net : Do not derserialize numbers as booleans

How to convert string values into numbers and booleans from an object?

Equation of vectors with booleans in python

Optimization: How to read csv data in python to a dict with converting strings in their booleans?

Are comparisons really allowed to return numbers instead of booleans, and why?