5 Python Tricks Nobody Told You

5 Python Tricks Nobody Told You

Useful tricks for useless things.

Underscore is a gem in Python. It has so many uses, and also you use it when you don’t have any use for it. Let me explain. This article is about everything you can do with a single underscore inside of your Python program, and when and where to use it.

Here are the 5 common ways to use an underscore (“_”) in Python

1. The Throw-Away Variable

When you don’t need a variable, instead of creating one like temp or temporary_variable , you can just write _. Let me give you an example:

first_name, middle_name, last_name = ["fname", "mname", "lname"]
print(first_name, last_name)

Here we aren’t using the middle_name variable, so we can use _ instead to define it as the throw-away variable like this:

first_name, _, last_name = ["fname", "mname", "lname"]
print(first_name, last_name)

It does not change the way the interpreter works but rather is just an indication to the developer that this variable is not needed in the actual program.

2. The Un-needed Loop Variable

There are code blocks where you don’t require the variable (Eg: Index) in the code, you just want the ’n’ number of iterations that the loop provides.

for x in range(10):
num = num * 10

In this loop, the x variable is useless. And if you use the variable x after this loop somewhere, you might get some bugs due to the value it is holding because of it. Instead, you can use an underscore with the same logic as the throw-away loop:

for _ in range(10):
num = num * 10

This way you prevent bugs, and make the code a tad bit more understandable as you signify the uselessness of the loop variable.

You can also use this with the enumerate function when — suppose you only require the index — you don’t require one of the two variables it returns:

word_list = ["word-1", "word-2", "word-3", "word-4"]
for idx, _ in enumerate(word_list):
print(idx)

Here you only store the index idx and discard the items using _ . This way the loop is more readable and shows what it does and what it doesn’t.

3. Make Long Numbers More Readable

You probably won’t understand what this number is until you carefully count the digits first:

134578920

You are accustomed to reading numbers in a format like this:

13,45,78,920 — for the Indian numeral system OR
134,578,920.

You can achieve something similar in Python when writing code. No, I am not talking about printing, but rather in actual code. Take a look:

num_1 = 134578920 # is same as
num_2 = 134_578_920

print(num1)
print(num2)
# output
# > 134578920
# > 134578920

And if you want to print it the same way, you can use f-strings

print(f"{num_1:,}") # 134,578,920
# Note the difference
print(f"{num_1:_}") # 134_578_920

You instantly made your life 10x easier.

4. The REPL Trick

If you use REPL every now and then, and you need to create variables to store values of every expression you write, worry not.

Underscore in REPL holds the value for the last evaluated expression. Take a look

5. The Valid Variable

_ as we have seen previously, doesn’t change anything for the interpreter in the cases we saw. So technically speaking, _ is a valid variable name, and you can use it as one.

But I strongly recommend against doing that unless you are in someone else’s codebase and want to mess with them.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *