Tuple packing and unpacking are magical.

Call module.doc, rather help(module) for docstring.

*identifier means it will initialize to a tuple, receiving excess parameters, defaulting to empty tuple.

**identifier means it will initialize to a dictionary, receiving excess keyword argument, defaulting to an empty dictionary.

ternary operators (generally frowned upon in the Python community) https://stackoverflow.com/questions/394809/does-python-have-a-ternary-conditional-operator

_single_leading_underscore: weak "internal use" indicator. E.g. from M import * does not import objects whose name starts with an underscore.
single_trailing_underscore_: used by convention to avoid conflicts with Python keyword, e.g. Tkinter.Toplevel(master, class_='ClassName')

__double_leading_underscore: when naming a class attribute, invokes name mangling (inside class FooBar, __boo becomes _FooBar__boo

__double_leading_and_trailing_underscore__: "magic" objects or attributes that live in user-controlled namespaces. E.g. __init__, __import__ or __file__. Never invent such names; only use them as documented.

PEP 8 – Style Guide for Python Code

shallow vs deep copy

iterators

generators

coverage run –source=. manage.py test -v 2 coverage html

“-c command Specify the command to execute (see next section). This terminates the option list (following options are passed as arguments to the command).”

Iterables

dict/mapping list/sequence dataframe generator

Time Complexity

List Operation Average Case Amortized Worst Case Copy O(n) O(n) Append O(1) O(1) Pop last O(1) O(1) Pop intermediate O(n) O(n) Insert O(n) O(n) Get item O(1) O(1) Set item O(1) O(1) Delete item O(n) O(n) Iteration O(n) O(n) Get slice O(k) O(k) Del slice O(n) O(n) Set slice O(k+n) O(k+n) Extend O(k) O(k) Sort O(n log n) O(n log n) Multiply O(nk) O(nk) x in a O(n) min(s), max(s) O(n) Get length O(1) O(1)

collections.deque Operation Average Case Amortized Worst Case Copy O(n) O(n) Append O(1) O(1) Append left O(1) O(1) Pop O(1) O(1) Pop left O(1) O(1) Extend O(k) O(k) Extend left O(k) O(k) Rotate O(k) O(k) Remove O(n) O(n)

Set

Dict Operation Average Case Amortized Worst Case k in d O(1) O(n) Copy O(n) O(n) Get item O(1) O(n) Set item O(1) O(n) Delete item O(1) O(n) Iteration O(n) O(n)

Debugging

pytest <file> --pdb this will drop a trace at where it failed

python -m pdb <file.py>

or

import pdb; pdb.set_trace()

Use h for help.

Errors & Exception Handling

traceback

import traceback

try:
    1/0
except Exception as e:
    traceback.print_exc()

Linting, Styling

~/Library/Python/3.7/bin/pylint code.py ~/Library/Python/3.7/bin/pydocstyle code.py

your pathname>pylint -rn --max-line-length=79 --generate-rcfile > name.pylintrc

“Put the changes you want before the -generate-rcfile > name.pylintrc statement and provide your own name before the .pylintrc extension. You can create a configuration file either stand-alone, as just shown, or at the same time you evaluate a Python program. The .pylintrc file is automatically saved in your current working directory, though there is an option for adding a directory path (see https://pylint.org and https://pylint.readthedoes.io/en/latest/user_guide/run.html for more details).”

“To reuse your custom configuration file, use the -rcfile option followed by the name of your personal configuration file and the name of the program you’re evaluating. For example, to run myconfig.pylintrc on the pseudonyms_main.py program, enter the following: C:\Python35\Stuff>pylint --rcfile myconfig.pylintrc pseudonyms_main

Techniques

Chaining

Assignment

>>> x = y = z = 1
>>> x, y, z
(1, 1, 1)

Comparison

>>> x = 5
>>> 1 < x <= 10
True
>>> 10 < x <= 20
False

Lambda

A lambda function is a small anonymous function

A lambda function can take any number of arguments but can only have one expression

Syntax: lambda arguments : expression

Example:

def identity(x):
    return x

lambda x : x

Colab

To remove folders:

import shutil

shutil.rmtree('/folder_name')

By design, rmtree fails on folder trees containing read-only files. If you want the folder to be deleted regardless of whether it contains read-only files, then use:

shutil.rmtree('/folder_name', ignore_errors=True)