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
- Download and install the Python setup from Python Releases for Windows
- Download Eclipse or a similar IDE and follow this tutorial for setting up Python on Eclipse
- 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
- Use the
- 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
- Install
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:
Computer Vision¶
Cloud Intergration¶
- Amazon Web Services (https://aws.amazon.com/python/)
- Google Cloud (https://googlecloudplatform.github.io/google-cloud-python/)
GUIs (Graphical User Interfaces)¶
- PyGObject (https://pygobject.readthedocs.io/en/latest/)
- tKinter (https://docs.python.org/2/library/tkinter.html)
- wxPython (https://wxpython.org/)
Data Science & Scientific Computing¶
- NumPy (http://www.numpy.org/)
- SciPy (https://www.scipy.org/)
- pandas (https://pandas.pydata.org/)
- parquet (https://arrow.apache.org/docs/python/parquet.html)
Interactive Python¶
- IPython (http://ipython.org/)
- Jupyter Notebook (http://ipython.org/)
Games & Simulations¶
- Pygame (http://www.pygame.org/news.html)
- Pyglet (http://www.pyglet.org/)
Machine Learning¶
- TensorFlow (https://www.tensorflow.org/install/)
- Keras (https://keras.io/)
Networking¶
- Twisted (https://twistedmatrix.com/trac/)
Plotting & Data-visualization¶
- matplotlib (https://matplotlib.org/)
- seaborn (https://seaborn.pydata.org/)
- plotly (https://plot.ly/)
Web Scraping¶
- BeautifulSoup (https://www.crummy.com/software/BeautifulSoup/)
- Scrapy (http://www.scrapy.org/)
Miscellaneous¶
- pint (https://pint.readthedocs.io/en/latest/)
- Define, operate and manipulate physical quantities