Python
lambda, Recursion & Built-in Functions
43 flashcards · answers and spaced-repetition review in the KnowCard app
A teammate wants a lambda that prints a value and returns it: lambda x: print(x); return x. Why won't this work?
You need to sort people by their age attribute. A colleague writes people.sort(lambda p: p.age). What's wrong?
What does this print?
```
funcs = [lambda: i
for i in range(3)]
print([f() for f in funcs])
```
In a loop you build callbacks with lambda: x and they all end up using the last value of x. What's the standard fix?
What does this print?
```
def fac(n):
if n > 1:
return fac(n-1) * n
return 1
print(fac(0))
```
A deep recursive function on a big input dies with RecursionError: maximum recursion depth exceeded. Why, and what's the usual fix?
What does this print?
```
r = map(lambda x: x*2, [1, 2, 3])
print(len(r))
```
What does this print?
```
m = map(str, [1, 2, 3])
print(list(m))
print(list(m))
```
What does this print?
```
a = [1, 2, 3]
b = [10, 20]
print(list(zip(a, b)))
```
What does this print?
```
m = [[1, 2, 3],
[4, 5, 6]]
print(list(zip(*m)))
```
What does this print?
```
for i, c in enumerate("ab", start=1):
print(i, c)
```
A function does nums.sort() then return nums.sort() to hand back the ordered list, but the caller gets None. Why?
What does this print?
```
words = ["bb", "a", "ccc"]
print(sorted(words,
key=len,
reverse=True))
```
What does this print?
```
print(all([]))
print(any([]))
```
What does this print?
```
nums = [1, 2, 3, 4]
print(any(n > 10 for n in nums))
print(all(n > 0 for n in nums))
```
What does this print?
```
print(max([], default=0))
print(min([5, 3, 8]))
```
You want the longest string in words. A colleague writes max(words, len). What goes wrong, and what's correct?
What does this print?
```
print(sum([2, 3], 10))
print(sum([]))
```
What does this print?
```
r = range(1, 1000000)
print(r[2])
print(2 in r)
```
What does this print?
```
for x in reversed(range(3)):
print(x, end=" ")
```
What does this print?
```
print(isinstance(True, int))
print(isinstance(1, bool))
```
What does this print?
```
class A: pass
class B(A): pass
print(issubclass(B, A))
print(issubclass(A, A))
```
How do repr("Püthon") and ascii("Püthon") differ?
In Python 3, what does round(0.5) return? And round(2.5)?
What does pow(7, 5, 4) compute, and why prefer it over 7 ** 5 % 4?
How do max([1, 5, 2]) and max(1, 5, 2) differ, and what does max("hello") return?
You call min([1, 2, "world"]). What happens?
What does this print?
print(bin(5))
print(hex(255))
print(oct(8))
print(bin(-12))
What does int("FF", 16) return, and what about int("10", 2)?
What does divmod(11, 4) return, and how does it relate to // and %?
Both eval and exec run code from a string. What's the key difference, and why are they dangerous?
What does this print?
r = filter(lambda x: x % 2 == 0, range(6))
print(list(r))
What does this print?
r = map(lambda x, y: x + y, [1, 2, 3], [10, 20, 30])
print(list(r))
In Python 3, a = input() then a + 1 raises TypeError. Why?
Your set can't be used as a dict key or placed inside another set. Which built-in fixes this?
What do globals() and locals() return, and how do they differ?
What's the difference between id(obj) and hash(obj), and when does hash fail?
How can you check at runtime whether an object can be called like a function?
What does vars(obj) return, and what does vars() with no argument do?
Instead of import pdb; pdb.set_trace(), which single built-in drops you into the debugger?
Can a lambda contain an if-statement? And how do you call a lambda without naming it first?
What does this print?
print(chr(80))
print(ord("P"))
What does this print?
print(list(range(2, 10, 2)))
print(list(range(10, 0, -2)))
Start learning today
Free to start — download the app or use it in your browser.
