Get up to 80 % extra points for free! More info:

Lesson 1 - Introduction to the Python language

Welcome to the first lesson of the Python course. We'll go through step by step, from the very beginning to the more complex structures, object models, form applications, etc. With a little patience and persistence, you will become a good programmer.

To fully understand the Python language, we'll have to look to the past and get a good understanding of how programming languages have evolved over the course of time. Doing so will enable us to understand how Python works.

Evolution of programming languages

1st generation languages ​- Machine code

Computer processors can perform a limited number of simple instructions, which are stored as a sequence of bits, i.e. numbers. In most cases, the aforementioned instructions are written using the hexadecimal system, so as to make reading them less of a chore. However, the instructions are so limited, that all you can really do is sum up addresses and jump between instructions. As you may already know, in the world of programming, one does not simply add two numbers together. What we do, is look at the numbers' addresses in memory and then sum them up (which takes multiple instructions). Here's what adding two numbers would look like in the hex:

2104
1105
3106
7001
0053
FFFE
0000

The instructions are given to the processor in binary. This sort of code is extremely unreadable and is dependent on the instruction set of the given CPU. I assure you, it is extremely nauseating to program in this "language". Unfortunately, every program must be compiled in binary format so that it can be executed by a computer processor.

Assembly language principle - Python Basic Constructs

2nd generation languages ​​- Assembler

Assembler (ASM for short) is no simpler than machine code, but at least it's human readable! Here, the instructions have human readable text codes, so that people wouldn't have to memorize every single one of the number combinations. The instruction codes are later compiled into binary code. Adding two numbers up in ASM would go something like this:

ORG 100
LDA
ADD B
STA C
HLT
DEC 83
DEC -2
DEC 0
END

It's a bit more human-readable, but most people, including me, would still have no clue how this program works.

3rd generation languages

Third generation languages finally give a good amount of abstraction of how the program is seen by the computer. Rather than forcing us to adapt to the computer's arcane way of thinking, the languages focused a bit more on how we see the program. Numbers were then perceived as variables and code had an almost "mathematical-notation" sort of aesthetic.

Adding up two numbers in the C language would go like this:

int main (void)
{
    int a, b, c;
    a = 83;
    b = -2;
    c = a + b;
    return 0;
}

Pretty much anyone could assume what this program does just by looking at it. It sums 83 and -2 up, and stores the result in a variable named c. The main advantage third generation languages had over all of the previous languages was high readability.

As time went on and code optimization was in demand, object-oriented programming was brought into play, which we will get into later. Third generation languages are essentially divided into the following categories:

Compiled languages

Compiled aka unmanaged languages have their source code in a language that people can fully understand. The source code must still be translated into machine code so that it can be executed by the processor. This translation is provided by a compiler, which compiles the entire program into machine code.

Compiler language principle - Python Basic Constructs

Compiled languages have the following advantages:

  • Speed - The program only slows down during the one-time compilation. Once a program is compiled, it runs as quick as, or even quicker due to compiler optimizations, a program written in ASM.
  • Inaccessibility of source code - the program is distributed in the compiled form, which makes modifying it very difficult if you don't have the source code.
  • Easy to detect errors in source code - If there is an error in the source code, the entire compilation process crashes, and the programmer gets to see where he/she messed up. This greatly simplifies software development.

There are, as you may have guessed, disadvantages as well:

  • Platform dependency - the program is still platform-dependent, i.e. on the processor or operating system. We cannot take a pre-compiled program, and run it on another platform without recompiling it and tweaking it a bit.
  • Inability to edit - Once the program is compiled into the machine code, you cannot edit it any other way, only by re-compilation. That also applies to the languages mentioned above.
  • Memory management - Due to the fact that computers mechanically execute instructions, you may occasionally run into memory overflow errors. Compiled languages don't ​​have automatic memory management, so they're more of a hassle. Run-time errors are caused mainly by manual memory management, which cannot be detected by compilation.

Examples of compiled languages include the C language, its object-oriented successor C++, and Pascal/Delphi.

Interpreted languages

Interpreted languages make an attempt to solve program portability issues, and make programmers' lives a bit easier. These are mostly modern languages and Python is one of them. Interpreters work much like compilers do, but instead of translating the entire program all at once, they only translate what is needed at a given moment in time. Its name comes from the human profession of Interpretation. Where an interpreter is someone who listens and serves as a "middle man" for people who do not speak the same language. In other words, he/she translates what each person says to a language that they understand. The translation is done while each one speaks. Interpreted languages work in pretty much the same way. The source code is read line by line, compiled into machine code, executed, and then thrown away.

Interpretation is not the fastest way to run programs for sure, however, it adds several significant advantages. The results can also be cached to improve speed and nowadays, the code readability and quality matters more that the performance. It's because our computers are very fast, but applications become more and more complex and it's easy to make errors in them, especially when using old programming languages.

Interpreter language principle - Python Basic Constructs

Advantages of interpretation are:

  • Portability: The program is fully portable. If the platform has an interpreter, our program will be able to run on said platform (developing an interpreter is much simpler than developing a compiler).
  • Simpler development - We no longer have to deal with manual memory management. All of that is done for us by what is known as the garbage collector (we'll get into that and more in the advanced courses). In some languages, and in Python as well, we don't even have to specify data types, which usually leads to more comfortable data structures.
  • Stability - Due to the fact that the interpreter actually understands the code, it spots errors that would eventually be executed by compiled programs. Interpreting programs is, without a doubt, safer than compiling them. Also, using this type of language brings reflection into play, where the program examines itself during the run-time (more on this later on in the courses).
  • Easy editing - We can write programs in parts, and upload them to the target destination whenever we want because the code doesn't need to be compiled. In other words, it can easily be edited on the fly.

The source code of that adding program in Python would look like this:

a = 83
b = -2
c = a + b
print(c)

Notice there is only the core algorithm without any other syntax, we don't even specify variable types. Can things be more simple? :)

As you may expect, interpreters have three major disadvantages:

  • Speed - Interpretation can be very slow at times, and the program wouldn't use your computer to its full capacity. This can be improved a lot, however, if you plan to program something hungry for resources, some compiled language would be a better choice.
  • Difficulties in finding errors - Due to compilation being done during run-time, errors won't pop up before the code is executed, which can be sometimes annoying. However, we usually still make fewer errors than in compiled languages.
  • Vulnerability - Since the program is distributed as source code, anyone and everyone can alter or even steal parts of it.

There is also a combination of compiled and interpreted languages - virtual machine. It improves speed a little with maintaining some interpretation advantages. You can read about e.g. it in the introductions to the Java or C# languages.

As a result of this information, you should come to understand that Python is a modern programming language. It isn't the fastest, but the source code of our applications will be shorter and have less errors than older, compiled languages.

Python 3 and Python 2

What can be a bit confusing for beginners is that there are 2 supported Python versions:

  • Python 2.x
  • Python 3.x.

The development team changed some language syntax in version 3. The most remarkable difference is that you now need to write parentheses when calling functions. These changes made lots of people stick to version 2. It might be because there are lots of libraries written for Python 2 that are still in the process of being ported to Python 3. However, Python 3 is more modern. It uses Unicode by default and there are many improvements. It's often recommended to start with Python 3 unless you're planning on working on a platform or with libraries that are currently only available for Python 2. Even then, going back to 2.x isn't that hard.

Other Python implementations

There are also multiple implementations of the Python interpreter used to cooperate with other programming languages. You may be interested in them later on. The most important are:

  • PyPy - The faster Python implementation
  • IronPython - Python based on .NET (C#)
  • Jython - Python implementation in Java
  • Boost.Python - Cooperation with C++

Now, we know what we're going to work with. In the next lesson, IDLE and the first Python console application, we'll work with IDLE to create our first program.


 

All articles in this section
Python Basic Constructs
Skip article
(not recommended)
IDLE and the first Python console application
Article has been written for you by David Capka Hartinger
Avatar
User rating:
20 votes
The author is a programmer, who likes web technologies and being the lead/chief article writer at ICT.social. He shares his knowledge with the community and is always looking to improve. He believes that anyone can do what they set their mind to.
Unicorn university David learned IT at the Unicorn University - a prestigious college providing education on IT and economics.
Activities