Skip to content

Frequently Asked Questions

Jure Šorn edited this page Mar 14, 2024 · 76 revisions
Which Python version is this for?
    Everything should work in the latest Python version, which is 3.12. Every feature that requires version higher than 3.8 has that mentioned in comments or brackets. There are only six such features, four requiring 3.9, and two requiring 3.10.

    As of 12th March 2024, the only libraries whose latest version requires Python version higher than 3.8 are: numpy, pandas and matplotlib. They all require Python 3.9. This cheatsheet covers pandas library version 2.0 or higher which was released on 3rd April 2023.

How to use it?
    This cheatsheet consists of minimal text and short examples so things are easy to find with Ctrl+F / ⌘F. If you're on the webpage, searching for '#<name>' will only search for the titles. To get a link to a specific section click the grey hashtag next to the section's title before copying the address. To search for titles in the text editor use ^<name> with enabled regular expressions option.

    I also keep the Python console open at all times to test little snippets of code, to check out the available functions of a module using code completion and above all, to use help(<module/object/function/type/str>) command. If something is still unclear, then I search the Python docs by googling 'python docs <module/function>'.

    Recently I started using the ptpython REPL (Python console). It supports multiline editing, syntax validation, IDE-like autocompletion and syntax highlighting. It can be installed with pip3 install ptpython.

What does the '<type>' signify?
    It is a placeholder for an object. It needs to be replaced by an expression, literal or a variable that returns/is of that type. However, if it is located on the left side of an assignment (=) then it signifies what type of object is produced/returned by the expression on the right side of the assignment.

Why the '<type>' semantics?
    It makes examples much less ambiguous.

Why are some basics like '<list> = [<el_1>, <el_2>, ...]' and '<el> = <list>[<int>]' missing?
    This cheatsheet is not intended for complete beginners. This way it can save some valuable space. Nonetheless, it tries to be simple enough to be useful for somebody who completed the introductory course. A nice one-page cheatsheet that can help you get started can be found here.

What exactly is <el>?
    El is short for element and can be any object, but it usually denotes an object that is an item of a collection.

What exactly is <collection>?
    Collection is my name for an iterable object. An iterable object in Python is any object that has at least one of iter() and getitem() special methods defined. By convention, <object>.__iter__() should return an iterator of object's items and <object>.__getitem__(<index>) an item at that index. I chose not to use the name iterable because it sounds scarier and more vague than collection, even though it has a precise definition.

    To make matters a bit more confusing, an abstract base class called Iterable doesn't fully follow this definition. An expression instanceof(<object>, collections.abc.Iterable) only checks whether an object has iter() special method, disregarding the getitem().

    Although collection has no definition in Python's glossary, there exists a Collection abstract base class. Expression instanceof(<object>, collections.abc.Collection) returns 'True' for any object that has len(), iter() and contains() special methods defined. <object>.__len__() should return the number of elements and <object>.__contains__(<el>) should check if object contains the passed element.

What about PEP 8?
    Check out Google Style Guide and use Ctrl+Alt+L / ⌥⌘L shortcut in PyCharm to automatically reformat code.

Why are there no blank lines between method definitions?
    This way classes can be copy-pasted into the Python console, which would otherwise raise IndentationError.

Why are tests not covered?
    Check out The Hitchhiker’s Guide to Python for a nice overview.

Django?
    Here is a nice Django cheatsheet.

Why are there no concrete Regex examples?
    Regular expressions are a separate technology that is independent from Python. There are many resources available online.

Why are descriptors not covered?
    Because property decorator is sufficient for everyday use.

Clone this wiki locally