First Steps

"Take the first step, and your mind will mobilize all its forces to your aid.
But the first essential is that you begin. Once the battle is startled, all that
is within and without you will come to your assistance." (Robert Collier, the author
of self help books)

He also wrote: "The first essential, of course, is to know what you want."We hope you know what you want, and that it means to learn, to understand and to
apply Python.

Aiming in the same direction is a famous quote by J.P. Morgan "The first step
towards getting somewhere is to decide that you are not going to stay where you are."

Breaking the Shell

"Your pain is the breaking of the shell that encloses your understanding." (Kahlil
Gibran, Lebanese poet and artist)
This website is created by:

Python 3

Training Classes

This website aims at providing you with educational material suitable for self-learning.
Nevertheless, it is faster and more efficient to attend a "real"
Python course in a classroom, with
an experienced trainer. So why not attend one of the live
Python courses in Strasbourg, Paris, London, Berlin, Munich, Hamburg, Frankfurt,
or Lake Constance by Bernd Klein, the author of this tutorial?

In-house Training Courses

If you like it, we will come to your company or institute and provide a special training for your employees, as we've done it many times in Amsterdam (The Netherlands), Berlin (Germany), Bern (Switzerland), Basel (Switzerland), Zurich (Switzerland), Frankfurt (Germany), Locarno (Switzerland), Den Haag (The Hague), Hamburg, Toronto (Canada), Edmonton (Canada), Munich (Germany) and many other cities.
We do training courses in England, Switzerland, Liechtenstein, Austria, Germany,
France, Belgium, the Netherlands, Luxembourg, Poland, UK, Italy and other locations in Europe and in Canada.

This way you will get a perfect training up to your needs and it will be extremely cost efficient as well.

The Interpreter, an Interactive Shell

The Terms 'Interactive' and 'Shell'

The term "interactive" traces back to the Latin expression "inter agere".
The verb "agere" means amongst other things "to do something" and "to act", while
"inter" denotes the spatial and temporal position to things and events, i.e.
"Between" or "among" objects, persons, and events.
So "inter agere" means "to act between" or "to act among" these.

With this in mind, we can say that the interactive shell is between the user and the operating
system (e.g. Linux, Unix, Windows or others). Instead of an operating system an interpreter can be
used for a programming language like Python as well. The Python interpreter can be used from an
interactive shell.

The interactive shell is also interactive in the way that it stands between the commands or actions and their execution.
This means the Shell waits for commands from the user, which it executes and returns the result of
the execution. After this the shell waits for the next input.

A shell in biology is a calcium carbonate "wall" which protects snails or mussels from its environment or its
enemies. Similarly, a shell in operating systems lies between the kernel of the operating system
and the user. It's a "protection" in both direction. The user doesn't have to use the complicated
basic functions of the OS but is capable of using simple and easier to understand shell commands.
The kernel is protected from unintended incorrect usages of system function.

Python offers a comfortable command line interface with the Python shell, which is also known
as the "Python interactive shell".
It looks like the term "interactive Shell" is a tautology, because "Shell" is interactive on
its own, at least the kind of shells we have described in the previous paragraphs.

Using the Python interactive Shell

With the Python interactive interpreter it is easy to check Python commands.
The Python interpreter can be invoked by typing the command "python" without any
parameter followed by the "return" key at the shell prompt:

A closer look at the output above reveals that we have the wrong Python version. We wanted to use
Python 3.x, but what we got is the installed standard of the operating system, i.e. version 2.7.11+.

The easiest way to check if a Python 3.x version is installed: Open a Terminal. Type in python
but no return. Instead type the "Tab" key. You will see possible extensions and other installed
versions, if there are some:

Of course, "hello" is not a proper Python command, so the interactive shell returns ("raises") an error.

The first real command we will use is the print command. We will create the mandatory "Hello World" statement:

>>> print("Hello World")
Hello World
>>>

It couldn't have been easier, could it? Oh yes, it can be written in a even simpler way. In the interactive
Python interpreter - but not in a program - the print is not necessary. We can just type in a string or a
number and it will be "echoed"

>>> "Hello World"
'Hello World'
>>> 3
3
>>>

How to Quit the Python Shell

So, we have just started, and we already talk about quitting the shell. We do this, because we
know, how annoying it can be, if you don't know how to properly quit a program.

It's easy to end the interactive session: You can either use
exit() or Ctrl-D (i.e. EOF) to exit. The brackets behind the exit function are crucial.
(Warning: exit without brackets works in Python2.x but doesn't work anymore in Python3.x)

The Shell as a Simple Calculator

In the following example we use the interpreter as a simple calculator by typing an
arithmetic expression:

>>> 4.567 * 8.323 * 17
646.18939699999999
>>>

Python follows the usual order of operations in expressions. The standard order of operations
is expressed in the following enumeration:

exponents and roots

multiplication and division

addition and subtraction

This means that we don't need parenthesis in the expression "3 + (2 * 4):

>>> 3 + 2 * 4
11
>>>

The most recent output value is automatically stored by the interpreter in a special
variable with the name "_". So we can print the output from the recent example again by
typing an underscore after the prompt:

>>> _
11
>>>

The underscore can be used in other expressions like any other variable:

>>> _ * 3
33
>>>

The underscore variable is only available in the Python shell. It's NOT available
in Python scripts or programs.

Using Variables

It's simple to use variables in the Python shell. If you are an absolute beginner
and if you don't know anything about variable, please confer our chapter
about variables and data types.
Values can be saved in variables. Variable names don't require any special tagging,
like they do in Perl, where you have to use dollar signs, percentage signs and at signs
to tag variables:

>>> maximal = 124
>>> width = 94
>>> print(maximal - width)
30
>>>

Multiline Statements

We haven't introduced multine statements so far. So beginners can skip the rest of this chapter
and can continue with the following chapters.

We will show, how the interactive prompt deals with multiline statements like for loops.

>>> l = ["A",42,78,"Just a String"]
>>> for character in l:
... print(character)
...
A
42
78
Just a String
>>>

After having input "for character in l:" the interpretor expects the input of the next line
to be indented. In other words: The interpretor expects an indented block, which is the body
of the for loop. This indented block will be iterated. The interpretor shows this "expectation"
by showing three dots "..." instead of the standard Python interactive prompt ">>>".
Another special feature of the interactive shell: When we have finished with the indented
lines, i.e. the block, we have to enter an empty line to indicate that the block is finished.
Attention: The additional empty line is only necessary in the interactive shell!
In a Python program, it is enough to return to the indentation level of the "for" line, the one with
the colon ":" at the end.

Strings

Strings are created by putting a sequence of characters in quotes. Strings can be surrounded by single quotes,
double quotes or triple quotes, which are made up of three single or three double quotes.
Strings are immutable. This means that once defined, they cannot be changed. We will cover this topic in
detail in another chapter.

>>> "Hello" + " " + "World"
'Hello World'

String in triple quotes can span several lines without using the escape character:

>>> city = """
... Toronto is the largest city in Canada
... and the provincial capital of Ontario.
... It is located in Southern Ontario on the
... northwestern shore of Lake Ontario.
... """
>>> print(city)
Toronto is the largest city in Canada
and the provincial capital of Ontario.
It is located in Southern Ontario on the
northwestern shore of Lake Ontario.
>>>

There is a multiplication on strings defined, which is essentially a multiple concatenation: