Python Cheatsheet
gto76.github.io
gto76.github.io
See also:
* Python Crash Course cheatsheet: https://ehmatthes.github.io/pcc_3e/cheat_sheets/
* Scientific Python cheatsheet: https://ipgp.github.io/scientific_python_cheat_sheet/
* Common beginner errors: https://pythonforbiologists.com/29-common-beginner-errors-on...
* Python regular expression cheatsheet: https://learnbyexample.github.io/python-regex-cheatsheet/ — my blog post, includes examples as well
• Does not cover the “raise from” syntax¹.
• Does not cover Structural Pattern Matching (the “match” and “case” keywords)². (EDIT: The FAQ states that it covers Python 3.8, and this feature was added in Python 3.10.)
• In my basic script template I have this, to show warnings by default:
if not sys.warnoptions:
import warnings
warnings.simplefilter("default")
• My basic script template also uses the “-bbI” switches to python on the shebang line.1. <https://docs.python.org/3/reference/simple_stmts.html#the-ra...>
2. <https://docs.python.org/3/reference/compound_stmts.html#the-...>
> Inside Function Call
> Splat expands a collection into positional arguments, while splatty-splat expands a dictionary into keyword arguments.
args = (1, 2)
kwargs = {'x': 3, 'y': 4, 'z': 5}
func(*args, **kwargs)1. Use of <identifier> notation for almost everything. It might be technically sound, but it brings to mind reading Backus-Naur form specs.
For cheat sheets my_int, my_iterator etc format is more pleasant for eyes.
2. Examples from various external libraries are of questionable real life utility, they are a nice showcase of what is possible, but are really too terse at times - looking at you popcorn audio example.
Official docs for libraries such as Plotly or Pandas are much more pleasant. Then again Copilot is even faster for looking up a tricky matplotlib incantation...
Thus the cheat sheet should have been split in two parts, one would cover Python and possibly standard library.
Second part would cover the popular libraries.
You might also cross link topics into their relevant python docs for deeper references. I know I always need to dig deeper into CSV/argparse for specific things when working with those systems.
Edit: awesome content
___
Here are a few things I noticed that could be improved on the Python cheatsheet page:
- The dictionary section is missing explanation of dict comprehensions. Dict comprehensions are an important and useful feature for constructing dictionaries in a concise way.
- In the section on modules, it would be good to mention virtual environments. Virtual environments are an important tool for Python dependency and package management.
- The examples in the Pandas section are useful, but more explanation or details could be provided on some of the core DataFrame operations like merging/joining, groupby, aggregations, etc.
- In the sections on concurrency and parallelism, async/await could be explained and demonstrated. Asyncio is commonly used for asynchronous programming in Python.
- The cheatsheet focuses mainly on built-in modules and functionality. It could be useful to also cover some widely used 3rd party libraries like NumPy, SciPy, Matplotlib, TensorFlow, etc.
- Sections on testing and debugging could be added - things like unittest, pytest, logging, debugging tools. Testing and debugging are key skills for Python developers.
- The cheatsheet is very text heavy. More visuals, diagrams, or tables could help make it more scannable and easier to navigate.
Overall it covers a lot of ground, but filling in some of those gaps would make it more comprehensive and useful as a reference. The content is excellent, just some ways it could be expanded on.
> The dictionary section is missing explanation of dict comprehensions. Dict comprehensions are an important and useful feature for constructing dictionaries in a concise way.
Those are in the inline section, with the other comprehensions: https://gto76.github.io/python-cheatsheet/#inline
> In the sections on concurrency and parallelism, async/await could be explained and demonstrated. Asyncio is commonly used for asynchronous programming in Python.
Those are in the coroutines section: https://gto76.github.io/python-cheatsheet/#coroutines
> The cheatsheet focuses mainly on built-in modules and functionality. It could be useful to also cover some widely used 3rd party libraries like NumPy, SciPy, Matplotlib, TensorFlow, etc.
NumPy: https://gto76.github.io/python-cheatsheet/#numpy
Matplotlib: https://gto76.github.io/python-cheatsheet/#plot
(It doesn't cover SciPy, Tensorflow. That said, I've been writing python for 23+ years, and can count on two fingers how many times I've needed either of those.)
----
Some parts of the critique are correct, if not especially useful IMO. I'd say most of the parts of the critique that are correct seem to misunderstand the purpose of the cheatsheet. The best points the model generated, IMO, were the ones related to pandas and virtual environments.
It is great for a cheat sheet to be on a single page. It makes ctrl+f searching useful and quick.
A cheat-sheet I really like is https://devhints.io/bash which I feel has just enough info (for me) when jumping back into bash from other work to give me proper context.
I have nothing against references or this website as a reference those are useful as well. There is a value in having a good reference which strikes a balance of being exhaustive while avoiding the trap of unreadable verbosity that specifications and standards often have.
Likewise, nobody knows how to write technical documentation that's readable by everybody.
In the case of Python, the official docs are typically the last place where I'd look for stuff, given the quality of documents such as this cheat sheet. There's still a need for authoritative "developer" documentation, but not for me.
The diversity of Python developers, from hobbyists to pro's, justifies a similar diversity of documentation.
you say "I can't speak to Python specifically", but i can assure you that the official docs are far superior to this cheat sheet.
I'm embarrassed to say I learned what generators are from this. 7 lines of code; totally unambiguous explanation. I don't even use iterators all that often, so I'm not surprised that I've never googled something that lead me to them.
Compare with numpy and scipy, where the docstrings are comprehensive (there's a correspondance between online and embedded docs.)
https://gist.github.com/gto76/145776c5eace059b09ca1d6ca771a1...
(The first person to mention ChatGPT gets bopped. I do not need to use an LLM to look up module documentation)