Boolean short-circuiting and more!

Hey there, 👋

How is your Python going?

In this Mathspp Insider 🐍🚀 email we’ll talk about Booleans! I hope you like it!

This email at a glance

  • the best thing you can do for your Python skills this December is join the Python Problem-Solving Bootcamp;

  • all Python values can be interpreted as Booleans when used in conditions and inside the built-in bool;

  • falsy values include 0, 0.0, None, "", and empty containers;

  • everything else is Truthy;

  • the Boolean operators and and or short-circuit, that is, they only evaluate the right-hand side if needed;

  • Boolean short-circuiting can be used with the truthy and falsy values of objects in interesting idioms;

Learn Python skills through deliberate practice

The best thing you can do for your Python skills is practice.

In December, I’m giving you the chance to practice on 42 challenges over the course of 21 days.

Whenever you get stuck, you can make use of the private community of all other bootcamp participants.

And by the end of the day, I’ll send you a detailed analysis of the challenge and of multiple solutions to that problem.

The analysis will teach you new ways of using Python and it will teach you new modules, new tools, and new Python idioms.

You can join the bootcamp by following this link:

Here is some feedback from past participants:

If you want to be like them and become a better, more efficient, more fluent Python programmer, just click here 😉.

Truthy and Falsy

In Python, all objects can be interpreted as True or False when needed.

For example, if you write if some_value:, then some_value doesn’t need to be a Boolean for the conditional to work.

You can check if a value is truthy or falsy by using the built-in bool:

>>> bool(None)
False
>>> bool(3)
True
>>> bool("Rodrigo")
True
>>> bool("")
False

Almost everything is truthy.

Except what’s not!

The most common falsy values include 0, 0.0, None, "", and empty containers.

(Instances of your own classes will all be truthy be default unless you customise that behaviour.)

You’ll also see this in action in many different challenges of the problem-solving bootcamp that I’m organising soon.

Boolean short-circuiting

Another great feature that is related to Booleans is called short-circuiting.

(This feature isn’t unique to Python!)

Boolean short-circuiting means that the operators and and or will only evaluate their right-hand side if they really need to.

Look at the code below.

Notice how the first expression produced two prints while the second one only produced one print:

>>> def t():
...     print("inside t")
...     return True
...
>>> def f():
...     print("inside f")
...     return False
...
>>> t() and f()
inside t
inside f
False
>>> t() or f()
inside t
True

Why did the second expression t() or f() only print a single message?

When the operator or evaluates the left-hand side, it finds t() which evaluates to True.

So, the expression t() or f() simplifies to True or f().

Now, look at this expression: True or _.

Can you pick a value for _ such that True or _ gives False?

You can’t!

And that’s why Python didn’t even bother running the f() in t() or f()!

As soon as it found a True on the left of or, Python knew there was no point in looking at the right-hand side.

This is Boolean short-circuiting and it can be used for a couple of common idioms in programming.

I’ll teach you at least two of those idioms in the problem-solving bootcamp I’m organising.

Boolean short-circuiting for default values

If you put the two concepts above together, you get one common Python idiom that uses Boolean short-circuiting to assign default values.

Here is an example motivated from this newsletter!

I like to greet users by their name, but not everyone shares their name.

So, I have some logic that is similar to the one shown below:

>>> def greet_reader(name=None):
...     greeting = name or "there"
...     return f"Hey, {greeting}!"
...
>>> greet_reader("Rodrigo")
'Hey, Rodrigo!'
>>> greet_reader()
'Hey, there!'

Notice how we get a generic message if I call greet_reader without any arguments but I get a personalised message if I call greet_reader with the reader name.

The magic happens in the assignment greeting = name or "there", which uses Boolean short-circuiting together with the truthy and falsy value of name to compute the greeting.

If this is cool, let me tell you this is just one of the many things you’ll learn in my upcoming problem-solving bootcamp.

ICYMI

🐍🚀 How was this email?

Login or Subscribe to participate in polls.