Better Programming

Advice for programmers.

Follow publication

Member-only story

3 Chaotic Python Features To Avoid

Yong Cui
Better Programming
Published in
5 min readJul 19, 2021

Advanced Rubik’s cube
Photo by Imran Bangash on Unsplash.

In the last couple of decades, Python has become one of the most popular programming languages for general purposes. Thus, it has been adopted by people in different domains, such as finance, science, and engineering. One of the reasons for Python’s popularity is its versatility — there are often different approaches to achieve the same functionalities. However, some approaches can be confusing to others. In those cases, you should consider alternatives.

In this article, I’ll discuss a few Python features that can be confusing — particularly to beginners. As a comparison, I’ll provide alternatives that implement the same functionalities. As you’ll see, alternatives may require more code, but they should be preferred because of their clarity. After all, readability is the biggest goal that you should strive to achieve for your coding project.

The else Clause in for/while Loops

Most programming languages share similar features regarding the for and while loops. They have the following general form:

for item in list_or_array:
do something with the item
while some_condition_evaluated:
do something

They are intuitive to all of us, regardless of our programming levels. However, Python allows the else clause to be used with for and while loops. Observe these features below:

We see the else clause most frequently in an if…else… statement in which the else clause runs only when the previous if clause doesn’t run. This behavior may make some people think the same thing happens to the else clauses in for and while loops. However, if you run the above code, you’ll find out that both else clauses run in each case.

Thus, the rule for the else clause in these cases is that it will be skipped only when there is a break statement executed in the for or while clause. Observe the contrast below:

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Yong Cui
Yong Cui

Written by Yong Cui

Work at the nexus of biomedicine, data science & mobile dev. Author of Python How-to by Manning (https://www.manning.com/books/python-how-to).

Responses (8)

Write a response

I agree about the second one, but first and third uses are actually handy. They may be confusing to people coming from other languages, but who cares, they are in Pythonistan now, and they have to abide by its customs and traditions.

--

Negative slicing is extremely useful and not confusing at all.
The else at the end of a for or while makes sense only if there is a possibility of exiting early due to a break. It lets one easily determine whether the cycle went through to the end or not.

--

These are all useful features though. Nobody should be avoiding features just because you personally were confused by them. The setdefault method is quite handy, and I've never before today heard of anyone thinking it was for retrieving values…

--