Make Your Own Python CheatSheet
You probably stumble upon Python programming cheatsheets pretty regularly. They vary considerably, some aiming at being minimalist and practical while others favour style over content to be eye-catching on social media. I could critique the numerous types and styles I have seen over the years but I'll spare you that and just say that I personally don't find them very useful. In this article I will propose an alternative: whenever you learn or discover something about Python which you want to remember for future use then type it into a .py file reserved for that purpose. Don't copy and paste it, actually type it and run it. And include plenty of comments so you're not baffled by your own code 5 minutes later!
Cheatsheets are usually aimed at beginners but there's no reason for programmers at any level to maintain their own, tailored to their particular requirements. If it starts to get unwieldy then split it into more than one file: OOP, multi-threading, NumPy, Matplotlib and so on.
I could end this article here but it would be pretty short, not much more than a social media post. However, I'll include a sample .py file as an example of what I am suggesting. The code in this file is somewhat more advanced than beginner level - there's no initialising variables, def'ing functions or for loops as I assume the sort of people who read my articles are already proficient in Python.
You may or may not find this code useful but that's not the intention. Please feel free to download the file and adapt and extend it as you see fit. Keep it somewhere easily accessible or, better still, create a desktop shortcut. If there's anything in it you can do without even thinking about then delete it; it's just wasting space. Fiddle about with the code, add comments, rename variables and so on so it's more to your liking. Make it your own.
And - this is the most important bit - add to it each and every time you learn or use something new. Use easily-searchable function and variable names, and, as I mentioned earlier, include plenty of comments for anything that might not be obvious.
If you are a Jupyter user I have replicated the code in the .py file as a .ipynb file. If you are not familiar with Jupyter it's a browser-based interactive Python environment (you can also use R and Julia) in which you can edit and run Python code in individual vertically-aligned cells. The code and other elements can then be saved as JSON-based .ipynb files.
This project consists of the following files which you can clone/download from the GitHub repository.
cheatsheetpython.py
cheatsheetpython.ipynb
In the future I'll write a few more on topics such as NumPy and Matplotlib.
I am not going to show the code here or provide a commentary on it; please download the file and read or skim its contents. However, I will list the topics covered.
decorators: functions which wrap other functions to extend or modify them
map: calls a function on each item in an iterable and returns an iterator of the results
reduce: repeatedly calls a function on successive values in an iterable and the return values of the previous function call, ultimately returning a single value
zip: merges 2 or more iterables into tuples containing correspondingly indexed values from each iterable
complex numbers: Python has extensive functionality for handling complex numbers
generators: create an iterator wrapping a function which can then be used in, for example, a for loop
dateandtime: handling dates and times can be fiddly in any programming language so it is useful to have a few example snippets of code
list methods: the list data structure provides numerous methods so it's good to have a reference for the ones you use frequently
list comprehensions: these can get quite complicated so it is worth maintaining a few snippets to copy paste and adapt to your current needs
namedtuple: the syntax for namedtuples is simple but a bit clunky IMHO so you might find this example handy
counter: returns a dictionary-like object containing the counts or frequencies of each item in an iterable. It's actually surprisingly complex but this example just shows basic usage.
I hope this article has inspired you to consider creating and maintaining your own cheatsheet of code snippets, and as I mentioned you are welcome to download and adapt mine. Also, remember there's a Jupyter version if you find that more convenient.