Aaron's profile picture
Aaron
@AbzAaron
View on Twitter

20 useful tricks & tips in Python.

A Thread 👇

  1. Negative Rounding

We can specify an optional 2nd argument in Python's round function.

If we pass a negative value here, this will start rounding from the left of the decimal point; meaning we can round to the nearest 10, 100, and so on.

  1. Reversing a String

There's an extremely easy way to reverse strings in Python using slicing.

Below we use [::-1]

This means start at the end of our string, and end up at position 0.

  1. Joining Strings

The join string method joins elements of a string, list or tuple with a string separator of our choice.

This could be useful if we have a list of strings we wish to concatenate into one single string.

  1. Joining Strings with Filter

We can also use filter & lambda to extract items from an iterable before using join.

The filter function filters items through a function.

Below, we pass it a lambda function (function defined without a name) which filters our list.

  1. Open a URL

We can open up URL's in Python easily with the webbrowser module 🙂

  1. With Statement

When opening files, we can use the with statement.

This will guarantee the file is closed at the end of the code block, even if an exception is raised.

  1. The Time Module

The time module helps us keep track of how much time has passed from one point to another.

This is useful when assessing how long a chunk of code or function takes to run 🙂

  1. Underscore as a "throwaway"

The underscore ( _ ) can be used as a "throwaway" variable name.

When iterating, like in the example below, it indicates that the loop variable isn't used.

  1. Docstrings

Docstrings are string literals that appear within functions, methods, classes, or modules.

These are a form of documentation, describing what a code block does.

We can access them using Python's help function or doc method.

  1. Dictionary Key-Value Pairs

Using Python's dictionary items method, we can easily return the key-value pairs of a dictionary.

  1. Swapping Variables

Often in programming, when we want to swap 2 variables, we need to introduce a 3rd temp variable.

In Python, we can just do the following 🙂

  1. List Comprehension

We might want to create a new list, iterate through an old list, and append to our new list based on some condition.

With list comprehension, we can achieve this in one simple line:

  1. Multiprocessing

Want to speed up your code?

With Python's multiprocessing module, we can split work across separate processes.

Running several processes at the same time is faster than running them one after the other.

  1. Virtual Environments

Everyone should start using virtual environments.

We can easily set these up in Python.

These are useful when working on multiple projects, each of which requires different versions of third party libraries.

  1. Dictionary from Lists

If we have two lists, and want to generate a dictionary from them, with one list as keys, and the other as values, we can achieve this simply with Python's zip function 🙂

  1. Merge Dictionaries

We can easily merge dictionaries together in Python using the below syntax. Give it a try 🙂

  1. Enumerate Function

This allows you to iterate over an object while keeping track of how many iterations have occurred.

Enumerate starts counting at 0 by default, but we can change this to whatever we want.

  1. F-Strings

F-strings are a great way to format strings.

They make our code more concise and readable (as well as faster!)

  1. Removing List Duplicates

Want to remove all duplicates from a List?

Just convert the List to a Set (these don't allow duplicate values) and the convert back to a List. Simple 🙂

  1. Numeric Literals Underscore

If you're writing large numbers in Python, it can be difficult to read, and we can't use commas without just converting the number to a string.

Instead, use underscores 🙂

The behaviour of the two integers below is identical.

That's it 😀

If you liked this, or learned something new, consider liking, retweeting, and following me for more Python and SQL content.

And as @razacodes would say, go and code some Python!

Help us raise more money for charities by sharing this page ♥️
Wait! Before you go...
Grab Exclusive Deals for Books, Courses, Software.
100% of Profits Are Donated To Research-Backed Charities.