Python: An introduction

The objective of this post is to present a quick introduction to the Python programming language.

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 IDE.png

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.

python-script

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.

Advertisements
This entry was posted in Python and tagged , , . Bookmark the permalink.

9 Responses to Python: An introduction

  1. Pingback: techtutorialsx

  2. Pingback: Python: Parsing JSON | techtutorialsx

  3. Hi antepher! I love Python as well. Seems like your using an out of date version of Python. Upgrade to the latest version of Python 2 at python.org/getit. You can also get Python 3. Get it at python.org/getit. Cheers! Epic Chas Gamer 😀

    Liked by 1 person

  4. Hi again antepher! Good luck with your blog! When I said you are using an out of date Python 2, I mean there is a mini update to Python 2: 2.7.9. I think it is just a bug fix update, but it is worth it. Cheers! Epic Chas Gamer 😎

    Liked by 1 person

  5. newsordice says:

    Hi antepher, thanks for the great post. I’m looking at learning some basic Python in the near future and your information above will certainly be useful, especially your list of libraries. Thanks!

    Liked by 1 person

    • antepher says:

      Thanks for your feedback 🙂 Python is a very powerful but simple to learn language, it’s a very good choice to learn it. I’m planning on doing some more tutorials soon, stay tuned.
      Best regards

      Liked by 1 person

  6. Pingback: Flask: Hello World | techtutorialsx

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s