Builtins Easy¶
What does this code print?¶
print(len([1, 2, 3, 4]))
034TypeError
Answer¶
len returns the number of items in a container; the list has 4 elements.
What does this code print?¶
print(sorted([3, 1, 2]))
[1, 2, 3][3, 2, 1]None[3, 1, 2]
Answer¶
sorted returns a new list in ascending order; it never mutates its
argument and never returns None.
What does this code print?¶
print(sum([1, 2, 3]))
[1, 2, 3]12306
Answer¶
sum adds the items of an iterable starting from 0: 1 + 2 + 3 == 6.
What does this code print?¶
print(bool([]))
TrueFalse[]None
Answer¶
An empty list is falsy, so bool([]) is False. Empty containers are
always falsy.
What does this code print?¶
print(list("ab"))
['ab']['a', 'b']'ab'[97, 98]
Answer¶
list on a string iterates its characters, producing a one-character
string per element.
What does this code print?¶
print(max([4, 2, 9, 1]))
14916
Answer¶
max returns the largest item of the iterable, which is 9.
What does this code print?¶
print(min([4, 2, 9, 1]))
1249
Answer¶
min returns the smallest item of the iterable, which is 1.
What does this code print?¶
print(list(range(3)))
[1, 2, 3][0, 1, 2][0, 1, 2, 3][3]
Answer¶
range(3) yields 0, 1, 2 — it starts at 0 and stops before
the given stop value.
What does this code print?¶
print(int("100"))
4"100"1.0100
Answer¶
int parses a decimal string into an integer, giving 100.
What does this code print?¶
print(len({"a": 1, "b": 2}))
4213
Answer¶
len of a dict counts its keys; there are 2.
What does this code print?¶
print(list(enumerate(["x", "y"])))
[('x', 0), ('y', 1)][(1, 'x'), (2, 'y')][(0, 'x'), (1, 'y')]['x', 'y']
Answer¶
enumerate pairs each item with a counter starting at 0, yielding
(index, value) tuples.
What does this code print?¶
print(bool(0))
TrueFalse0None
Answer¶
The number 0 is falsy, so bool(0) is False.
What does this code print?¶
print(sorted("dbca"))
['a', 'b', 'c', 'd']'abcd'['d', 'c', 'b', 'a']['dbca']
Answer¶
sorted always returns a list, even when given a string: it iterates
the characters and sorts them.
What does this code print?¶
print(tuple([1, 2]))
[1, 2](1, 2){1, 2}(1, 2,)
Answer¶
tuple builds an immutable sequence from the iterable, printed as
(1, 2).
What does this code print?¶
print(set([1, 1, 2, 2]))
[1, 1, 2, 2]{1, 1, 2, 2}(1, 2){1, 2}
Answer¶
A set holds only unique elements, so the duplicates collapse to
{1, 2}.
What does this code print?¶
print(float("3.5"))
3"3.5"3.5ValueError
Answer¶
float parses a numeric string, including a decimal point, into a
floating-point number.
What does this code print?¶
print(any([False, True, False]))
TrueFalse1None
Answer¶
any is True if at least one element is truthy; here one True
is enough.
What does this code print?¶
print(all([True, True, False]))
TrueFalse0None
Answer¶
all is True only if every element is truthy; the single
False makes it False.
What does this code print?¶
print(sum([]))
None[]TypeError0
Answer¶
sum of an empty iterable returns its start value, which defaults to
0.
What does this code print?¶
print(divmod(17, 5))
(2, 3)3.4(3, 2)(3.4, 0)
Answer¶
divmod(a, b) returns (a // b, a % b) — the quotient 3 and the
remainder 2.