The Python Tutorial: Chapter 5 - Data Structures Flashcards
What does .extend() do?
What are its arguments?
- Takes a list as input.
2. Just a list
What’s another function for getting rid of all the items in the list besides using the del clause?
.clear()
What do .index and .count do and what are the arguments?
.index returns the index of the first item for whatever item you are looking for. The args are x,[start,[end]]
count counts the number of times x appears in a list
Creating a stack with a list is intuitive with pop and append, but how do you create a queue? (FIFO instead of LIFO)
You use the collections package with the deque module E.g. >>> from collections import deque
> > > queue = deque([“Eric”, “John”, “Michael”])
> > > queue.append(“Terry”) # Terry arrives
> > > queue.append(“Graham”) # Graham arrives
> > > queue.popleft() # The first to arrive now leaves
‘Eric’
> > > queue.popleft() # The second to arrive now leaves
‘John’
If a list comprehension has two ‘for’s in it, which one will be evaluated first?
The last one
E.g.
»»» [[row[i] for row in matrix] for i in range(4)]
[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
As we saw in the previous section, the nested listcomp is evaluated in the context of the for that follows it, so this example is equivalent to:
> > > > > > transposed = []
for i in range(4):
… transposed.append([row[i] for row in matrix])
What does the zip function do? Give an example
It combines lists into tuples.
E.g.
Combines lists together to make tuples at index position
E.g.
>>> matrix = [ ... [1, 2, 3, 4], ... [5, 6, 7, 8], ... [9, 10, 11, 12], ... ]
> > > list(zip(*matrix))
[(1, 5, 9), (2, 6, 10), (3, 7, 11), (4, 8, 12)]
Give two examples of using the dict() function
> > > dict([(‘sape’, 4139), (‘guido’, 4127), (‘jack’, 4098)])
{‘sape’: 4139, ‘guido’: 4127, ‘jack’: 4098}
> > > dict(sape=4139, guido=4127, jack=4098)
{‘sape’: 4139, ‘guido’: 4127, ‘jack’: 4098}
Give an example of a set comprehension and a dictionary comprehension
Set Comprehension:
»> a = {x for x in ‘abracadabra’ if x not in ‘abc’}
> > > a
{‘r’, ‘d’}
Dictionary Comprehension:
»> {x: x**2 for x in (2, 4, 6)}
{2: 4, 4: 16, 6: 36}
Give an example of sequence unpacking
> > > x, y, z = t
Items on the left need to be as long as the tuple.
This can be used for any sequence (lists, strings, tuples)
What are the differences between a tuple and lists and when would you use a tuple?
- Tuples are immutable, lists are not
2. Tuples are usually heterogeneous, lists are not
What are the 7 types of sequences?
strings, Unicode strings, lists, tuples, bytearrays, buffers, and xrange objects
Source: https://en.wikibooks.org/wiki/Python_Programming/Sequences
What do is and is not do?
The compare the location of an object in memory. To check the actual location use the id() function.
What is the hierarchy of execution for: comparison operators (not in, in, is, is not), boolean operators (and, or, not), and numerical operator comparisons?
- Numerical
- Comparison
- Boolean
a. not has highest priority and or has the lowest
What is the walrus operator and what does it do?
\:= This is the walrus operator and it assigns and returns a value in the same expression, which simplifies your code. This is only available in Python 3.8 and later. E.g. >>> print(walrus := True) True >>> type(walrus)
Source: https://realpython.com/lessons/assignment-expressions/