Python basics



1. Python versions
2. Package managers
3. Tips
4. Compiler


1. Python versions

Mac OSX can have multiple type of python; System python and the installed pythons. The system python should never be removed. To find the working version of python, open Terminal and type either of

python --version
python3 --version

depending on python 2.x or 3.x. To remove unwanted versions,

 brew uninstall python
 brew uninstall python3

This will remove the python installed via Homebrew. When you encouter an error, a quick fix is

 brew doctor
 brew upgrade
 brew cleanup

2. Package managers

Pip and Pipenv

Pip is a Package Installer for Python. More precisely pip is for python3.x, and pip2 is for python 2.x. If we want to install a package on Python2, use pip2. Some examples are

pip2 install cython
pip2 install PyOpenGL
pip install pygame
pip install dlib

If you want to check which (legacy) versions of the package (e.g. numpy) is available, use (see here)

pip install --use-deprecated=legacy-resolver numpy==

When using virtual environments (pyenv-virtualenv, pipenv, anaconda, etc), we should first activate an environment, then install the package. For example, installing via pip (without virtualization) is different from installing via pipenv. If we want to install a package inside pipenv, type

pipenv install cython

Environment Isolation Tools (1. pyenv)

Pyenv manages different versions of python. Both pyenv and pyenv-virtualenv can be installed by brew in OSX,

brew install pyenv
brew install pyenv-virtualenv

See the official website for setting up PATH. To check which version of pythons are available,

pyenv install --list

To install (or uninstall) a python version in pyenv,

pyenv (un)install 3.x.y

To check all the installed versions of pythons inside pyenv,

pyenv versions

To create a new virtal environment using the current version of python,

pyenv virtualenv environment_name

Environment Isolation Tools (2. pyenv-virtualenv)

Pyenv-virtualenv manages virtual environments. Suppose you installed pyenv, pyenv-virtualenv, the latest version of python, set PATH, and created a new virtal environment environment_name (see this page for how to install).

To activate the virtal environment environment_name, and launch a jupyter notebook,

pyenv activate environment_name
(environment_name)> jupyter notebook

If you install various packages in the jupyter notebooks, then the packages remain installed inside this environment. (In Google Colab, you need to install packages for each session.) To exit from the virtal environment, type Ctrl+C or

pyenv deactivate

Switching iPython kernel

Even if you launch a jupyter notebook from inside a virtual environment, jupyter uses its default kernel. In order to switch to the ipython kernel of your new environment, you should click "Kernel > Switch Kernel" in the menu. Alternatively, you can use the command (see here)

jupyter notebook --NotebookApp.kernel_manager_class=my_kernel 

Pip's in pyenv-virtualenv

Pip exists for each virtual environment of Python. Sometimes, pyenv-virtualenv chooses the correct Python but an incorrect Pip, which causes a ModuleNotFoundError. To avoid this problem, one may try

!python3 -m pip install XXX

rather than

!pip install XXX

The former calls Pip associated to the Python in action (virtual Python in the virtual environment), whereas the latter calls Pip associated to the global Python.

3. Tips

Maths  The math module can be imported in two ways,

  • “import math”; use arctan as math.atan
  • “from math import atan” or “from math import *”; use arctan as atan
  • the power multiplication is a**b, where b should be a float
  • the imaginary unit is 1j. j is a variable
  • exp() does not take complex arguments by default. We should import cmath and use cmath.exp().

List 

  • Numbers in a square bracket [0,1] is a mutable list
  • Numbers in a parenthesis (0,1) is an immutable tuple; see SE
  • the notation [1]*3 means [1,1,1] and not [3,3,3]
  • it is useful to create lis=[0]*length (an empty list with a fixed length), because we cannot create a list directly by specifying an element, like lis[1]=0
  • the n-th element of a list L is L[n-1], where n=1,2,…,len(L)
  • a more general format is L[start:end:step], thus L[::-1] shows the list elements in the reverse order
  • Lists cannot be added or subtracted. Should use np.array

Loop  “for” loop should be written in the format,

for (var) in (iterable object):

and keep indentation within the scope of the loop. Strictly speaking, this corresponds to foreach in C++

Range  range(start, stop, step) is an iterable object, which should be used with caution.

  • it returns a list of integers as the range type; e.g. list(range(0, 10, 2)) gives [0,2,4,6,8]
  • all arguments must be integers
  • range and list have the same index structure; the content starts at 0, ends at (length-1)

Dictionary data type 

  • the method items() transforms a dict into a dict_items
  • list() transforms a dict_items into a list
  • See Data Structures for the dictionary data type
  • To extract the value for the key of a dictionary, use get(key,0). It returns 0 when no such key exists

Decimal and binary 

  • int(b) returns b as a decimal number; e.g. 0010 to 10
  • int(b, 2) returns b as a binary number; e.g. 0010 to 2
Use format() to convert a decimal number to a text string keeping the leading zero’s. For example, str(’{:05b}’.format(10)) keeps five numbers.

Conditional statements 

  • Python does not have Switch. Use If .. elif ... else.
  • Beware the syntax, If var==0 or var==1 (correct), If var==0 or 1 (wrong)

Class  A class can be defined as follows,

class ClassA(ClassB):  
    def __init__(self):
       do something
       super().__init__(arg):

The first line constructs Class A inherited from Class B, and there is no optional argument. In the second line, __init__(self) constructs an instance of Class A. In the last line, super().__init__(arg) construct an instance of Class B with the argument arg. One should not confuse a class with a function. For more information, read the official documents like inheritance and super.


Misc 

  • One can break a (long) line by inserting \ at the end.
  • Single-line comments can be created by #
  • Multi-line comments can be created by triple quotations (under some limitations)

4. Compiler

Python is slow compared to C, even if we do not use ipynb. A python compiler PyPy may accelerate your computation. However, a lot of python libraries are incompatible with PyPy. See FAQ