The world’s leading publication for data science, AI, and ML professionals.

5 Python Tricks You Should Start Using in 2021

Python is evolving. Don't get left behind!

Photo by Michael Dziedzic on Unsplash
Photo by Michael Dziedzic on Unsplash

Start the New Year with one of the best New Year’s resolutions: Learn more Python.

You can start with this article in which I present 5 Python tricks that will make your life easier.

You’ll learn:

  • How to format big integers more clearly
  • What are Magic commands in IPython
  • A simple way to debug Code
  • A better way to work with file paths
  • The proper way of string formating

1. Underscores in Numeric Literals

Using Underscores in Numeric Literals. Image by Roman Orac
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. Magic commands with IPython

Using %paste command to paste the code to IPython interpreter. Photo by Roman Orac
Using %paste command to paste the code to IPython interpreter. Photo by Roman Orac

My Development workflow with Python is to have a terminal pane with neovim on the left and IPython interpreter on the right.

This makes testing the code easier as I can copy the code from the left and paste it in the interpreter on the right.

What is the IPython interpreter?

It is like a Python interpreter but on steroids.

IPython is a command shell for interactive computing in multiple Programming languages, originally developed for the Python programming language, that offers introspection, rich media, shell syntax, tab completion, and history

What is the easiest way to paste the code to the IPython interpreter from the clipboard?

Did you know that IPython supports magic commands?

One of them is the %paste command, which pastes the code from the clipboard with formating.

Simply type %paste in the IPython interpreter.

3. Debugging Python code

Debugging Python code IPDB. Image made with carbon.sh. Image by Roman Orac
Debugging Python code IPDB. Image made with carbon.sh. Image by Roman Orac

PyCharm editor comes with a build-in debugger for Python code. But what if you are using Visual Studio Code, Atom, Sublime or Vim?

You can use pdb module:

foo()
import pdb; pdb.set_trace() 
# your code will stop here and interpreter will open
bar()

Python 3.7 (and onwards) simplifies this with a build-in breakpoint function call:

foo()
breakpoint()
# your code will stop here and interpreter will open
bar()

See PEP 553 for more details.

4. Pathlib

Photo by Alice Donovan Rouse on Unsplash
Photo by Alice Donovan Rouse on Unsplash

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")
print(path)
# output: some_folder
# We can add more subfolders in a readable way
path = path / "sub_folter" / "sub_sub_folder"
print(path)
# output: some_folder/sub_folter/sub_sub_folder
# make path absolute
print(path.resolve())
# output: /Users/r.orac/some_folder/sub_folter/sub_sub_folder

5. Simplify string formatting

f-string formatting in Python. Image by Roman Orac
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

Conclusion

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

I’ve been coding in Python for the past 10 years and I’ve only learned these tips recently. By using them, it makes Python Programming more enjoyable.

Let’s connect

Talk: Book a call Socials: YouTube 🎥 | LinkedIn | Twitter Code: GitHub


Related Articles