3 Python Tricks That Will Improve Your Code

Roman Orac
Aug 10, 2021

I’ve been coding in Python for more than 10 years. There was a time when I thought I knew it all, which was a clear sign I was getting complacent.

Then I decided to do a bit of research about Python improvements. Those 3.6, 3.7, 3.8 Python versions aren’t there for nothing, right?

After going through release notes, I found these neat tricks that I would like to share with you.

By reading this article, you’ll learn:
  • How to format big integers more clearly
  • A better way to work with file paths
  • The proper way of string formating

1. Formatting big integers

Using Underscores in Numeric Literals. Image by Roman Orac

From Python 3.6 (and onwards) you can use underscores to make numbers easier to read. See PEP 515 for more details.

Let’s look at an example:

a = 1000000000
# Is variable a billion or 100 millions?
# Let's use underscores to make it easier to read
a = 1_000_000_000
# You can group numbers as you like
b = 1_0_9_0

It also works with hexadecimal addresses and grouping bits.

# grouping hexadecimal addresses by words
addr = 0xCAFE_F00D
# grouping bits into nibbles in a binary literal
flags = 0b_0011_1111_0100_1110

2. Pathlib

Working with paths can be challenging especially if your code needs to run on multiple operating systems.

Luckily for us, Python standard library has pathllib.

Let’s look at an example:

from pathlib import Path
path = Path("some_folder")
# output: some_folder
# We can add more subfolders in a readable way
path = path / "sub_folder" / "sub_sub_folder"
# output: some_folder/sub_folder/sub_sub_folder
# make path absolute
# output: /Users/r.orac/some_folder/sub_folter/sub_sub_folder

Despite using / sign as the separator to concatenate paths, pathlib makes it operating system agnostic, meaning it will also work on Windows OS.

Also read: Using Python And Pandas Datareader to Analyze Financial Data

3. Simplify string formatting

f-string formatting in Python. Image by Roman Orac

I’m used to using old school string formatting in Python:

person = 'Roman'
exercise = 0
print("%d-times %s exercised during corona epidemic" % (exercise, person))
# output
# 0-times Roman exercised during corona epidemic

Until recently, I didn’t know there is a better (more modern) way of string formatting in Python.

In Python 3.6, PEP 498 introduces Literal String Interpolation, which simplifies the string formatting.

We can rewrite the example above to:

person = 'roman'
exercise = 0
print(f"{exercise}-times {person} exercised during corona epidemic")
# output
# 0-times Roman exercised during corona epidemic

A string prefixed with f is known as fstring.

fstrings even support math operations:

print(f"{exercise+1}-times {person} exercised during corona epidemic")
# Output
# '1-times roman exercised during corona epidemic'

But I didn’t exercise during the corona epidemic so adding +1 in the fstring would simply be a lie 😂

What about formatting float values?

f = 0.333333
print(f"this is f={f:.2f} rounded to 2 decimals")
# Output
this is f=0.33 rounded to 2 decimals


These Python tricks will make your code more concise and will make coding more enjoyable.

Many Python developers don’t know about these tricks — you’re not one of them anymore.

Before you go

Follow me on Twitter, where I regularly tweet about Data Science and Machine Learning.

Also read: Understanding Python's Collections Module

“3 Python Tricks That Will Improve Your Code”
– Roman Orac twitter social icon Tweet

Join Competition

Share this article:


Post a comment
Log In to Comment

Related Stories

Jan 06, 2023

Top 2 Online Data Science Courses to Improve your Career in 2023

The discipline of Data Science is expanding quickly and has enormous promise. It is used in various sectors, including manufacturing, retail, healt...

By Nikos_DataSource
Nov 25, 2021

5 Tips To Ace Your Job Interview For A Data Scientist Opening

5 Tips To Ace Your Job Interview For A Data Scientist Opening.PNG 795.94 KBImage SourceAspiring data scientists have a bright future ahead of them....

Daniel Morales
By Daniel Morales
Nov 12, 2021

When to Avoid Deep Learning

IntroductionThis article is intended for data scientists who may consider using deep learning algorithms, and want to know more about the cons of i...

Matt Przybyla
By Matt Przybyla

Win USD $5 in cash prizes with our data science competition!

🎉 Model submissions for the "1" competition will close in

arrow-up icon