Questions and answers: Python

Revise Python: here is a selection of questions of every level, with the answer and an explanation. To test yourself without seeing the answers, play the quizzes.

Play the Python quizzes

Easy

Who designed the Python language?

Answer : Guido van Rossum

Guido van Rossum designed Python, whose first published version dates from 1991.

Where does the name of the Python language come from?

Answer : From the British comedy troupe Monty Python

Guido van Rossum chose this name in reference to the British comedy troupe Monty Python.

What delimits the code blocks in Python, such as the body of an if or of a function?

Answer : Indentation

In Python, indentation is not a display detail: it delimits the blocks of if, for, functions and classes.

What is the type of the value 3.14 in Python?

Answer : float

A number written with a decimal point is of type float; int designates integers without a decimal part.

Which keyword adds a second condition after an if in Python?

Answer : elif

elif is short for else if: it adds a condition after the if, before an optional else.

Which Python structure can be modified after it is created?

Answer : The list

A list, written between square brackets, can be modified (adding, removing, replacing); a tuple, written between parentheses, cannot.

Which expression creates a Python dictionary with the key 'nom' and the value 'Ada'?

Answer : {'nom': 'Ada'}

A dictionary is written between braces, in the form key: value; {'nom', 'Ada'} without a colon is a set.

Which values does list(range(3)) produce?

Answer : [0, 1, 2]

range(3) starts at 0 and stops before 3: it produces 0, 1 and 2, that is three values.

Medium

What does [n * n for n in [1, 2, 3]] return?

Answer : [1, 4, 9]

A list comprehension goes through the sequence and computes each element: here n * n gives 1, 4, then 9.

With def f(a, b=2): return b, what does f(1) return?

Answer : 2

A parameter that has a default value receives it when the caller does not provide an argument for it.

What does the **kwargs parameter contain inside a function?

Answer : A dictionary of the named arguments

**kwargs gathers the arguments passed with a name (word=value) into a dictionary.

What does 'Bonjour'[0:3] return?

Answer : 'Bon'

The slice [start:end] takes the indexes from start and stops before end: here the indexes 0, 1 and 2, that is 'Bon'.

By convention, what name is given to the first parameter of a Python instance method?

Answer : self

The first parameter of an instance method receives the current object; the convention is to name it self.

What is a virtual environment (venv) used for in Python?

Answer : Isolating the packages installed for a given project

A venv is a folder that isolates the packages of a project, to avoid version conflicts between projects.

Which keyword follows try to handle an exception in Python?

Answer : except

The except block runs when a matching exception is raised in the try block; the rest of the try is then interrupted.

Why write with open('notes.txt') as f rather than f = open('notes.txt')?

Answer : The file is closed automatically at the end of the block, even on error

with uses a context manager: the file is closed when the block ends, even if an error occurs.

Hard

What does calling a function that contains the yield keyword return?

Answer : A generator object, which produces the values one by one on request

A function with yield returns a generator: its body runs only when the next value is requested.

What happens when next(it) is called on an exhausted iterator, without a default value?

Answer : It raises the StopIteration exception

next() returns the next item; with no item left, the StopIteration exception signals the end of the iteration, which the for loop catches.

What is a decorator in Python?

Answer : A function that takes a function as an argument and returns a modified function

A decorator is applied with @ above a function; a classic example is a decorator that measures the duration of a call.

In CPython, the reference implementation of Python, what does the GIL do?

Answer : It allows only one thread to run Python bytecode at a time

The GIL is the lock of the CPython interpreter: only one thread runs Python bytecode at a time, which limits the parallelism of computations.

What does the await keyword do in a coroutine?

Answer : It suspends the coroutine while waiting for an awaitable, without blocking the event loop

await suspends the coroutine until the awaited object is ready, and the event loop can run other tasks in the meantime.

What does the Python interpreter do with a type annotation such as x: int = 'abc'?

Answer : Nothing: the annotation does not influence the execution

Annotations are used by tools such as mypy and for readability; the interpreter neither checks nor converts them at run time.

Which special method is called by len(obj)?

Answer : __len__

len(obj) calls the __len__ method defined in the class of the object, which must return an integer.

What is the difference between copy.copy and copy.deepcopy on a list of lists?

Answer : copy.copy duplicates only the first level; deepcopy also duplicates the sublists

copy.copy creates a new outer object, but its sub-objects stay shared; deepcopy also duplicates the nested objects.

Related themes

Play the Python quizzes