Python Cheatsheet – Basic Syntax and Usage

This Python Cheatsheet summarizes basic syntax and usage on how to get started with Python. The cheatsheet is organized in ten blocks, corresponding to the usual steps of learning of a programming language: installation and tools, how to create and run scripts and notebooks, how to use variables, perform calculations with operators, use if-else, loops, data structures, functions, classes, packages. To run the code examples, use your current Python installation with Jupyter Notebook, Visual Studio Code, Spyder or choose one of the many available online Python environments, such as Google Colaboratory, Online GDB, ...

Also, you can test your basic understanding of Python programming and language syntax by taking the quiz "Python Basics"!

 About Python

Python is a versatile programming language used for data analysis and machine learning, while also offering frameworks for developing graphical user interfaces and web-based applications.

Python is a procedural language, meaning that programs are structured using functions, and can also be used in an object-oriented way, simply put: programs can be structured using classes. Consequently, Python has become a popular programming language for schools who need to teach both programming approaches, without the hassle of using two different languages.

The ease of use of free Python packages for data management, modeling and analysis Numpy, Pandas, Scikit-Learn, Keras and Tensorflow make Python an attractive choice for students and engineers who need to implement applied data science projects.

1. Prerequisites: Installation and Tools

Spyder Visual Studio Code Jupyter Notebook Anaconda pip conda

In order to write Python programs, you need three things: 1. a current Python installation, 2. a Python development environment (IDE), 3. additional packages, depending on what you need to program.
1. Python installation: Python can be downloaded from the Python website, the installation is straight-forward, after which the interactive Python console can be used.
2. Python IDE: Python development environments such as Spyder, Visual Studio Code or PyCharm and many others offer additional functionality for writing, debugging and running Python programs.
3. Packages: You will need additional packages for your program. Python packages are installed using the command-line packaging tools pip or conda. Pip works only for Python, while Conda works for other languages too, such as the statistical language R. If you prefer to use a graphical user interface for package management, the web-based packet manager Anaconda is a good choice.

1-1 Web-based Tools
Run Python in the cloud

For those who want jump right into Python programming without the hassle of learning to use a fully-fledged development environment, there are several light-weight web-based Python environments that can be installed locally or used in the cloud.

Jupyter Notebook

Jupyter Notebook is a web-based environment that supports the interactive creation, documentation and sharing of Python scripts, especially in the field of data science. In a Jupyter Notebook you can write and run code, visualize data, and also share this code with others. What is special about Jupyter Notebook is that code and documentation are written in independent cells, so that individual blocks of code can be executed individually. Depending on the context, the Jupyter Notebook is the web-based environment in which the notebooks are created, modified and run, or the interactive document ("Notebook", "Jupyter Notebook") that you have created in it. Jupyter Notebooks have the file extension ipynb, e.g. Python-Tutorial.ipynb. If you open an *.ipynb file with a simple text editor such as Notepad++, you will see that it is a JSON file, a human-readable data exchange format that is widely used in web applications for data storage.

Google Colab

Google Colaboratory is a web-based Python environment provided by Google that supports data science projects for free, and is essentially an enhanced version of Jupyter Notebook. Students and data scientists use it because no installations are required, and you get free GPU for ressource-intensive training tasks.

1-2 Python IDEs

For the development of larger data science projects, the package management platform Anaconda and the development and runtime environments Spyder, Visual Studio Code or PyCharm can be used, which provide extensive functionality for package management, software development and presentation.


Spyder is a Python development environment used primarily in data analysis projects. The current Spyder version 5.x has a modernized and more user-friendly interface compared to the previous version 4.0. Development in Spyder is project or folder based, meaning you can organize multiple Python scripts either in a project or as a loose collection in a folder.

Visual Studio Code

Microsoft's Visual Studio Code is a minimalist development environment that supports different programming languages ​​across platforms, including Python. The appropriate support is installed here through extensions. In particular, the Jupyter extension was installed for this tutorial in order to be able to structure the code into code blocks and run them separately in the interactive console. Development in Visual Studio Code is folder-based, i.e. you create a folder and scripts that belong together in it.

2. First Steps in Python

Module *.py Notebook *.ipynb help import --version pip Indentation

Before programming, it is useful to know how to write and run Python scripts or Jupyter notebooks, as well as some system commands to get further information and help.

2-1 Create a Python script or Jupyter Notebook

A Python script or program consists of a single text file with the extension *.py, for example, also called a Python module, which contains contains the statements of the program as well as comments. A Jupyter Notebook has the ending *.ipynb, for example HelloWorld.ipynb and is created using the Jupyter Notebook web-app.

2-1 Hello World in Python

This minimal Hello World program contains single-line comments (introduced with #), and a print-statement. A Hello-World program is the smallest runnable program in a programming language and prints the text "Hello World" to default output (here: the console).

# (2) First lines in Python
# (2-1) Hello World program in Python
print("Hello World!")

"Hello World!"

Larger Python programs are organized in multiple source code files ("modules"), stored in packages or folders, and use functions or classes for their internal structure.

2-2 Run a Python program

To run a Python program from Python source code, you need a Python interpreter. Development environments for Python such as Spyder, Visual Studio Code, PyCharm or Jupyter Notebook contain a Python interpreter and support the coding workflow with syntax highlighting, debugging functionality, etc.

A development environment such as the depicted Visual Studio Code typically has a default configuration of views / panels that support the development process: a file or project browser, a source code editor with syntax-highlighting and auto-completion, different panels for output, problems, console etc. You can run the entire script, or, if it is structured in code cells, you can run only the selected cells.

Visual Studio Code

2-3 Useful tools and system commands
Help and import

With the help-function you get help on syntax, functions and packages from Pythons built-in help system. Just put the name of the function or package in round brackets. With the import statement you import entire packages, e.g. import pandas or selectively classes / functions / constants from packages, e.g. from numpy import linspace.

import pandas
from numpy import linspace, arange
help(linspace); help(arange)
Find out Python and packages version

Knowing your version of Python and installed packages is important to prevent compatibility issues in your code.

Find out Python version from command line

Firstly, you can find out the version of your Python installation from the command line (cmd.exe).

python --version
Find out Python version in your code

Secondly, you can find out the version of your Python installation from within your Python code by using the function python_version() from the package platform

from platform import python_version
Find out version of installed packages

Finally, you can find out the versions of your installed packages using pip-tool: pip list gives you a list of all installed packages, pip install package_name installs a package, pip install package_name --upgrade upgrades a package to the last version

pip list
pip install package_name --upgrade
2-4 Statements, Comments, Indentation

A Python program is made up of statements and comments. Statements are: variable declarations, assignments, calculations, conditional statements, loops, function calls.


Python comments serve to improve the readability and comprehensibility of source code, they are not executed by the system. Single-line comments begin with a "#" symbol followed by text. Multi-line comments must be surrounded by three double quotation marks at the beginning and end.

# Single-line comment
# Another comment


Python uses indentation and spaces to define blocks of code, i.e. to indicate related commands. Related code blocks typically appear in multiline commands such as in if-else, in loops, functions or classes. For example, the print-statements following the colon in line 2 belong to the if-part of an if-else statement and must be indented with the same number of spaces. The default number of spaces used for indentation is 4, but any number of spaces can be used, for example 2 or 3 is also allowed.

# Correct: line 3 and 4 are indented
if 4 > 0:
    print("4 is greater than 0") 
# Incorrect: line 4 is indented
# only with 3 spaces
if 4 > 0:
   print("4 is greater than 0") 

3. Variables, Constants, Operators

Variables Data types Typecasting Calculations

A program usually takes input data (numbers or strings), processes them and returns output data. The data of the program are stored as variables, constants or literals. Calculations with these data are performed using a defined set of operators.

3-1 Declare variables

Variables are named memory locations in which the program's data is stored,for example name, year, x, y. The value of a variable can be changed and overwritten. Python has a built-in type inference, meaning that variables are declared as soon as they are assigned a value, e.g. x = 1, name = "HS KL".


# Assignments: one per row
varname = varvalue
# Assignments: multiple per row
# Assignments: use comma as separator
varname1, varname2 = varvalue1, varvalue2
Define variables by assigning values
# name has data type String
name = "HS KL"   
# jahr has data type Integer
jahr = 2022          
# Assignments: one per row
x = 1
y = 2
# or multiple per row
x, y = 1, 2 # x = 1, y = 2
# Print variable values to standard output
print(name, "\n", jahr, "\n", x, y)

The data type (integer, floating point number, string, etc.) of the variable is assigned automatically by Python and can be found out using the type statement.

Start the Quiz "Python Basics"  
3-2 Declare constants

A constant is a named memory space whose value should not be reassigned. Different from C and Java, Python has no syntactic support for constants such as a const keyword or preventing reassignment. Common practice is to use a variable with capital letters as constant, or to define all needed constants in a separate Python script, so as to make sure that their value is not reassigned accidentally.

# Assignments: one per row
CONSTNAME = constvalue
# Assignments: multiple per row
# Assignments: use comma as separator
CONST1, CONST2 = constval1, constval2
Define constants in their own module

Here we create a file in folder mypkg, where all our constants will be stored.

# mypkg/
MY_PI = 3.14
MY_E = 2.71
from mypkg import myconstants

Yet another workaround for using constants in Python is to define a tuple data structure that contains the needed constants, as described in section 7-2.

Start the Quiz "Python Basics"  

3-3 Data types and typecasting

Python uses internally data types for integers (int), floating point numbers (float), truth values ​​(bool), characters and character strings (str), as well as data structures for lists and sets. Different from C or Java, there is no data type double, instead, floats are stored using double precision (64 bit).

The data type of a variable is usually specified implicitly when a value is assigned, but it can also be specified explicitly by typecasting if required, by writing the desired data type in front of the name of the value and enclosing it in parentheses, e.g. x = float(5) or x = str("Hello!").

# Specify data type explicitly
varname = datatype(varvalue)
# Find out data type of varname
Example 1
Numeric variables and typecasting
# Data type of variable x ... 
# ... is implicitly int
x = 5
print(type(x)) # Output: class'int'
# Data type of variable x ... 
# ... is set explicitly to float
x = float(5) 
print(type(x)) # Output: class'float'
Example 2
String variables

The data type String describes texts or sequences of characters. For a string x you get the first element with x[0], the last element with x[-1], the characters from i inclusive to j exclusive with x[i:j].

x = "Hello!"
print(x[0]) # H
print(x[-1]) # !
print(x[1:6]) # ello!
print(x[-4:-1]) # llo
print(len(x)) # length = 6
x = " Hello from Kaiserslautern "
print(x.strip()) # Strip blank spaces
Start the Quiz "Python Basics"  
3-4 Calculations

Calculations are performed by linking variables to expressions using operators (+, -, *, /, %, ...), taking into account the priority of the operators.

Python has operators for abbreviating assignment (+=, *= etc.), comparison operators (==, !=, >, <, >=, <= ) and logical operators (&&, ||, !). When evaluating an expression that involves comparison or logical operators, the result is one of the boolean values, i.e. True or False.

Example 1
Calculate expressions using different operators

In this synthetic example we calculate expressions using different operators: abbreviating assignment (+=, *=), modulo (%), division (/) and integer division (//).

a, b, c = 25, 3, 0 # a = 25, b = 3, c = 0
# Abbreviating assignment
a += 1 # a = 26
b *= 2 # b = 6
# Modulo-operator
c = 17 % 5; # c = 2
# Division with and without rest
res = a / b + a // b  # res = 4.33 + 4 = 8.33
print("a = %d, b = %d, res = %.2f" % (a, b, res));
# Comparison and logical operators
cond1 =  (a == (b + 20)) # True: 26 = 6 + 20
cond2 = (a < b) # False: 26 is not less than 6
cond = cond1 and cond2
print(cond1, cond2, cond) # True False False
Example 2
Calculate the area of a triangle with sides a, b, c

Here we calculate the area of a triangle with sides a, b, c. We need the sqrt-function from math package to calculate the square root of an expression.

import math
a, b, c = 10, 20, 20
S = (a + b + c) / 2
F = math.sqrt(S*(S-a)*(S-b)*(S-c))
print("Area =", F)
Start the Quiz "Python Basics"  

4. Output and Input

print end sep Formatted output input

The results of a Python script are printed to standard output via the print-function. Standard output for a script is the console or, in a Jupyter Notebook, the area below a cell. For reading input from the console in a Python script, you can use the input-function.

4-1 Output with print()

In Python, output to the console is done using the print function, which has two optional parameters: sep and end, used to specify a separator for the objects and an end character. For formatted output, variables can be inserted into a formatting character string using format characters, in a C-like style.


Print a list of objects with separator and end

print(object1, ...,  objectn, sep = ';', end = '');

Formatted output (C-Style)

print(formatting_string % (variable_list))

Formatted output with print and format

Example 1
Output objects using sep and end
# Output: 5;20.5 // 5:20.5
x, y = 5, 20.5
print(x, y, sep=';', end=' // ')
print(x, y, sep=':')
Example 2
Formatted output

Each data type has appropriate formatting characters: %d or %i for int (integers), %f for float (single-precision floating-point), %lf for double (double-precision floating-point).

# Output: x=5, y=20.5, sum=25.50
x, y = 5, 20.5
sum = x + y
# (1) Formatted output of a text
print("x=" + str(x) + " y=" + str(y) + " sum=" + str(sum)) 
# (2) Formatted output with placeholder (C-Style)
print("x=%d, y=%.1f, sum=%.2f" % (x, y, x + y))
# (3) Formatted output with format
print("x={0:d}, y={1:.1f}, sum={2:.2f}".format(x, y, x+y))
4-2 Input with input()

Input from the Python command console or in a Jupyter Notebook cell is done using the "input()" function. The x = input() command creates an input field, and whatever you type in it is stored in the variable x. If the input is to be interpreted as a specific data type, it must be converted using an appropriate function (e.g. int() or str()). Example: In the following example, two whole numbers are entered and formatted for checking.

# Print prompt text, then read input in next line
var = datatype(input()) 
# Prompt text and input in same line 
var = datatype(input(prompt_text))  
Example 1
Prompt text and input on different lines

# Read the input from console
print("Enter a number")
x = int(input())  
print("Enter another number")
y = float(input())
# Formatted output
print("Your input: x = %d, y = %f" % (x, y))
Example 2
Prompt text and input in same line

# Read the input from console
x = int(input("Enter a number: "))  
y = int(input("Enter another number:"))
# Formatted output
print("Your input: x = %d, y = %d" % (x, y))

5. Conditional Statements

if elif else

A conditional statement is a control structure that determines which of two (or more) blocks of statements, depending on one (or more) conditions. In Python, as in most programming languages, it is realized via the if-else statement.

5-1 If-else Statement

The if-else statement is a multiline statement that reads as follows: if condition1 is true, then execute statements1, else if condition2 is true, then execute statements2, and so on. The elif keyword in Python is just like: "if the previous conditions don't apply, try this condition" and the else keyword catches everything that isn't caught by the previous conditions.

If-else Statement

if condition1:
elif condition2:
elif conditions3:
Calculate an interest rate depending on amount of money

What interest rate is calculated for amount = 20000?

amount, rate = 10000.0, 0.0
if amount > 50000:
    rate = 1.0
elif amount > 10000:
    rate = 0.5
elif amount > 0:
    rate = 0.2
else :
    rate = -0.2
5-2 Switch-case like statement

The switch-case statement provided by C-like languages offers support for situations where you have many different cases and the implementation via if-elif-elif-elif...else statement would be cumbersome. Python has no similar support for many case distinctions. In Python, as a workaround for switch-case, you can use function wrappers for dictionaries or, in newer Python versions, the more general concept of regular expression matching.

Switch-case via a dictionary

def switch_case(argument):
    switch_dict = {
        case1: label1,
        case2: label2,
        case3: label3,
    return (switch.get(argument, default_label))
Text output depending on choice

def switch_case(argument):
    switch = {
        1: 'First Choice',
        2: 'Second Choice',
        3: 'Third Choice',
    return (switch.get(argument, 'Default: Invalid'))
case = switch_case(2) 
print(case) # Output: Second Choice
case = switch_case(100)
print(case) # Output: Default: Invalid

6. Loops

while for in range break continue

Loops are used for program flow control, so that statements can be executed repeatedly, as long as an execution condition is met. Python has two loop commands: while loop and for loop. For additional control of the loop execution, the command break and continue are used: break allows you to exit a loop if a given condition is fulfilled, with continue you can skip loop steps depending on a condition.

6-1 While Loop

A while loop allows statements to be executed repeatedly, as long as an execution condition is met.

while condition :
Example 1
Print list of students

The counter variable that is queried (here: "i") in the condition must incremented explicitly in the loop body (here: i+=1), else the loop condition will be always true and the execution of the loop will not stop until your script runs out of memory.

students = ["John Doe", "Jane T.", "Mad Max"] 
i = 0
while i < len(students):
Example 2
Calculate the sum 1 + 2 + ... + 5
sum = 0
i = 1
while i <= 5:
    print(str(i) + "+")
    sum += i
    i += 1
print("Summe = " + str(sum))
Example 3
Infinite loop stopped with break
counter = 1
while (True):
    print(counter,  ':')
    counter += 1
    if (counter == 10):
5-2 For Loop

For loops are used to iterate over the elements of a sequence (list, tuple, set, etc.). The syntax of the for loop requires to use the member operator "in".

C-like for loops that specify a start and end condition and an increment for a counter variable must be rewritten in Python using the range() function, as in the example below. This type of for loop is used less often in Python, since you often work with objects and, as in Example 1, you can use the in operator to iterate over the elements of a list or set.

# Using iterator
for element in list:
# Using a counter variable and range()
for i in range(n):
Example 1
Print list of students
students = ["John Doe", "Jane T.", "Mad Max"] 
for std in students: 
    print("Student: " +  std) 
Example 2
Calculate sum 1 + 2 + ... + 5
sum = 0
for i in range(1,6):
    print(str(i) + "+")
    sum += i
print("Sum = " + str(sum))
Example 3
For-loop with continue

With continue you can skip loop steps depending on a condition. In this example, we skip all students whose names start with 'J'.

students = ["John Doe", "Jane T.", "Mad Max"] 
for std in students: 
    if (std.startswith('J')):

7 Data Structures

List Tuple Set Dictionary

While variables store single data objects like numbers or strings, a data structure stores multiple data objects, and has operations so that these data can be manipulated (stored / retrieved / updated) efficiently. Python's standard library provides as data structures: List, Tuple, Set and Dictionary, which may contain objects of any data type, that is in a list you can store numbers as well as strings. The set of operations / methods depends on the data structure. For lists the operations are insert(), append(), remove(), concatenate(), count(), sort(), index(), clear() ..., for dictionaries they are called keys(), values(), items(), get(), setdefault(), update() ....

7-1 Lists

Lists are collections of objects of any data type that are ordered and changeable. They allow duplicate members and are defined with square brackets. List items are selected by index as in the example below. Lists are comparable to arrays in C or Java.

list = [ element1, element2, ..., elementn]
element[i] = object
Example 1

A Python program maintains a list of students at a college. The expression students[1,3] selects all elements with index ≥ 1 and < 3 (slicing).

# List
students = ["John Doe", "Jane Test", "Mad Max"] 
print(students) # ['John Doe', 'Jane Test', 'Mad Max']
# Assignment: Mad Max is replaced by Jane Doe
students[2] = "Jane Doe" # Assignment
print(students[0]) # John Doe
print(students[-1]) # Jane Doe
print(students[1:3]) # ['Jane Test', 'Jane Doe']

A frequently required functionality in connection with lists is the conversion of a list into a string, this can be achieved with the help of the join function. In the following example, we have a list of student names that we want to convert to a string for output, using a semicolon to separate each element.

Example 2

In this example the string elements of a list are concatenated to a single string, with semicolon as separator.

stud_list = ["John Doe", "Jane Test", "Mad Max"] 
print("List:\n", stud_list)
stud_string = ';'.join([str(item) for item in stud_list])
print("String:\n", stud_string)
7-2 Tuples

Tuples are ordered and immutable collections used to store related records. They allow duplicate members and are defined with parentheses, the elements being separated by comma. The elements of a tuple can be selected by their index.

mytuple = (element1, element2, ..., elementn)
Example 1

A Python program maintains a list of college locations. Since the addresses do not change, tuples are used here. The assignment in line 7 generates an error because the elements of a tuple cannot be changed.

# Tuple
address = ("HS KL", "Schoenstrasse", 11)
print(address) # ('HS KL', 'Schoenstrasse', 11)
print(address[-1]) # 11
print(address[1:3]) # ('Schoenstrasse', 11)
# TypeError: 'tuple' object does not support assignment
address[0] = "Morlauterer Strasse" #  TypeError
Example 2

Another use for tuples is as a workaround to store your constants. In section 3-2 we have learned that Python does not support constants as other languages do. A simple workaround is to store your constants as tuples. Assignment is disallowed for tuples, this makes sure that you can not re-assigned your constant by mistake. One drawback is that a tuple must have at least two elements, and you must access an element via its index.

# Declare a tuple for different precisions of PI
MY_PI = (3.14, 3.14159)
print(MY_PI[0]) # 3.14
print(MY_PI[1]) # 3.14159
MY_PI[0] = 3 #  TypeError: assignment is not allowed

7-3 Sets

Sets are unordered collections of elements and do not allow for duplicate members. Because the content is unordered, you can't use an index to access the items. A set is defined by enclosing its elements in curly braces, as in the example below.

myset = {element1, element2, ..., elementn}

# Sets
set1 = {1, 2, 3, 4, "a", "b", "c"}
set2 = {3, 4, 5, 6, "c"}
set1.add(8)       # add new element
set2.discard(6)   #  delete an element
print(set1) # {1, 2, 3, 4, 8, 'a', 'b', 'c'}
print(set2) # {3, 4, 5, 'c'}
u = set1.union(set2) # union of two sets
print(u) # {1, 2, 3, 4, 5, 8, 'a', 'b', 'c'}
diff = set1.difference(set2) # difference of two sets
print(diff) # {1, 2, 8, 'a', 'b'}

Alternatively, a set can be defined using the set function. In example 2, a list is first declared, and then a set from the list using the set function.

list = [1, 2, 3] # Output: [1, 2, 3]
set3 = set(list) # create a set from a list
print(set3) # Output: {1, 2, 3}

The set data structure is an implementation of the mathematical notion of set and supports set operations such as union, intersection, difference.

7-4 Dictionaries

Dictionaries are collections of key-value pairs that are unordered and mutable. The values ​​are indexed via keys, which must be unique.

my_dict = { 
 key1 : value1, 
 key2 : value2, 
 keyn : valuen 

A Python program manages a phone book. A phone number is assigned to each contact. As keys we use the names of the contacts, as values the phone numbers.

# Dictionary (Key-Value Store) 
phonebook = { 
 "John Doe" : "0171 876654", 
 "Anna Test" : "0151 987654" 
# Add a new value with key John Doe
phonebook["John Doe"] = "0171 123456"  
print(phonebook) #  
# Get value by key
annasNumber = phonebook.get("Anna Test")   
print(annasNumber) # 0151 987654 

8 Functions

def return local variable global variable Function comment Lambda function

A function is a named block of code that runs only when used ("called"). Functions are defined once and can then be called any number of times. You can pass data as parameters to a function, and a function can also return values. A function parameter is just a name / object placeholder listed in the function definition and must be replaced with an actual value ("argument") when using the function.
Most of the times we use functions written by others and available through packages. For example, we use the sin()-function from the math package, or the read_excel()-function from the pandas package. Self-written functions are useful for structuring larger scripts and for creating reusable pieces of code.

8-1 Functions with empty parameter list

A function with empty parameter list is simply a named group of statements, that is defined once and can be called multiple times.


A function is defined by the keyword def and a function name (here: my_func), round brackets () to indicate the empty parameter list, and a colon, followed by the statements defining the function. Important: the statements of the function body must be indented.

# (1) Define the function my_func
def my_func(): 
    # TODO: Enter your statements here

# (2) Use the function my_func
my_func()	# Function call
Function separator_line()

An useful example of a function with empty parameter list is the function separator_line(), that creates a separator line to be used for embellishing console output.

# Define the function separator_line
def separator_line(): 
    print("<<<", end = ' ')
    print(">>>", end = '\n')

# Use the function separator_line
separator_line()	# Output: <<<>>>
separator_line()	# Output: <<<>>>
8-2 Function with non-empty parameter list

A function is first defined by using the keyword def, followed by a function name (here: my_func) and a comma-separated parameter list that is surrounded by round brackets. A function is then used ("called") by specifying its name and a list of arguments.

Function arguments in Python can be specified in two different ways:
1. by providing argument values. In this case, number and order of the arguments must match the parameter list.
2. by providing parameter name - parameter value pairs, then the order can be switched.
Definition and usage of a function with non-empty parameter list
# Define the function my_func
def my_func(param_1, param_2, ... param_n):    
    # TODO: enter your statements here  
# Use the function my_func      
my_func(arg_1, arg_2, ..., arg_n)
my_func(param_n = arg_n, ..., param_1 = arg_1)
Function separator_line() with parameters

The previous example of a separator line function to be used for embellishing console output can be generalized using three parameters: symbol1, symbol2 and n, so that we can create separator lines with different symbols and different lengths.

# Define the function separator_line
def separator_line(symbol1, symbol2, n):  
    print(symbol1*n, end = '')
    print(symbol2*n, end = '')
    print(' ')   
# Use the function separator_line      
separator_line('<', '>', 4)	# Output: <<<>>> 
separator_line('*', '*', 3) # Output: ******

In self-written functions, any variable defined within a function is a local variable: it can be used only within that function. Variables defined outside functions are called global variables, they can be used in any function. For example, the variable v defined in Example 2 "Calculate volume of a cone" is a local variable.

8-3 Function with return value

A function can also return data/parameters as a return value, these can be used further in the calling function in calculations or outputs. In order for a function to return a value, the keyword "return" is used.

Function with return value

# Define the function my_func
def my_func(param_1, param_2, ... param_n):    
    # TODO: enter your statements here  
    ret = calculation involving param1 ... 
    return ret
# Use the function my_func      
ret1 = my_func(arg_1, arg_2, ..., arg_n)
ret2 = my_func(arg_1, arg_2, ..., arg_n)
Example 1
Function with 2 parameters

This example shows how to define an user-defined function f(x,y) = sin(x)*exp(-y). Important: x and y must be real values, the function will not work for parameters x, y that are lists, so f(list1, list2) will generate an error. In Python, if a function should accept list parameters, you must vectorize it first using NumPy's vectorize.

# Define the function f
from numpy import pi, sin, exp
def f(x, y) :
    return sin(x)*exp(-y)
# Use the function f
z = f(pi/4, 1); print(z)
# Use vectorized form of function f
fvec = np.vectorize(f)
z = fvec([pi/4, pi/2], [1, 2]); print(z)
Example 2
Calculate volume of a cone

This example shows how to write a function volume_cone() that calculates the volume of a cone. Known: The volume of a cone is calculated using the formula V = PI*r^2*h/3, where r is the radius and h the height of the cone. So our function has two parameters, r and h and returns the volume.

import math
# Define the function volume_cone()
def volume_cone(r, h): 
    v = math.pi * math.pow(r,2) * h / 3
    return v
# Use the function volume_cone()
r = float(input("Enter radius:")); # Ask user to enter radius
h = float(input("Enter height:")); # Ask user to enter height
print("Volume of cone = ", volume_cone(r, h))
Lambda functions

Lambda functions are a type of inline functions used for small code fragments that can be defined in just one line. Assume you need the function f(x) = sin(2*x)*exp(-x) repeatedly in your code. Instead of writing a multi-line regular function definition with def, you could just write the following lambda function:

import math
f = lambda x: math.sin(2*x)*math.exp(-x)
y1 = f(1.57); y2 = f(2);
print(y1, y2)
8-4 Function comments

Functions should be commented, so as to provide the necessary information about what the function does and how to use it. In Python, function comments are created by enclosing a documentation text in three double quotes in the second line of the function definition, that is, after the colon. This type of function comment is read by the Python help system, and when you type help(function_name), is printed to standard output.

Function comment

The general syntax for a function comment is shown here: it should include a short description and the role of the parameters and return value.

# Define the function my_func
def my_func(param_1, param_2, ... param_n):    
    This function does this and that ... 
    @param param1: represents this and that 
    @return ret: the return value represents ... 
    # TODO: enter your statements here  
    ret = calculation involving param1 ... 
    return ret

# Define the function separator_line
def separator_line(symbol1, symbol2, n):  
    This function creates a separator line 
    with n repetitions of symbol1 and n repetitions of symbol2
    @param symbol1: first symbol
    @param symbol2: second symbol
    @param n: number of repetitions
    print(symbol1*n, end = '')
    print(symbol2*n, end = '')
    print(' ')   
# Get help on the function
Python: Get help on function

9 Classes and inheritance

class __init __ () self Base class Subclass Inheritance super()

Python is an object-oriented programming language, meaning that almost everything is a class with attributes and methods. The class is an object constructor for creating new objects of the same type. The class attributes are variables and the class methods are functions belonging to that class. Python classes are defined using the keyword class and by implementing an __init __ () method that is implicitly executed when creating a new class object.

9-1 Define and initialize classes

We define a class by using three keywords: class, self and pass, and one special function: __init __ ().


* The __init __ () method is used to set object properties or perform other operations required when creating the object.
* The keyword self is a reference to the current instance of the class and is used to access class variables. When defining methods for a class, you must specify self as the first parameter in the parameter list. When calling class attributes and methods within a class definition, you write self. before the attribute or method name, so as to specify that they belong to the current class.
* The keyword pass indicates that at the given place nothing happens. This statement is used as a placeholder for future code. For example when you develop a new class and already write down the method names, but not yet the actual implementations.

# (1) Define a class
class ClassName:
    def __init__(self, param1, ... paramn) :
        self.param1 = param2
        self.paramn = paramn
    def method1(self, param_list):
        # TODO: enter your statements here
    def method2(self, param_list):
        # TODO: enter your statements here
# (2) Use a class
object1 = ClassName(arg_list)
object1.method1(arg_list);  object1.method2(arg_list);
object2 = ClassName(arg_list)
Class Student

We declare a class Student, with which we want to create new Student objects. A student has the properties name and semester and two methods: inc_semester() that increases the semester, and display(), that displays the properties of a student.

(1) Define the class

* Line 3-5: The __init __ () function sets current values ​​for name and semester.
* Line 6-7: The inc_semester() method increases the value of the attribute semester by 1.
* Line 8-10: The display()-method creates text output from the properties of the class object.

# (1) Define class Student
class Student:
    def __init__(self, name, semester) : = name
        self.semester = semester     
    def inc_semester(self):
        self.semester += 1
    def display(self):
        print("Student: " + + 
              ", Semester: " + str(self.semester))
(2) Use the class

After defining a class, you can use it to create new class objects (here: new students) and manipulate their data (here: display and increase semester).

  • * Line 2-5: Create a new class object std1, display it, increase the semester and display it again
  • * Line 5-6: Create a new class object std2 and display it.
  • * Line 7: Delete the class objects. The keyword "del" destroys the object and deletes it from memory.
# (2) Use class Student
std1 = Student("Jane Doe", 1) # Create a new student
std1.display() # Output: Student: Jane Doe, Semester: 1
std1.inc_semester() # Increment the semester variable
std1.display() # Output: Student: Jane Doe, Semester: 2
std2 = Student("Bart Simpson", 3) # Create another student
std2.display() # Output: Student: Bart Simpson, Semester: 3
del std1, std2 # Delete class instances
9-2 Inheritance in Python

With class inheritance we can define classes that inherit all the methods and properties of another class.

The class that is inherited from is called the base class. The class that inherits properties and methods from the base class is called a subclass. In Python, a subclass is defined by placing the base class name in parentheses after the class name.

Student inherits from Person

Persons have a name and a birth date. Students are Persons, that additionally have a semester, indicating in which term of their study they are.

(1) Define base class and subclass

How do we map this knowledge with classes and inheritance? We define a base class Person with attributes name and birthdate and a subclass Student with attribute semester. Since the subclass inherits the properties and functions of its base class, we do not have to redefine name and birthdate in the subclass, only the additional property semester and the additional method inc_semester().

  • * Line 2-9: Define base class Person. The to_string()-method of the base class Person takes as parameter a label and builds a string output from the properties of the Person.
  • * Line 11-19: Define subclass Student. In line 11, the initialization method of the base class is called with super.__init__(). In line 13, the property semester is set, which only belongs to the subclass.
  • * Line 17-19: Define the to_string() method of class Student. Note that we have a to_string()-method in both base class and subclass. The method to_string() defined in the subclass calls the base class method with the same name using the super()-keyword and then appends the semester attribute to the string.
# Define base class Person
class Person: 
    def __init__(self, name, birthdate): = name
        self.birthdate = birthdate
    def to_string(self, label):
        mystr = label + ": " +
        mystr += ", Birth date: " + self.birthdate
        return mystr
# Define subclass Student      
class Student(Person): 
    def __init__(self, name, birthdate, semester):
        super().__init__(name, birthdate)
        self.semester = semester
    def inc_semester(self):
        self.semester += 1
    def to_string(self, label):
        mystr = super().to_string(label)
        return  mystr + ", Sem: " + str(self.semester)
(2) Use base class and subclass

After we have defined base class and subclass, we can use them to create new Persons and Students and manipulate their data.

# Use base class and subclass
pers1 = Person("Alice First", "01.01.1980")
# P1: Alice First, Birth date: 01.01.1980
std1 = Student("Bob Second", "01.02.1990", 1) 
# S1: Bob Second, Birth date: 01.02.1990, Semester: 1

10 Packages

NumPy Matplotlib Pandas Scikit-Learn Keras

The most frequently used Python libraries for data analysis and machine learning are:

  • NumPy- arrays and random numbers, mathematical functions
  • Matplotlib - data visualization, charts
  • Pandas - Data processing for tabular data, Series, DataFrames
  • Scikit-Learn - Algorithms for Machine Learning, Classification, Regression
  • Keras - Artificial Neural Networks, wrapper for Tensorflow

The following badges give a short description for each of the aforementioned packages and illustrate their basic usage with a representative code fragment.

  • NumPy   array arange shapeconcatenate hstack vstack zeros ones empty linspace

    Numpy is a Python library for data management and preparation, providing support for array and random number generation and manipulation. With NumPy, you can create arrays in different ways and initialize and extract data in a convenient way. NumPy also provides mathematical constants and functions (pi, sin, cos ...).
    import numpy as np
    # One-dimensinal arrays
    x1 = np.array([1, 2, 3, 4])
    x2 = np.arange(1, 5, 1) # 
    x3 = np.arange(0, 1.1, 0.1)
    print('x1:', x1, '\nx2:', x2, '\nx3:', x3)
    # Two-dimensional arrays
    a1 = np.array([[1, 2], [3, 4]], )
    a2 = np.zeros((2, 3))
    print('a1:\n', a1, '\na2:\n', a2)
    NumPy arrays and functions

  • Matplotlib   figure plot surf legend xlabel ylabel

    Matplotlib is a Python library for data visualization that supports the creation of various types of charts via the pyplot package: line, scatter, bar charts, one- and two-dimensional, static or interactive. The most important commands are plot for one-dimensional and surf for two-dimensional plots. The plot command receives as parameters the x and y coordinates of the data to be plotted, and optionally a string with formatting information. There are also many options for adding labels, titles, legends, etc.
    import numpy as np  
    import matplotlib.pyplot as plt
    x = np.linspace(0,10,40)  # x-values
    y1 = np.sin(x);y2 = np.cos(x);  # y-values
    fig = plt.figure(figsize=[6, 3])  # Create figure with given size
    plt.plot(x, y1,'r*', label='sin');  # Plot sin function
    plt.plot(x, y2,'b+', label='cos');  # Plot cosine function
    plt.title('Sin und Cos Functions');
    plt.legend(loc="upper center")
    Matplotlib example

  • Pandas   Series DataFrame loc iloc read_excel write_excel

    Pandas is a Python library for creating and manipulating spreadsheet-like data. Pandas plays a central role in data analysis, as it allows you to load large CSV files into the program's main memory and then further cleanse and visualize the data. Pandas functions such as iloc(), loc(), resample() are used to select rows/columns/cells and to group and aggregate data.
    import pandas as pd
    # Read data from students.xlsx
    df = pd.read_excel('students.xlsx', index_col=0) 
    # Write data to a csv file
    df.to_csv('students.csv', index=True, sep = ';')

  • Scikit-Learn   model_selection train_test_split DecisionTreeClassifier DecisionTreeRegressor cross_val_score roc_curve

    Scikit-Learn is a Python library for machine learning and along with Pandas, Keras and Tensorflow and provides support for the usual steps of supervised and unsupervised learning: data preparation, training phase and model evaluation, as well as powerful algorithms for classification, regression and clustering problems. The example code shows the simplified procedure for training a decision tree model used for the classification of failures. First we read the data using the read_csv function, then extract features and target variable, split the data set into training and validation data using train_test_split, train the model using the fit function, visualize the tree using the plot_tree function.
    import pandas as pd
    import matplotlib.pyplot as plt 
    from sklearn import model_selection as ms
    from sklearn import tree
    df = pd.read_csv("data.csv", header=0, sep = ";", index_col=0)  
    print('DataFrame:\n', df);  
    # Extract features in a Numpy array x  
    x = df.iloc[:,0:2].to_numpy()  
    # Extract target variable in a Numpy array y   
    y = df[['failure']]  
    y = y.values   
    # 90% training and 10% validation
    X_train, X_test, y_train, y_test =  ms.train_test_split(x, y, test_size=0.1, random_state=1)   
    model = tree.DecisionTreeClassifier(criterion='entropy', splitter='best')  
    # Create decision tree, y_train)
    # Visualize decision tree
    fig, ax = plt.subplots(figsize=(5, 5)) 
    tree.plot_tree(model, filled=True, feature_names=df.columns[0:2], class_names=['yes','no'])

Here a few other useful packages.

  • Math   sin pow sqrt fabs trunc ceil
    The math-package declares mathematical functions: trigonometric functions (sin, cos, ... ), power and logarithmic functions (pow, log, sqrt, ...), functions for rounding (ceil, floor). This package also defines some useful constants such as math.pi and math.e.

  • Seaborn   load_dataset set_theme lmplot scatterplot lineplot histplot
    Python's Seaborn-package is an enhancement of Matplotlib and provides a high-level syntax for data visualization. Seaborn provides functions for loading datasets into Pandas DataFrames, pre-defined themes, and shortcut functions such as lineplot, scatterplot and histplot for different statistical visualizations.
    import seaborn as sns
    import pandas as pd
    # Set Seaborn theme
    sns.set_theme(style="whitegrid", palette="pastel")
    # Load dataset from csv-file
    mydata = pd.read_csv("data.csv", sep=';')
    # Plot humidity against temperature
    sns.barplot(x = 'temp', y='humidity', data=mydata);
    Seaborn barplot