Functions Flashcards

1
Q

Why use functions in python?

A

They allow us to encapsulate logic in our code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you specify default arguments in functions?

A

def func(def_arg = value)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Why shouldn’t you use mutable default arguments in python?

A

It changes the result of a function at each call.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Explain how *args and **kwargs work in python, give an example of when they might be useful

A
  • args : allow an arbitrary number of positional arguments

* *kwargs : allow an arbitrary number of key-word args.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What comes first, positional arguments or default arguments?

A

positional then default

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does it mean to say that a function has side effects? Why are they not recommended?

A

Side effects are where calling the function mutates their inputs or other parts of the programme. This can be a source of bugs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

When a function is executed, a namespace is created, what is inside this namespace?

A

All variables/function parameters defined within the function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How would you modify global variables inside a function?

A

global

… code …

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Briefly explain what a decorator does, what are its inputs and outputs?

A

A decorator modifies a function at run time. It takes a function as input, and outputs a modified version.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Give an example of when a decorator might be useful

A

It allows the modification of one isntance of calling the function, without which you would have to define multiple similar functions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Explain what a generator is? Which keyword do they use? How can it be useful?

A

for …:
… code …
yield x
Useful since each element of the sequence isn’t stored, only calculated at each iteration.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you iterate over a generator?

A

for i in :
… code …
to get the next in sequence: next()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Write a list comprehension using an if-else statement

A

[x ** 2 for x in range(100) if x%2 == 0 else x ** x]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Write a list comprehension using a lambda function

A

[(lambda x : x**2)(x) for x in range(100)]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do list comprehensions differ from generator comprehension? What is the difference in syntax?

A

list comprehensions are performed all at once, generator comprehensions are performed at each iteration.
[list comp]
(generator comp)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does a Map Function do?

A

map(func, list) applies a function to each element of a list

17
Q

What does a Filter Function do?

A

Indexes based upon a conditional statement.

18
Q

When might a Zip function be useful?

A

When we want to iterate over pairs of objects.