Programming with Python
Kühne Logistics University Hamburg - Fall 2024
Tip
I really appreciate active participation and interaction!
.py
files)Tip
I’d encourage you to start and submit your solution early
Warning
But you should not simply use them to replace your learning.
Photo by Choong Deng Xiang on Unsplash
Photo by National Cancer Institute on Unsplash
Photo by Clay Banks on Unsplash
Photo by Ishant Mishra on Unsplash
Photo by Denys Nevozhai on Unsplash
Tip
Great resources to start are books and small challenges. You can find a list of book recommendations at the end of the lecture. Small challenges to solve can for example be found on Codewars.
In case you find errors and typos in the lecture notes, please report them in the following form:
https://tally.so/r/w7oapa
=
applicationCaution
Not all packages available in Python are available in Pythonista, thus you might need a computer to solve certain problems.
Task: Create a directory for the course and create a new file called hello_world.py
with the following code:
Hello, World!
Run it with the green ‘run’ button or by pressing F5!
Note
“Hello world” is a classic example to start with. It is often used as a test to check if your computer is working properly and that you have installed the necessary software.
Task: Change the code in your hello_world.py
file. Assign the string "Hello, World!"
to a variable called message
and print the variable.
We can also mix "
and '
in a string, if we are consistent:
I shout "Hello, World!"
Try it yourself! What does happen, if you try to run it?
SyntaxError: invalid syntax
Note
We will cover these concepts in more detail later in the course.
Let’s go back to our first program:
#
message
print
=
Time
But there are certain rules to variable names!
_
a
and A
are different!for
, if
, def
, etc
Question: Which of the following fulfill these conditions?
a, _duration, 1x, time_left, 1_minute, oneWorld, xy4792
function([arguments])
None
Hello, World!
Hello, World!
Note
We will cover functions in more detail later in the course.
type()
is a function that returns the type of a valueBack to our example of “Hello, World!”
Hello, World! is a <class 'str'>
Result: “Hello, World” is a string - in short ‘str’.
But what about the f”?
f
Note
In older code bases, f strings were not available. Here, interpolation could be done as shown below with print()
and .format()
. But this method is less concise and arguably less readable.
{<to_print>:<width>.<precision>f}
width
can be a number specifying the output width<
, ^
, >
can be used before the width to align the textprecision
can be used to specify the decimals.f
can be used to format floatshello has 5.00 characters.
Great, the result is 3
"Hello"
, 'World'
, "123"
, '1World23'
Hello, World!
Note
Strings are immutable, we can’t change characters in them once created.
Hello, World!
True
and False
1
and 0
, respectivelyif
, while
, for
, elif
, `else> More on them in our next lecture!
1
, -3
, 0
or 100
-4.78
, 0.1
or 1.23e2
1 is of type <class 'int'>, 128.64 is of type <class 'float'>
Warning
The interpreter will automatically convert booleans to integers to floats when necessary, but not the other way around!
addition = 1 + 2; print(f"Result: addition is {addition}")
substraction = 1 - 2; print(f"Result: substraction is {substraction}")
multiplication = 3 * 4; print(f"Result: multiplication is {multiplication}")
division = 7 / 4; print(f"Result: division is {division}")
floor_division = 7 // 4; print(f"Result: floor_division is {floor_division}")
exponentiation = 9 ** 0.5; print(f"Result: exponentiation is {exponentiation}")
modulo = 10 % 3; print(f"Result: modulo is {modulo}")
Result: addition is 3
Result: substraction is -1
Result: multiplication is 12
Result: division is 1.75
Result: floor_division is 1
Result: exponentiation is 3.0
Result: modulo is 1
2 + 3 * 4 = 14
input([userprompt])
Important
The function always returns the input as string!
Task: Solve the following task:
Use type conversion for other data types
int(input())
float(input())
bool(input())
str(input())
1.789 converted to 1 of type <class 'int'>
1.789 converted to 2 of type <class 'int'>
Note
That’s it for todays lecture!
We now have covered the basics on the Python syntax, variables, and data types.
Note
Think Python is a great book to start with. It’s available online for free here. Schrödinger Programmiert Python is a great alternative for German students, as it is a very playful introduction to programming with lots of examples.
For more interesting literature to learn more about Python, take a look at the literature list of this course.
Lecture I - Introduction | Dr. Tobias Vlćek | Home