🐍🚀 What else does else do?

Hey there, 👋

How is your Python going?

In this Mathspp Insider 🐍🚀 email we’ll talk about else statements in for loops and I’ll also share some exciting news I have!

An else statement in a for loop?

Everyone knows the else statement from conditional blocks.

You write if some_condition: if you want to run some code only in case some_condition is true and then the else: block is used if you want to run code in case it wasn’t.

But the else is much more than that.

Apparently, you can also use else after a for loop!

Let me tell you what it does…

But first, some news:

The Python Problem-Solving Bootcamp is back!

The Python Problem-Solving Bootcamp is a 21-day intensive bootcamp where we get together to solve 42 programming challenges in Python.

This will be the second edition of this bootcamp, after the first one was so successful!

(21 ⭐️⭐️⭐️⭐️⭐️ reviews and one ⭐️⭐️⭐️⭐️ review!)

The bootcamp will run from the 1st of December up to the 21st.

The bootcamp is fully asynchronous and we’ll have a Discord channel where we’ll discuss ideas and algorithms.

We’ll also have a place to ask for help when we get stuck with a particular problem.

You can read more about the bootcamp here.

You can also reply to this email with questions you might have.

I’ll see you inside 😉

What else does else do?

(Sorry for the bad pun, I couldn’t resist it!)

If you have an else statement in a for loop, the else statement runs only if the loop terminates naturally.

If you use a break inside the loop, the else statement does not run.

This can be illustrated with these two excerpts from the REPL:

# No break = we dive into the else
>>> for i in range(10):
...     pass
... else:
...     print("Else!")
# Break = we skip the else
>>> for i in range(10):
...     if i == 5:
...         print("Break!")
...         break
... else:
...     print("Else!")

This may look weird, and I thought that, too…

So, let that sink in for a second.

You can also take a look at the diagram below; it summarises what I said so far:

Example usage of else in a for loop

The diagram above summarises really well the point of the else in a for loop.

It really only makes sense in loops that contain a break.

Picture this:

You are creating an application and there are a couple of places where the user may save their configuration files.

You use the loop to look for the configuration file.

If you find the file, you can open it and parse the configuration.

Then, you break the loop.

However, if you cannot find any configuration file, you never hit the break, and you enter the else statement.

This else statement can be used to

  • raise an error; or

  • create a default configuration; or

  • prompt the user to create their configuration at that point in time; or

  • whatever you want!

So, this is an example of where the else could make sense.

Did this example make sense to you?

If not, reply to this email and I’ll help you out!

Now, here’s a small challenge for you:

Try to think of a situation where the else might be useful.

It can be a situation where you used it in the past or a situation where you think it could be useful.

Reply to this email and share your example with me!

More challenges

In case you want to practise solving some programming challenges in Python, check out the Python Problem-Solving Bootcamp.

The next cohort starts on the 1st of December and we’ll be solving 42 challenges in 21 days.

It’s going to be A LOT of fun.


In Case You Missed It, I’ve been hard at work these past few days!

  • I published a Python package called error-links. You install it with pip install error-links and then you run the command error-links install.

    What does it do?

    It shows Google and Python docs links whenever your code breaks with an exception.

    This is the GitHub repo: https://github.com/rodrigogiraoserrao/error-links/

  • I’ve also published the first few articles of my series “Building a Python Compiler and Interpreter” on my blog. You can read them here: https://mathspp.com/blog/tag:bpci

🐍🚀 How was this email?

Login or Subscribe to participate in polls.