Python is one of the world's most popular, in-demand programming languages. This is for many reasons:
It's easy to learn
It's super versatile
It has a huge range of modules and libraries
I use Python daily as an integral part of my job as a data scientist. Along the way, I've picked up a few useful tricks and tips.
Here, I've made an attempt at sharing some of them in an A-Z format.
Most of these 'tricks' are things I've used or stumbled upon during my day-to-day work. Some I found while browsing the Python Standard Library docs. A few others I found searching through PyPi.
However, credit where it is due - I discovered four or five of them over at awesome-python.com. This is a curated list of hundreds of interesting Python tools and modules. It is worth browsing for inspiration!
all or any
One of the many reasons why Python is such a popular language is because it is readable and expressive.
It has often joked that Python is 'executable pseudocode'. But when you can write code like this, it's difficult to argue otherwise:
x = [True, True, False]
if any(x): print("At least one True")
if all(x): print("Not one False")
if any(x) and not all(x): print("At least one True and one False")
Do you want to plot graphs in the console?
$ pip install bashplotlib
You can have graphs in the console.
Python has some great default datatypes, but sometimes they just won't behave exactly how you'd like them to.
Luckily, the Python Standard Library offers the collections module. This handy add-on provides you with further datatypes.
from collections import OrderedDict, Counter
# Remembers the order the keys are added! x = OrderedDict(a=1, b=2, c=3)
# Counts the frequency of each character y = Counter("Hello World!")
Ever wondered how you can look inside a Python object and see what attributes it has? Of course, you have.
From the command line:
>>> dir() >>> dir("Hello World") >>> dir(dir)
This can be a really useful feature when running Python interactively, and for dynamically exploring objects and modules you are working with.
def addMatrix(a : Matrix, b : Matrix) -> Matrix: result =  for i,row in enumerate(a): result_row = for j, col in enumerate(row): result_row += [a[i][j] + b[i][j]] result += [result_row] return result
x = [[1.0, 0.0], [0.0, 1.0]] y = [[2.0, 1.0], [0.0, -2.0]]
z = addMatrix(x, y)
Although not compulsory, type annotations can make your code easier to understand.
They also allow you to use type checking tools to catch those stray TypeErrors before runtime. Probably worthwhile if you are working on large, complex projects!
This creates a randomized 128-bit number that will almost certainly be unique.
In fact, there are over 2¬Ļ¬≤¬≤ possible UUIDs that can be generated. That's over five undecillion (or 5,000,000,000,000,000,000,000,000,000,000,000,000).
The probability of finding duplicates in a given set is extremely low. Even with a trillion UUIDs, the probability of a duplicate existing is much, much less than one-in-a-million.
Pretty good for two lines of code.
This is probably my favorite Python thing of all.
Chances are you are working on multiple Python projects at any one time. Unfortunately, sometimes two projects will rely on different versions of the same dependency. Which do you install on your system?
result = wikipedia.page('freeCodeCamp') print(result.summary) for link in result.links: print(link)
Like the real site, the module provides support for multiple languages, page disambiguation, random page retrieval, and even has a donate() method.
Humour is a key feature of the Python language - after all, it is named after the British comedy sketch show Monty Python's Flying Circus. Much of Python's official documentation references the show's most famous sketches.
The sense of humor isn't restricted to the docs, though. Have a go running the line below: