MBTechWorks.com    

Python Programming Overview

January 15, 2018

Python logo

History

Python is one of the most influential programming languages of our time. It has risen to become one of the top 5 most popular languages in use across many hardware platforms and operating systems.

Started in 1989 by Guido van Rossum at the Centrum Wiskunde & Informatica (National Research Institute for Mathematics and Computer Science) in the Netherlands, its focus was on readability and code conciseness. This made it an ideal language to learn for beginning programmers. However, its simplicity did not limit its capabilities.

Designed to include experience and lessons learned from other languages, it uses contemporary methods and concepts such as dynamic typing, automatic memory management, object-oriented constructs and supports imperative, procedural and functional programming models. It is also an "interpreted" language, meaning that the source code itself is executed, rather than having to first compile it into machine code.

Python 2 versus Python 3

Python version 2 was released in the year 2000 with major new features over version 1 and quickly became the dominant version of Python. Considerable development has been done with this version and is still in wide use today. Version 3 was released in 2008 with further enhancements over version 2. Unfortunately, Version 3 is not backwards compatible with Version 2 - that is Version 2 source code will not necessarily run in Version 3.

So, which version should you use? If you are implementing or working with an existing version 2 application, then work at that level; or covert it to version 3. Otherwise, the future is in Version 3 and beyond, so start there!

Programming

To create a Python program, type the code (called "source code") into a text editor or an IDE (Integrated Development Environment). Included in the Raspbian operating system for the Raspberry Pi are several applications that can be used. Raspbian's Text Editor will do. This is a simple way to get started, but does not offer syntax highlighting and other services that an IDE offers. At time of this writing, Raspbian includes three IDEs for Python programming.

  1. Python 2 (IDLE) - Integrated DeveLopment Environment for python version 2.
  2. Python 3 (IDLE) - Integrated DeveLopment Environment for python version 3.
  3. Thonny - an IDE that is ideal for beginners.

Once the source code is saved from either the Text Editor or one of the IDEs, it can be executed (run) from either the command line terminal (LXTerminal in Raspbian), or from within the IDE from the Run option.

Indentation

Blocks of code are grouped by indentation with one or more spaces as in the following (illustrative, not actual Python code):

  1|  Code Block 1
  2|    some code
  3|    some more code
  4|  Code Block 2
  5|    some code
  6|    some more code
  7|  More code . . .

Each line of code with the same left-to-right horizontal indentation is related and in the same code block. (Note, the numbers and | are not part of Python code and only used for illustrative purposes.)

An actual Python code example below. This example adds two numbers into result. It then uses an "if" statement to conditionally execute the print line based on whether result is greater than 8 or not.

  1|  num1 = 5
  2|  num2 = 10
  3|  result = num1 + num2  
  4|  if result > 8:
  5|    print("result is greater than 8")
  6|  else:
  7|    print("result is less than or equal 8")

Blocks of code may also be contained within other blocks as in:

  1|  Code Block 1
  2|    Sub Block A
  3|      sub block code
  4|      more sub block code
  5|    some code 
  6|    some more code
  7|  Code Block 2
  8|    some code
  9|    some more code
 10|    and so on

Lines 3 and 4 are part of Sub Block A. Lines 5 and 6, as well as Sub Block A at line 2 with its code, are part of Code Block 1. It is important to note that each group of code ends, or is delimited, when it encounters a line of code that is less indented.

When programming in Python, care must be taken to align code with the correct space indentation. A single incorrect space can cause program errors or produce invalid results.

Python Program Example

The following is a simple example of Python source code used in our Quick Start. It prompts for input of two numbers, a math operator, and then calculates and prints (displays) the result. Comments within the source code follow the "#" character.

Python source code example image
Article closing text image