0% found this document useful (0 votes)
118 views

Python Best Practices Tips and Tricks

As a classic eBook reader that has received lots of rave reviews, Calibre has lived up to its name with its highly-customized settings and practical features. With Calibre, you can not only view EPUBs, do things like changing font size with one click, altering fonts, or looking up unfamiliar words with two clicks, but also modify metadata, manage your eBook collection, edit your eBooks and so on. You can be a techie and dig deep into Calibre’s sophisticated features.

Uploaded by

Santosh Raj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
118 views

Python Best Practices Tips and Tricks

As a classic eBook reader that has received lots of rave reviews, Calibre has lived up to its name with its highly-customized settings and practical features. With Calibre, you can not only view EPUBs, do things like changing font size with one click, altering fonts, or looking up unfamiliar words with two clicks, but also modify metadata, manage your eBook collection, edit your eBooks and so on. You can be a techie and dig deep into Calibre’s sophisticated features.

Uploaded by

Santosh Raj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

30 Python Best Practices, Tips, And Tricks

towardsdatascience.com/30-python-best-practices-tips-and-tricks-caefb9f8c5f5

May 3, 2020

Improve your Python knowledge and skills

Photo by author

Here are 30 Python best practices, tips, and tricks. I’m sure they’ll help you
procrastinate your actual work, and still learn something useful in the process.

1. Use Python 3
In case you missed it: Python 2 is officially not supported as of January 1, 2020.
This guide has a bunch of examples that only work in Python 3. If you’re still on Python
2.7, upgrade now.

If you’re on MacOS, you can use Homebrew to painlessly upgrade Python.

2. Check for a minimum required Python version


You can check for the Python version in your code, to make sure your users are not
running your script with an incompatible version. Use this simple check:

3. Use IPython

1/12
Screenshot by author

IPython is basically an enhanced shell. It’s worth it just for the autocompletion alone,
but there is much more. I like it too for all the magic commands that are built-in. Here
are a few :

%cd — to change the current working directory


%edit — to open an editor and execute the code you typed in after closing the
editor
%env — to show the current environment variables
%pip install [pkgs] — to install packages without leaving the interactive shell
%time and %timeit — to time the execution of Python code

Read the full list here.

Another useful feature is referencing the output of a previous command.In and Out are
actual objects. You can use the output of the 3rd command by using Out[3] .

Install IPython with:

pip3 install ipython

2/12
4. List Comprehensions
A list comprehension can replace ugly for loops used to fill a list. The basic syntax for a
list comprehension is:

[ expression for item in list if conditional ]

A very basic example to fill a list with a sequence of numbers:

And because you can use an expression, you can also do some math:

Or even call an external function:

And finally, you can use the ‘if’ to filter the list. In this case, we only keep the values that
are dividable by 2:

5. Check memory usage of your objects


With sys.getsizeof() you can check the memory usage of an object:

Woah… wait… why is this huge list only 48 bytes?

It’s because the range function returns a class thatonly behaves like a list. A range is a
lot more memory efficient than using an actual list of numbers.

You can see for yourself by using a list comprehension to create an actual list of
numbers from the same range:

6. Return multiple values


Functions in Python can return more than one variable without the need for a dictionary,
a list or a class. It works like this:

This is alright for a limited number of return values. But anything past 3 values should
be put into a (data) class.

7. Use data classes


Since version 3.7, Python offers data classes. There are several advantages over
regular classes or other alternatives like returning multiple values or dictionaries:

a data class requires a minimal amount of code


you can compare data classes because __eq__ is implemented for you

3/12
you can easily print a data class for debugging because __repr__ is implemented
you can easily print a data class for debugging because __repr__ is implemented
as well
data classes require type hints, reduced the chances of bugs

Here’s an example of a data class at work:

An in-depth guide can be found here.

8. In place variable swapping


A neat little trick that can save a few lines of code:

9. Merging dictionaries (Python 3.5+)


Since Python 3.5, it became easier to merge dictionaries:

If there are overlapping keys, the keys from the first dictionary will be overwritten.

10. String to title case


This is just one of those lovely gems:

11. Split a string into a list


You can split a string into a list of strings. In this case, we split on thespace character:

To split on whitespace, you actually don’t have to give split any arguments. By default,
all runs of consecutive whitespace are regarded as a single whitespace separator by
split . So we could just as well use mystring.split() .

12. Create a string from a list of strings


And vice versa from the previous trick, create a string from a list and put a space
character between each word:

If you were wondering why it’s not mylist.join(" ") — good question!

It comes down to the fact that the String.join() function can join not just lists, but any
iterable. Putting it inside String prevents implementing the same functionality in multiple
places.

4/12
13. Emoji

Image by Pixaline on Pixabay

This one will either impress or repulse, depending on who’s looking. On a more serious
note, this can come in handy especially when analyzing social media data.

First, install the emoji module:

pip3 install emoji

With this installed, you can do as follows:

Visit the emoji package page for more examples and documentation.

14. Slicing a list


The basic syntax of list slicing is:

a[start:stop:step]

Start , stop and step are optional. If you don’t fill them in, they will default to:

0 for start
the end of the string for end
1 for step
5/12
Here are some examples:

15. Reversing strings and lists


You can use the slice notation from above to reverse a string or list. By using a negative
stepping value of -1, the elements are reversed:

16. Display kittens


I finally found a good excuse to include kittens in one of my articles! You, however,
might use it to display graphs and such. First, install Pillow, a fork of the Python Image
Library:

pip3 install Pillow

Now download this image to a file called kittens.jpg:

Image by TheDigitalArtist on Pixabay

You can use the following code to display the image from your Python code:

Or you can do it right from IPython:

6/12
It’s me, looking at kittens

Pillow can do a lot more than displaying the image. It can analyze, resize, filter,
enhance, morph, etcetera. See the documentation for all its features.

17. Using map()


One of Python’s built-in functions is called map() . The syntax for map() is:

map(function, something_iterable)

So you give it a function to execute, and something to execute on. This can be anything
that’s iterable. In the examples below I’ll use a list.

Take a look at your own code and see if you can use map() instead of a loop
somewhere!

7/12
18. Get unique elements from a list or string
By creating a set with the set() function, you get all the unique elements from a list or
list-like object:

19. Find the most frequently occurring value


To find the most frequently occurring value in a list or string:

Do you understand why this works? Try to figure it out for yourself before reading on.

You didn’t try, did you? I’ll tell you anyway:

max() will return the highest value in a list. The key argument takes a single
argument function to customize the sort order, in this case, it’s test.count. The
function is applied to each item on the iterable.
test.count is a built-in function of list. It takes an argument and will count the
number of occurrences for that argument. So test.count(1) will return 2 and
test.count(4) returns 4.
set(test) returns all the unique values from test, so {1, 2, 3, 4}

So what we do in this single line of code is take all the unique values of test, which is
{1, 2, 3, 4} . Next, max will apply the list.count function to them and return the
maximum value.

And no — I didn’t invent this one-liner.

20. Create a progress bar


You can create your own progress bar, which is fun to do. But it’s quicker to use the
progress package:

pip3 install progress

Now you can create a progress bar with minimal effort:

The following animation demonstrates all the available progress types:

Animation by Giorgos Verigakis from progress

21. Use the _ in an interactive shell

You can obtain the result of the last expression with the underscore operator, e.g. in
8/12
You can obtain the result of the last expression with the underscore operator, e.g. in
IPython this looks like:

In [1]: 3 * 3
Out[1]: 9In [2]: _ + 3
Out[2]: 12

This works in the Python shell too. In addition, the IPython shell allows you to use
Out[n] to get the value of the expression In[n] . E.g., Out[1] would give us the
number 9 in the example above.

22. Quickly create a web server


You can quickly start a web server, serving the contents of the current directory:

python3 -m http.server

This is useful if you want to share some stuff with a co-worker or want to test a simple
HTML site.

23. Multi-Line Strings


Although you can use triple quotes to include multi-line strings in your code, it’s not
ideal. Everything you put between the triple quotes becomes the string, including the
formatting, as you can see below.

I prefer the second way, which concatenates multiple lines together, allowing you to
format your code nicely. The only downside is that you need to explicitly put in
newlines.

24. Ternary Operator For Conditional Assignment


This is another one of those ways to make your code more concise while still keeping it
readable:

[on_true] if [expression] else [on_false]

As an example:

x = "Success!" if (y == 2) else "Failed!"

25. Counting occurrences

9/12
You can use Counter from the collections library to get a dictionary with counts of all the
You can use Counter from the collections library to get a dictionary with counts of all the
unique elements in a list:

26. Chaining of comparison operators


You can chain comparison operators in Python, creating more readable and concise
code:

27. Add some color

Screenshot by Jonathan Hartley from Colorama

With Colorama, you can add some color to your terminal.

28. Working with dates


The python-dateutil module provides powerful extensions to the standard datetime
module. Install it with:

pip3 install python-dateutil

You can do so much cool stuff with this library. I’ll limit the examples to just this one that
I found particularly useful: fuzzy parsing of dates from log files and such.

Just remember: where the regular Python datetime functionality ends, python-dateutil
comes in!

29. Integer division

10/12
By Torindkflt — Public Domain

In Python 2, the division operator ( / ) defaults to an integer division, unless one of the
operands is a floating-point number. So you have this behavior:

# Python 2
5/2=2
5 / 2.0 = 2.5

In Python 3, the division operator defaults to a floating-point division and the // operator
has become an integer division. So we get:

Python 3
5 / 2 = 2.5
5 // 2 = 2

For the complete motivation behind this change, you should read PEP-0238.

30. Charset detection with chardet


You can use the chardet module to detect the charset of a file. This can come in very
useful when analyzing big piles of random text. Install with:

pip install chardet

11/12
You now have an extra command-line tool called chardetect, which can be used like
You now have an extra command-line tool called chardetect, which can be used like
this:

chardetect somefile.txt
somefile.txt: ascii with confidence 1.0

You can also use the library programmatically, check out the docs.

That’s it! 30 tips, tricks, and best practices to start off the new year. I hope you enjoyed
them as much as I enjoyed creating the list. If you have anything to add, feel free to
leave a comment!

You’ll probably like this following article as well:

The 22 Most-Used Python Packages in the World

Educational and surprising insights into how Python is used

medium.com
If you enjoyed this article, please subscribe to mySubstack.

12/12

You might also like