Programming with Python
Kühne Logistics University Hamburg - Fall 2025
{}
My name is Mr. Smith, I'm 30 years old, and 1.83 meters tall.
We used the :.2f
format specifier to round the number to two decimal places.
int
, float
, str
, bool
=
operatorAddition
Subtraction
Multiplication
Division
Floor Division
Exponentiation
Modulo
+
-
*
/
//
**
%
Adds two numbers
Subtracts one number from another
Multiplies two numbers
Floating-point division
Integer division
Power of
Remainder of division
Note, that the /
operator always returns a float, even if the division is even. Furthermore, the +
operator can be used to concatenate strings and that the *
operator can be used to repeat strings.
+=
, -=
, *=
, /=
, //=
, **=
, %=
> Question: What is the value of x
after the operations?
Initial value of x: 10
After x += 5: 15
After x *= 2: 30
After x %= 4: 2
> Task: Calculate the final value step by step:
# Start with the following values
price = 100
discount_percent = 15
tax_rate = 0.08
# Your task:
# 1. Apply the discount to the price (price reduced by discount_percent)
# 2. Add tax to the discounted price
# 3. Round the final price to 2 decimal places
# Your code here
final_price = ? # What should this be?
You can use the round()
function to round a number to a specific number of decimal places. For example: round(3.14159, 2)
returns 3.14
.
For most programming purposes, you can treat everything in Python as an object. This means you can assign all types to variables, pass them to functions, and in many cases, call methods on them.
object.method([arguments])
You can use the dir()
function to list all methods and attributes of an object.
Here are some commonly used string methods:
upper()
: Converts all characters in the string to uppercaselower()
: Converts all characters in the string to lowercasetitle()
: Converts first character of each word to uppercasestrip()
: Removes leading and trailing whitespacereplace()
: Replaces a substring with another substringfind()
: Finds first substring and returns its indexcount()
: Counts the number of occurrences of a substring> Question: What will be the output of the following code?
message = "Hello, World!"
print(message.upper()) # Converts to uppercase
print(message.lower()) # Converts to lowercase
print(message.title()) # Converts to title case
print(message.replace("World", "Python")) # Replaces "World"
print(message.find("World")) # Finds "World" and returns its index
print(message.count("o")) # Counts the number of occurrences of "o"
HELLO, WORLD!
hello, world!
Hello, World!
Hello, Python!
7
2
Note, how replace()
does not modify the original string. Instead, it returns a new string.
> Task: Discuss and implement the following task:
Remember, that these methods return a new string. The original string is not modified.
The Python Programmer.
Here we chained methods together to perform multiple operations after another in one line.
sequence[start:stop:step]
start
is the index of the first element to includestop
is the index of the first element to excludestep
is the increment between indices (default is 1)start
or stop
, it will be replaced by the start or end of the sequence, respectivelystep
, it will be replaced by 1> Task: Discuss and implement the following task:
Remember, that these methods return a new string. The original string is not modified.
True
or False
)> Question: What will be the output of the following code?
lower_number = 2; upper_number = 9
print(lower_number == upper_number) # Equality
print(lower_number != upper_number) # Inequality
print(lower_number > upper_number) # Greater than
print(lower_number < upper_number) # Less than
print(lower_number >= upper_number) # Greater than or equal to
print(lower_number <= upper_number) # Less than or equal to
False
True
False
True
False
True
and
, or
, not
> Question: Which of the following expressions is True
?
True
True
True
Note, that and
and or
are evaluated from left to right.
in
, not in
> Question: Which of these expressions is True
?
True
False
Note, that in
and not in
can be used for strings, lists, tuples, sets, and dictionaries. Don’t worry! We will learn about lists, tuples, sets, and dictionaries later in the course.
if
, elif
, else
, for
, while
, break
, continue
> Question: What do you think each of the above does?
Mixing tabs and spaces can cause errors that are difficult to debug. The Python style guide (PEP 8) recommends using 4 spaces per indentation level for consistency and readability.
if
statements execute a block of code if a condition is True
elif
statements execute a block of code if the previous condition is False
and the current condition is True
else
statements execute a block of code if the previous conditions are False
You can use the and
and or
operators to combine multiple conditions.
The condition is True!
This will always be printed!
This will always be printed!
Writing if condition:
is equivalent to if condition == True:
The condition is True!
The temperature is greater than 10!
> Question: What will be the output of the following code?
name = "Harry"
profession = "wizard"
age = 16
if name == "Harry" and profession == "wizard" and age < 18:
print("You are the chosen one still visiting school!")
elif name == "Harry" and profession == "wizard" and age >= 18:
print("You are the chosen one and can start your journey!")
else:
print("You are not the chosen one!")
You are the chosen one still visiting school!
> Task: Create a grade classifier:
# Given a numerical score, classify it into letter grades
score = 87 # You can test with different values
# Your task: Create if/elif/else statements that assign letter grades:
# 90-100: "A"
# 80-89: "B"
# 70-79: "C"
# 60-69: "D"
# Below 60: "F"
# Also handle invalid scores (negative or > 100)
# Your code here
print(f"Score: {score}, Grade: {grade}")
for
and while
for
loops are used to iterate over a sequence (e.g., list, tuple, string)while
loops execute repeatedly until a condition is False
Nested control structures through further indentation are allowed as well, we thus can chain multiple control structures together.
0
1
2
3
4
0
2
4
6
8
The range()
function can take up to three arguments: start, stop, and step.
> Question: What do you expect will be the output?
y
e
l
l
o
w
b
a
n
a
n
a
0
1
2
3
4
> Question: What could be an issue with poorly written while-loops?
> Question: Anybody an idea what this code does?
0
10
20
30
40
50
60
70
80
90
100
Without control flow, programs would execute linearly from top to bottom, limiting their functionality and flexibility.
> Task: Implement the following task:
And that’s it for todays lecture!
We now have covered the basics on String methods, Comparisons, conditional statements and loops.
Nothing new here, but these are still great books to start with!
For more interesting literature to learn more about Python, take a look at the literature list of this course.
Lecture II - Control Structures for Your Code | Dr. Tobias Vlćek | Home