Python for Beginners

What is Python?

  • The Language:
    • A modern, robust, high level programming language created by Guido van Rossum.
    • First appeared in 20 February 1991.
    • Official website: http://www.python.org
    • Two incompatible versions:
      • Python 3
      • Python 2      <--- will die in 2020!
  • The Implementation:
    • CPython (the standard, written in C)     <--- you'll probably use this one.
    • Pypy (written in RPython)      <--- ~7.5x faster than CPython! Still immature for general usage.
    • Jython (for Java platform)
    • IronPython (for .NET Framework)
    • ...

Basic Setup

Python should come pre-installed by default on most Linux distros.
However it might not be the version you want to (or should) use.

  • Should I use Python 2 or Python 3?
    • If you are a new user, go for Python 3!
    • If you are using Python 2 already, please try to upgrade your scripts to Python 3.
    • In this tutorial I'll focus on Python 3.
  • Check which version of Python you have installed on your system:

    python --version
      Python 3.6.0
    

    Great, I have Python 3! If you see something like:

    python --version
      Python 2.7.13
    

    it means that python points to Python 2. In such case, try this:

    python3 --version
      Python 3.6.0
    

    If the last command returns an error, then please install Python 3:

    sudo apt-get install python3
    
  • From now on make sure you always run your scripts with Python 3 by typing:
    python3 myscript.py
    
    instead of (maybe Python 2 or 3):
    python myscript.py
    

IPython & Jupyter notebook

Make sure you install a version compatible with Python 3:

sudo apt-get install ipython3 jupyter
  • Note: if your system's repository doesn't provide the jupyter package yet, try to install the ipython3-notebook package instead.

ipython

A powerful interactive Python shell.

  • To launch an ipython session from a terminal:

    ipython
    
  • ipython is great for doing some experimentation or discover something new about the language.

  • To exit the session just press Ctrl-D or close the terminal.

  • Note: After exit your entire session will be lost!

jupyter notebook

Jupyter is the evolution of ipython ...eh, like a Pokemon! :-p

  • To launch a jupyter notebook (or ipython notebook) server from a terminal:
    ipython notebook
    
    or
    jupyter notebook
    
    It'll run on a webbrowser. We are using jupyter right now! :-)
    We can save our work and keep it organized on individual notebooks!

Installing Additional Modules/Packages

Always prefer to install things from your system's repository!

  • Let's install a few usefull packages:
    sudo apt-get install python3-numpy python3-scipy python3-matplotlib
    

What if a package I need is not in the repository?

  • If you have no choice, then first you have to install pip:
    sudo apt-get install python3-pip
    
    and use pip to install additional modules.
  • Note: When installing things with pip, never use sudo! I mean, don't do this:

    sudo pip install modulename

    or you might risk breaking your system!!!

  • Instead, you should pip-install modules locally, which is safe and you won't mess with your system.

    For example:

    pip3 install --user modulename
    

    will install the modulename package under ~/.local/lib/python3.X/site-packages

    To uninstall it:

    pip3 uninstall modulename
    

Additional Resources