Popup Quiz

Please find below a set of questions representing the kind of Python required to benefit in an optimal manner from this course. If you are able to answer most of the questions without problems, you likely have a level of Python knowledge suitable for this course.

How can you execute a Python script from a terminal?

Possible answerHide answer

$ python3 myscript.py

What is the difference between a = 3 and a == 3?

Possible answerHide answer

The first statement assigns the value of 3 to the variable a as an integer. a == 3 checks if a is equal to 3.

What is the purpose of indenting in Python? Name keywords (minimum: 8) typically leading to a subsequent indent!

Possible answerHide answer

The indents are showing the different levels of nested code. Python does not use parentheses to indicate code blocks (such as for-loops).
Standard keywords with a subsequent indent are for, while, if, else, elif, with, try, except, finally, def and class.

How can you run a loop over all odd integers from 3 up to but not including 15?

Possible answerHide answer

for i in range(3,15,2): ...

How can you run a loop over all items of the list mylist?

Possible answerHide answer

for entry in mylist: ...

When are you using for, when while?

Possible answerHide answer

A for-loop is more suitable if the termination condition leads to a fixed number of iterations e.g. when iterating over the items of a list.

Re-write the for-loop of Question 4 as a while-loop?

Possible answerHide answer

i=3 while i<15: ... i += 2

What is the difference of a set compared to a list?

Possible answerHide answer

A set is a container that only contains unique items and the order of the items is not relevant.

How do you sort a list in Python?

Possible answerHide answer

sorted_list = sorted(mylist) or mylist.sort()

And how do you sort a set?

Possible answerHide answer

Since a set does not guarantee the order of its items, it is unsortable. The command sorted(myset) is valid, but returns a sorted list of the items in myset.

How do you create in one line a Python dictionary with values from 0 to 9 and keys 'key_i' (as a string) where i stands for the square of the corresponding value?

Possible answerHide answer

mydict = {'key_{}'.format(v**2) : v for v in range(10)} or mydict = {f'key_{v**2}': v for v in range(10)}

How can you define a function that prints 'Hello, World!'?

Possible answerHide answer

def helloWorld(): print('Hello, World!') Please note that print is in Python 3 a fulfledged function (Parentheses!).

What is the difference between print and return in a function? Give an example of a function that uses both.

Possible answerHide answer

print has the purpose of displaying its argument for example on the terminal. return has the purpose of handing back the result of the function to the programme. The return value can for example be stored in a variable.
def square_with_print(x): print('The square of {0:.1f} is {1:.1f}!'.format(x,x**2)) return x**2

How do you read a text file line by line and store every line as an item in a list?

Possible answerHide answer

with open('/path/to/file.txt') as f_i: mylist = f_i.readlines() # or `mylist = list(f_i)`
Please be aware that both commands keep the newline characters at the end of each line.

How can you define in one line the function cos(x)·sin(x)?

Possible answerHide answer

import math myfunc = lambda x: math.cos(x)*math.sin(x)

How can you catch an exception, e.g. a ValueError that occurs when converting a string into an integer?

Possible answerHide answer

try: i = int(s) except ValueError: print(s+' cannot be converted into an integer.') ...
The print statement is only an example of a potential code snippet.

What does a = myfunction()[3] do?. What needs to be fulfilled for the code to be working?

Possible answerHide answer

The code stores the 4th element of the return value of myfunction() in the variable a. The return value of myfunction() needs to be an object supporting access by index with key '3' set. (e.g. a list with at least four elements or a dict {3: ...})

What do a = [], a = {} and a = () do?

Possible answerHide answer

They create an empty list, dictionnary and tuple, respectively. The latest one is of questionable purpose since a tuple is immutable. While {1,2,3} creates a set, an empty set can only be initiated by set().