/python_learningresources

Links to books, repositories, blogs and videos about useful Python resources

MIT LicenseMIT

python_learningresources

Links to books, repositories, blogs and videos about useful Python resources

Language

Language fundamentals

  • Difference between lists and tuples

  • Create dictionary from two lists

  • Index a list with another list

  • Single list from a list of lists; example and expected output:

    nested_list = [['some'], ['items']]
    flat_list = ['some', 'items']
    • List comprehension
      flat_list = [item for sublst in nested_list for item in sublst]
    • Chaining
      import itertools
      flat_list = list(itertools.chain.from_iterable(nested_list))
      # equivalently: flat_list = list(itertools.chain(*nested_list))
    • Monoid
      flat_list = sum(nested_list, [])
  • Slice assignment

  • Using the else clause in for and while loops

  • Function arguments

    As of Python 3.8, positional-only arguments are supported. These arguments cannot by passed as keywords. The forward slash, /, indicates to Python that all arguments before the forward-slash are positional-only arguments. Anything following the forward slash are positional or keyword arguments up to the *. The asterisk indicates that everything following it are keyword-only arguments. For instance, calling the function

    def positional(name, age, /, a, b, *, key):
        pass

    Here are some invalid calls to this function:

    >>> positional(name='Mike')
    TypeError: positional() got some positional-only arguments passed as keyword arguments: 'name'
    >>> positional('Mike', 17, 2, b=3)
    TypeError: positional() missing 1 required keyword-only argument: 'key'
    >>> positional('Mike', 17, 2, 3, 'test')
    TypeError: positional() takes 4 positional arguments but 5 were given
    >>> positional('Mike', 17, 2, b=3, keyword='test')
    TypeError: positional() got an unexpected keyword argument 'keyword'

    Some valid function calls are

    positional('Mike', 17, 2, 3, key='test')
    positional('Mike', 17, 2, b=3, key='test')

File handling

Conventions

A major design priciple behind Python was that code should be easily read. Conventions greatly improve the readability of your code, making it ''Pythonic''.

Packaging

The directory structure of a Python project should be created so that it is easily testable, deployable and put under continuous integration. Some links I found useful:

NumPy

  • NumPy version
    import numpy
    print numpy.__version__
    or from the command line:
    python -c 'import numpy; print(numpy.__version__)'
  • Print information about the CPU
    import numpy.distutils.cpuinfo.cpu
    cpu.info()
  • Figure out which BLAS and LAPACK implementations are used by your NumPy (and therefore SciPy, etc.)
    import numpy.distutils.system_info
    get_info('blas_opt')
    get_info('lapack_opt')
    Here is the list of all options for get_info.

Best practices

Visualization

Plotting with Matplotlib

Browse the extensive gallery for examples, read the User's Guide, or check the tutorials. Two types of plotting:

Links to APIs I often use:

Positioning the legend

Class hierarchy

If you install Pylint, it comes with Pyreverse. Type

pyreverse -o <output_format> <module_name>

to the terminal to save the class hierarchy fetched from the module <module_name> in <output_format> (png, svg, eps, pdf, dot, etc.). Note that module_name must be importable. If module_name is a package that contains other packages or modules, all the contents of the package will be parsed. To select a subpackage or a module, use the slash; e.g. to process only f2py from numpy, use

pyreverse -o png numpy/f2py

It is often useful for the end user to ignore the tests. You can ignore a directory by giving the --ignore flag. In the example above:

pyreverse -o png --ignore tests numpy/f2py

For further options, check the documentation:

pyreverse --help

GUIs

  • view pandas DataFrame objects

    PandasDataFrameGUI supports useful data analysis (filtering, sorting, plotting, etc.) and has few dependencies. A common mistake is that your code already imported matplotlib, which selected a backend other than 'WXAgg'. The solution is to load matplotlib before you use your own code, with

    import matplotlib
    matplotlib.use('WXAgg')

    as shown here.

    Online

    Binder