Python

Sequences: Lists & Tuples

35 flashcards · answers and spaced-repetition review in the KnowCard app

What does this print? ``` s = "haystackNEEDLE" print(s[8:14]) ```
What does this print? ``` name = "Python" print(name[-2]) ```
What does this print? ``` s = "Much less" print(s[1337:2674]) print(s[100]) ```
What does this print? ``` name = "ytnoM Python" print(name[4::-1]) ```
What is the idiomatic way to reverse a sequence with a slice, and what does it return?
What does this print, and what's the key fact about the result? ``` s1 = ["thesis"] s2 = s1[:] print(s1 == s2, s1 is s2) ```
What does this print? ``` x = [["nice"]] * 3 x[0][0] = "oops" print(x) ```
What does this print? ``` print([2, 3] in [1, 2, 3, 4]) ```
What does this print? ``` s = [1, 2] t = s s += [3, 4] print(t) ```
What's the difference between s.append(t) and s.extend(t) when t is a list?
What does this print? ``` result = [1, 2, 3].append(4) print(result) ```
What's the difference between list.sort() / list.reverse() and sorted() / reversed()?
What does this print? ``` l = ["Bob", "Catherine", "Al"] l.sort(key=len) print(l) ```
What does this print? ``` s = ["W", "o", "o", "h", "o"] s.remove("o") s.pop(1) print(s) ```
What does this print? ``` lst = [1, 2, 3, 4] for x in lst: if x % 2 == 0: lst.remove(x) print(lst) ```
What does this print? ``` no_tuple = (2) print(type(no_tuple).__name__) ```
What does this print? ``` a, b = 1, 2 a, b = b, a print(a, b) ```
What does this print? ``` first, *rest, last = [10, 20, 30, 40, 50] print(first, rest, last) ```
Why can a tuple be used as a dict key but a list cannot?
What does this print? ``` a = ([],) a[0].append("moved") print(a) ```
What does this print? ``` s = [0, 1, 2, 3, 4, 5] s[1:4] = ["a"] print(s) ```
What does this print? ``` s = [9, 8, 7, 6, 5, 4, 3, 2, 1] del s[3:6] print(s) ```
Python has five built-in sequence types. Name them, and say which are mutable vs immutable.
How do s.index(x) and s.count(x) behave, and how does each handle a missing element?
What does this print? print([1, 2, *[3, 4], 5])
Can you grow a list by assigning past its end, e.g. s = [1, 2, 3] s[3] = 4
How does a stepped slice assignment s[i:j:k] = t differ from a plain one s[i:j] = t?
What does this print? s = [1, 2, 3] s.insert(99, "x") s.insert(-99, "y") print(s)
What does this print? lst = [3, "one", 2] lst.sort()
What does this print? l = ["Bob", "Catherine", "Al"] l.sort(len)
What does it mean that Python's list.sort() is stable, and how do you exploit it for a multi-key sort?
What does this print? a = [] a.append(a) print(a)
Rewrite this loop as a list comprehension: result = [] for x in nums: result.append(x 2)
What does this print? print([(a, b) for a in [1, 2] for b in [3, 4] if a + b != 5])
What does this print? print(list(range(0, 10, 2))) print(tuple("ab")) print(list())

Start learning today

Free to start — download the app or use it in your browser.

Get it on App StoreGet it on Google Play
Sequences: Lists & Tuples (Python) · KnowCard