Python Basics

What is Python?

Python is a programming language that lets you work quickly and integrate systems more effectively. We will be using it for programming the Raspberry Pi, data aggregation, data transfer and data anaylsis. Python 2.7 and Python 3 are the most popular versions of Python.

Installation

Windows
Mac
  • Python 2.7 comes pre-installed with the Mac OS X 10.8 +.
  • To install and use other versions of Python on a Mac, use the tutorial on Using Python on a Macintosh
Linux
  • Python (2.7 and 3.4) usually comes preinstalled with major distributions of Linux. You can test if Python is installed using the following commands in the terminal:
python --version
python2 --version
python3 --version
  • If you get a message saying no command found or package is missing, you can install it using:
sudo apt-get install python
sudo apt-get install python3

HelloWorld with Python

Create a new file called helloworld.py using the IDE for Windows/Mac or using nano on Linux and enter the following Python code:

print("HelloWorld!")

Save and run the file. On the IDE it would be via clicking a Run Python Script button and via terminal you need to type python helloworld.py. The output should simply be the following:

HelloWorld!

Installing Python Modules

What makes Python so powerful is the plethora of packages made to allow a programmer do a lot of things like web-parsing, plotting, simulation, computer vision, machine learning or simply getting the weather. Use the official guide for Installing Python Packages to get things set up.

Windows
  • Use the py Python launcher in combination with the -m switch:
py -2   -m pip install SomePackage  # default Python 2
py -2.7 -m pip install SomePackage  # specifically Python 2.7
py -3   -m pip install SomePackage  # default Python 3
py -3.4 -m pip install SomePackage  # specifically Python 3.4
Mac / Linux
  • Install pip which is a Python Package Installer
sudo apt-get install python-pip
sudo apt-get install python3-pip
  • Install Python modules using pip:
pip2 install SomePackage # short hand installation for Python 2
pip3 install SomePackage # short hand installation for Python 2

# or

python2   -m pip install SomePackage  # default Python 2
python2.7 -m pip install SomePackage  # specifically Python 2.7
python3   -m pip install SomePackage  # default Python 3
python3.4 -m pip install SomePackage  # specifically Python 3.4

Note

If you get an Permission denied while using pip, you can append the command with --user. Example: pip install matplotlib --user. It is not recommended to use sudo to install packages using pip.

Note

It is highly recommended to install the Python module called IPython. It significantly improves upon the vanilla version of Python command line (terminal) interface.

Useful Modules

The official list of useful modules does not begin to cover the vast number of modules available for different tasks, but it is a good place to start. Some of them are listed below:

Interactive Python

Games & Simulations

Machine Learning

Networking

Plotting & Data-visualization

Miscellaneous