Python 3 green and yellow

Python Tutorial

This tutorial provides a comprehensive and in-depth introduction to the Python language. Python version 3, which is the latest and the correct one! It differs from other tutorials in that it provides many examples, exercises and many easy-to-understand charts and graphs. The course is aimed at both beginners and intermediate to experienced programmers or developers who are looking for differences to other programming languages.

A main focus in the creation of this tutorial is that the content is suitable for self-study.

Yes, Python is an easy to learn programming language, easier than many other programming languages. Still, many need a little extra help and explanation. In this introduction we offer this in a form that is both easy to understand, but also never loses sight of the necessary technical depth. The depth that especially experienced programmers are are looking for and are interested in. For newbies, it is also important that they learn the interrelationships and special features of Python right from the start.

This online Python course was created and is maintained by Bernd Klein, an experienced Python trainer, giving training classes all over the world. Melisa Atay has been helping to maintain the website since the beginning of 2020. Among other things, she made sure that all pages now have syntax highlights in the code. We would like to take this opportunity to thank the numerous people who help us around the world. People who kindly point out discrepancies or submit suggestions for improvement. Thank you all very much!


September 2020:
In my new article "Encoding Text for Machine Learning", I show how texts, like newspaper articles, books or the like, can be converted into numerical values. You need this conversion if you want to edit natural-language texts with neural networks. This is done with the help of the bag-of-words method. I also added a chapter in which I present some interesting text classification examples: "Natural Language Processing: Examples" (Bernd)

September 2020:
One of the most visited chapters of my Pyhton tutorial has the title Recursion and Recursive Functions. Have you ever heard about tribonacci, tetranacci, or pentranacci numbers? Most probably not. I haven't known them until this day. (Bernd)

August 2020:
I have improved the tutorial on functions. A smoother introduction, i.e. the call behavior and the parameter transfer are now introduced in easy-to-understand diagrams. Furthermore a lot of additional exercises with solutions round off the topics. Concerning Machine Learning: I added a new chapter on metrics (precision, accuracy, recall, f1-score)(Bernd)

August 2020:
Today I finally took the Python2 tutorial offline. Python2 should finally be history. I also reorganized the existing content. Up to now there was an "Advanced" rubric, which did not provide actual Python topics but applied Python programming. I have renamed it to Applications. I have also added a new section "Advanced". This time with advanced Python topics. Object orientation received a section of its own now. (Bernd)

June 2020:
I added three new chapters in the section "Machine Learning": Finally, I added a proper introduction chapter, "Representation and Visualization of Data" and "Train and Test Sets" (Bernd)

June 2020:
We are proud to present a new Turkish sibling of our website. Thanks to the work of Melisa Atay and Barbaros Akkurt.

June 2020:
I (Bernd) revised the chapter "Python Iterators and Generators"

May 2020:
Bernd changed the chapter on "Magic Methods and Operator Overloading". He added further examples of the __call__ method and sharpened the explanations a bit more.

April 2020:
Great news: The listings in the Python tutorial are getting more colorful. Melisa Atay works hard to turn the existing html files into Jupyter Notebooks, which get automatically transformed into html. This way the code listings get syntax highlighting. She also checks the tests and examples by doing this.

February 2020:
Bernd Klein finally finished two new chapters of my machine learning tutorial:

November 2019:
After having worked so much on Numpy, Matplotlib and Pandas, I (Bernd) concentrated on pure Python. I continued the journey into the the depth of properties. You can join me: "Deeper into Properties" One step further: "Descriptors"

October 2019:
I added a new chapter to the Pandas Tutorial: Replacing Values in DataFrames and Series

September 2019:
I reshaped most of the chapters of my Matplotlib Tutorial:

August 2019:
It was long overdue. Finally we have a chapter on pytest

Juli 2019:
I wrote a new chapter with an extensive example of multiple inheritance in Bursa and Istanbul and did the final touches on a train from Geneva to Zurich. The underlying ideas have arisen while giving Python courses in various locations. The examples were nearly finished as well. All I did in Turkey was writing the explanatory text.

June 2019:
The Chapter on Inheritance has been nearly completely rewritten. Instead of using the old and boring "employee" and "person" classes, we are now using the Robot, which we have also used in our Introduction into OOP. You will meet the robot Marvin and his new friends "James" and "Dr. Frankenstein"!

April 2018:
A completely new chapter An Extensive Example for Sets was added to our tutorial. This chapter is supplementing the chapter Sets and Frozensets

October 2017:
In our chapter on Polynomials we demonstrate how easily and beautifully a class for the creation and manipulation of polynomial functions can be written in Python.

January - March 2017:
We extended our chapters on Generators and Decorators

May 2016:
New chapter on Decorators. In combination with our chapter on Memoisation and Decorators it belongs to the most extensive treatisises on the topic of decoration à la Python!

December - March 2016:
We wrote four chapters dealing with abstract classes as a tutorial on Metaclasses: August 2015:
We added a chapter on Slots and another about the difference between type and classes.

July 2014:
An introduction into using database interfaces in Python for SQL, MySQL and SQLite

March 2014:
We are currently completely revising the chapter on object oriented programming. It's more or less complete rewrite. The old version dealing with OOP can still be accessed, though we recommend to work through the new ones.
The topic now comprises five instead of previously only one chapter:

Tutorial in hard copy

There is no PDF version available, but you can create it yourself. You can use the print functionality of your browser to do this. Use "Print to File" and you will get a nicely formatted version of a chapter.


Thank you very much for using this tutorial! We hope that you will enjoy learning Python with us!

Any Help is Welcome!

Though we do our best to prevent errors, we need your help to ensure that all the information presented in this tutorial is correct and up to date. If you find spelling or grammatical errors, it will be great if you will point them out to us so that we can fix them! We are continually improving this website and your help will assist us in making it the best tutorial! The same is true, of course, if you find logical problems or errors in the text or the code examples. We hope that there are only a few of them left in the text! But, as the saying goes, nobody is perfect! Please use the contact button!

Advanced Topics

System Programming with Python
Python has various modules to support system focused programming. The sys module is introduced in the first chapter. A focal point are the data streams (stdin, stdout, stderr) and redirections of streams. The interaction between is the focus in the following chapter of our course.
The interaction between Python and the Linux Shell is another topic of our advanced section. This chapter is followed by Forks and Forking.

You can learn more about threads and threading in our Introduction into Threads. We show how to find the active IP addresses in a local network by using forks.

We demonstrate in "Pipe, Pipes and '99 Bottles of Beer'" how to write a program which is construing the famous American song "99 bottles of beer" by using forked processes and Pipes. So, if you need a good example of pipes and forks working together you will find it here.
Graph Theory
We have three chapters dealing with Graphs.
  • A general introduction into the Graph theory and the corresponding Python code can be found in "Graphs in Python" You will also here the implementations of a graph class with essential functionalities for graph creation, manipulation and calculations.
  • Introduction into the module pygraph
  • Introduction into the module NetworkX
Computer Science and Computer Linguistics
Finite State Machines are not only used in computer science but in natural language processing as well. We cover the concept of the Finite State Machine in great detail, so that even an amateur in Computer Science can understand the examples. At least we hope so.

Alan Turing's Turing Machines and above all the underlying theory is a must for every computer scientist. We show a simple implementation of a Turing Machine.

If you are interested in Classifying documents, the Introduction into Text Classification using Naive Bayes and our Python Implementation of Text Classification will be the right chapters for you.
Numerical Computations with Python
If you want to get efficient and fast results with arrays and matrices, the NumPy module of Python is definitely the right tool collection for you. You will find answers to your questions in our tutorial, i.e. in our chapters "NumPy Module" and "Matrix Arithmetic".

We also provide an introduction into Linear Combination
It's also possible to create scores with Python: You can find a complete working example in Creating Musical Scores With Python

If you feel that the above topics are too complicated or sophisticated for you, you might like our course for beginners in Python. You find a documented link list in the following lines:
Databases with Python
An introduction into using database interfaces in Python for SQL, MySQL and SQLite
What you find are not real games! We show a recursive solution to Towers of Hanoi and a game Cows and Bulls better known in a commercial version called "Mastermind".

Classroom training Courses

This tutorial is, as we have already mentioned, intended for self-study! But some people need to learn Python very quick or prefer to learn in a classroom with an experienced trainer. You may consider visiting one of the courses by Bernd Klein, the author of this tutorial.

Our Next Training Courses

Our next open Python classes with Bernd Klein, the author of this website:

Due to the corona pandemic, we are currently running all courses online. Further Information!

Python Intensive Course
21 - 25 Aug 19
Python Course: Level II
7 - 11 Nov 19

Python Course for Data Analysis and Machine Learning
10 - 14 Aug 20 31 Aug - 4 Sep 20
Python Intensive Course
14 - 18 Dec 20
Python Text Processing and Computer Linguistics Course
14 - 18 Dec 20

Python Intensive Course
16 - 20 Nov 20

Lake Constance / Zurich:
Python Intensive Course
7 - 11 Sep 20 28 Sep - 2 Oct 20 16 - 20 Nov 20
Python Course for Data Analysis and Machine Learning
10 - 14 Aug 20 31 Aug - 4 Sep 20

Munich / München:
Python Intensive Course
15 - 19 Jun 20

Python Course: Level II
2 - 6 Dec 19

Python Course for Data Analysis and Machine Learning
31 Aug - 4 Sep 20