Python: An introduction

Introduction

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 IDE

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.

IDLE Python shell.
Figure 1 – IDLE Python shell.

As seen, IDLE presents the Python shell by default, where you can send commands.

Hello World

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.

Python hello world
Figure 2 – Hello World in Python.

This is very simple compared to, for example, Java, where you would need to write something like:

System.out.println("Hello World");

Or in C, where you would have to write something like:

printf("Hello World");

Scripts

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.

Opening a new scripting file.
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.

python run script
Figure 4 – Running the script.

For the print(“Hello World”) code, the same “Hello World” sentence should be printed in the shell of IDLE.

Libraries

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.

10 thoughts on “Python: An introduction”

  1. Pingback: Flask: Hello World | techtutorialsx

  2. Pingback: Flask: Hello World | techtutorialsx

  3. Pingback: Raspberry Pi 3 Raspbian: Python Hello World with IDLE | techtutorialsx

  4. Pingback: Raspberry Pi 3 Raspbian: Python Hello World with IDLE | techtutorialsx

Leave a Reply

Discover more from techtutorialsx

Subscribe now to keep reading and get access to the full archive.

Continue reading