The Python Tutorial: Chapter 5 - Data Structures Flashcards

1
Q

What does .extend() do?

What are its arguments?

A
  1. Takes a list as input.

2. Just a list

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

What’s another function for getting rid of all the items in the list besides using the del clause?

A

.clear()

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

What do .index and .count do and what are the arguments?

A

.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

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

Creating a stack with a list is intuitive with pop and append, but how do you create a queue? (FIFO instead of LIFO)

A
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’

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

If a list comprehension has two ‘for’s in it, which one will be evaluated first?

A

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])

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

What does the zip function do? Give an example

A

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)]

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

Give two examples of using the dict() function

A

> > > 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}

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

Give an example of a set comprehension and a dictionary comprehension

A

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}

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

Give an example of sequence unpacking

A

> > > 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)

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

What are the differences between a tuple and lists and when would you use a tuple?

A
  1. Tuples are immutable, lists are not

2. Tuples are usually heterogeneous, lists are not

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

What are the 7 types of sequences?

A

strings, Unicode strings, lists, tuples, bytearrays, buffers, and xrange objects
Source: https://en.wikibooks.org/wiki/Python_Programming/Sequences

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

What do is and is not do?

A

The compare the location of an object in memory. To check the actual location use the id() function.

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

What is the hierarchy of execution for: comparison operators (not in, in, is, is not), boolean operators (and, or, not), and numerical operator comparisons?

A
  1. Numerical
  2. Comparison
  3. Boolean
    a. not has highest priority and or has the lowest
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the walrus operator and what does it do?

A
\:=
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/

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