The objective of this post is to present a quick introduction to the Python programming language.
Python is a very easy to use language, which is why I like to use it to develop programs for quick proofs of concept (for example, creating a simple server or opening a socket to communicate with a ESP8266).
One big advantage of Python is that there are lots of libraries and modules that offer us easy to use functionalities, a little bit like we have in Arduino.
To install Python, just go here, download the installer and run it (assuming that you are using Windows, if you are on Linux you can also easily install Python from the command line). The differences between versions 2.x and 3.x are explained here.
You can also run some Python code online here, to test the language before installing.
Python comes with a simple IDE called IDLE. After installing Python, search for IDLE and run it. You should see a window similar to the one in figure 1.
Figure 1 – IDLE Python shell.
As seen, IDLE presents the Python shell by default, where you can send commands.
As stated before, Python is a very easy to use programming language. So, to output the famous “Hello World” sentence, you can just type the following in IDLE, in the shell, and press enter:
print (“Hello World”)
The string in quotes will be printed in IDLE’s command line, as shown in figure 2.
Figure 2 – Hello World in Python.
This is very simple compared to, for example, Java, where you would need to write something like:
Or in C, where you would have to write something like:
For more complex programs, it’s easier to write all the code in a file (or multiple files) and run it at once, instead of writing the commands one by one in the command line.
To do it in IDLE, we just go to File -> New File, as shown in figure 3.
Figure 3 – Opening a new scripting file.
In the new window, we just write our code and then click Run -> Run Module, as shown in figure 4.
Figure 4 – Running the script.
For the print(“Hello World”) code, the same “Hello World” sentence should be printed in the shell of IDLE.
One of the big advantages of Python is the big number of libraries/modules available that implement very different functionalities and are easy to install and use. Bellow, there’s a list of some interesting ones:
- OpenCV: Computer vision library;
- NumPy: Scientific computing library;
- ScyPy: A library for mathematics, science and engineering;
- Django: Web framework for rapid development;
- Bottle: Micro web framework;
- Flask: Micro web framework. Check this post for an example IoT application using it;
- Scrapy: Web crawling framework;
- Tkinter: A library for GUI design;
- SQLAlchemy: Object relational mapper for Python.
You can check some other useful modules here. There are plenty of libraries around the web, it’s just a matter of doing a quick search before starting to implement something from scratch, since it’s probable that someone has already created a library in Python for that.
In order to install new python libraries the easiest way is using pip, which is a very easy to use command line installer. Installing libraries with pip usually only requires a command like pip install libraryName.