Rodrigo πŸπŸš€ Profile picture
Take your Python 🐍 skills to the next level πŸš€!

Nov 1, 2021, 14 tweets

Here is a Python 🐍 function that tells you if the input number is even or odd...

But how does it work? πŸ‘‡

It looks a bit weird, doesn't it?

Someone DM'd me this function from a @realpython article, and now this ⚑🧡 will break it down for you.

Here we go πŸš€

If you have heard of Boolean short-circuiting, that's the short answer:

Boolean short-circuiting is what's responsible for the behaviour of `f`.

Let me explain...

First, we need to understand the precedence of the `and` and `or`.

Let me add parentheses πŸ‘‡

`and` has higher precedence than `or`.

In other words, `and` β€œbinds tighter” to its operands than `or`.

Now, usually we use `and` and `or` with Boolean operands, to return a Boolean value.

However, `and`/`or` work with arbitrary objects as operands.

We call it,

the Truthy and Falsy value of objects in Python.

This means any object can be used in the place of a Boolean.

In order to figure out the Boolean value that corresponds to an object, we can use the `bool` built-in πŸ‘‡

Interesting how 1 maps to True, 0 to False, but both strings map to True.

In fact, ALL strings map to True.
Well, all of them except the empty string.
The empty string maps to False.

So, what does this have to do with the way `f` works?

Remember when I mentioned Boolean short-circuiting?

This means that Python only looks at the right operand of a Boolean operator IF it needs it.

For example, if the left-hand side of an `and` is False, Python does not look at the right operand πŸ‘‡

Why doesn't Python thrown an error?

Because `False and x` is going to be `False`, regardless of whether `x` is `True` or `False`....

So, Python doesn't even bother evaluating `x`!

A similar thing can happen with `or`, if the left operand is `True` πŸ‘‡

Another really important thing that you need to know about this is that...

`and` and `or` β€œreturn” the actual value of the last operand it evaluated.

In other words, `and`/`or` don't necessarily return `True`/`False` πŸ‘‡

This is the gist of Boolean short-circuiting.

I have a whole article on it, if you want more examples and a more thorough explanation πŸ‘‡

mathspp.com/blog/pydonts/b…

So, now that we know how Boolean short-circuiting works, we can explain how `f` works.

Let's break the explanation down into two cases:

πŸ‘‰ x % 2 == 1
πŸ‘‰ x % 2 == 0

πŸ‘‰ x % 2 == 1

If x % 2 is 1, then the function body simplifies to

(1 and "odd") or "even"

πŸ‘‡

"odd" or "even"

At this point, because of short-circuiting, `or` sees a Truthy value on the left, and simply returns `"odd"`.

πŸ‘‰ x % 2 == 0

If x % 2 is 0, then the function body simplifies to

(0 and "odd") or "even"

πŸ‘‡

0 or "even"

At this point, the left of `or` is Falsy, so it evaluates the right operand, which is `"even"`, and returns it!

That's it, that's how the function `f` works to determine if numbers are odd or even!

Did you learn from this 🧡?

This is what I love to do: break down Python 🐍 and explain it to you in a simple manner!

Follow @mathsppblog for more content like this πŸ˜‰

See you around πŸ‘‹

TL;DR:

πŸ‘‰ `and` has higher precedence than `or`;
πŸ‘‰ Boolean short-circuiting means Python will only evaluate the operands it needs;
πŸ‘‰ we also use the truthy/falsy values of 1, 0, "odd", and "even".

Found a weird piece of Python code?
DM it to me! I'll explain it in a ⚑🧡

βš“

Share this Scrolly Tale with your friends.

A Scrolly Tale is a new way to read Twitter threads with a more visually immersive experience.
Discover more beautiful Scrolly Tales like this.

Keep scrolling