# Preparatory exercises¶

These exercises should help you to decide whether you should watch the introductory video lectures and/or read the respective scripts.

These lectures are addressed to beginners who have no previous programming experience at all, so depending on your knowledge you can also watch them selectively.

In case you have questions or problems please do not hesitate to contact Claudius via mail.

## 1. Basics¶

1.1 What is an operator?

In [ ]:



1.2 Which operators help to do the following calculation in Python:

• Substraction
• Division
• Taking roots
In [ ]:



1.3 Bind the result from the calculation $4^2 + 20$ to the identifier x and print it.

In [ ]:



1.4 What is a keyword and how does it relate to the concept of an identifier?

In [ ]:



## 2. Data types¶

2.1 How can you get the type of an object?

In [ ]:



2.2 What type is x in the following code? How can you transform it into a character?

In :
x = 2.0

In [ ]:



2.3 Explain the difference between integers, floats and characters.

In [ ]:



2.4 How does the operator + interact with integers, characters and Boolean values?

In [ ]:



2.5 Create a list l1 containing the elemens 4, y and 25. Then substitue the second value with 50 and print the resulting list.

In [ ]:



2.6 What is the difference between lists, tuples and sets?

In [ ]:



2.7 Create a list with the numbers from 1 to 10 and save it. Then extract the last three elements and print them.

In [ ]:



2.8 Sort the list in decreasing order and print it.

In [ ]:



2.9 Append 25 to the list.

In [ ]:



2.10 Append the numbers 99 and 80 to the list.

In [ ]:



2.11 Reverse the list.

In [ ]:



2.12 Create the following two sets: \begin{align*} m_1 &= \{1, 4, 23, 95, 12\}\\ m_2 &= \{0, 23, 80, 96, 95\}\\ \end{align*} What is the intersection of these sets?

In [ ]:



2.13 Create a dictionary with the following key-value pairs:

• "Hello" and "Hola"
• 5 and 120.5
• "bla" and [10, 80]

Call the value of 'bla'.

In [ ]:



## Functions¶

2.1 Define a function that computes the following function:

$$f(x,y) = 10x+(1-y)^2$$
In [ ]:



2.2. Extend the function such that it test whether its inputs are only numbers (int or float) Ergänzt die Funktion, sodass sie überprüft ob als Inputs nur Zahlen eingegeben wurden (int or float). If an input is no number, Python should raise an error.

In [ ]:



## 3. Loops¶

3.1 Explain the difference between a for loop and a while loop.

In [ ]:



3.2 Create a list with the square roots of the numbers between 2 and 12. Provide a solution with a for loop and another using a list comprehension. Which solution is usually better?

In [ ]:



3.3 The following loop does not work:

In [ ]:
l_1 = [0, 2, "Land", "!"]
l_2 = ["Lame", "La", 1, 3]
for i in l_1:
print(i * l_2[i], end=" ")


Adjust the code such that it does not iterate over the lements of l_1, but its indices.

Desired result: "LaLa Land!!!

In [ ]:



3.4 How often does one need to raise $1.1$ to the power of 2 until the result is larger than 10? Answer this question using a while loop.

In [ ]:



## 4. If-else statements¶

4.1 Translate the following into an if-else statement:

When x is an integer, print the adequate statement x equals two, x is smaller than two or x is bigger than two. When x is no integer, print the statemen x is no integer!.

In [ ]:
x = "2"
if type(x)==int:
if x == 2:
print("x equals two!")
elif x < 2:
print("x is smaller than two")
elif x > 2:
print("x is bigger than two")
else:
print("What he heck!?!?")
elif type(x)==str:
print("Why would you compare strings with ints?")


## 5. Imports and try except¶

5.1 Import pi from the math package and print it.

In [ ]:



5.2 Compute $\sqrt{2}$ using the function sqrt from the math package.

In [ ]:



5.3 Build a try-except block in which you try to divide two variables, x and y, and in which you transform them into floats in case this operation leads to a TypeError.

In [ ]:
x = "2" # Note that x is of type str
y = 4
try:
z = y / x
except TypeError:
print("A type error occured. Try to resolve by converting x to float.")
x = float(x)
z = y / x
print(z)


# Solutions¶

The solutions can be found on the course homepage.

In [ ]: