Python Real Python Interview Flashcards

1
Q

What can you use on a list instad of range to produce keys and values?

A

Show example of enumerate with fizzbuzz

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

Show an example of list comprehensions vs map and filter on a list [4, 2, 1, 6, 9, 7] to produce the sq and to show if odd

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

Show the difference between sort and sorted and show sorted features

A

reverse and key

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

Describe the features of sets

A

unordered and unique

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

demonstrate the use of sets with the list of random words

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

show how generators are better than list comprehensions for very large sequences. Use the sum of square roots example with 1000 integers.

Show a basic example of a generatior expression for a short range

A

Show a basic example of a generatior expression for a short range
using (), next, show StopIetration

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

Show a better example of checking if a key exists in a dict and returning a default value if not

A

show an example of .get

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

Show a better example of setting a key in a dict if it doesn’t already exist

A

show an example of .setdefault

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

Show an example of using default dict to create a dictionary based on the following list :
grades = [
… (‘elliot’, 91),
… (‘neelam’, 98),
… (‘bianca’, 81),
… (‘elliot’, 88),
… ]

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

Create a function that returns the top three most frequent letters given a string. e.g.

top_three_letters(“aabbccc”)
[(‘c’,3), (‘b’,2) (‘a’,1)]
top_three_letters(“aabbccd”)
[(‘a’,2), (‘b’,2) (‘c’,2)]

Show a long winded example and an example using collections

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

What are other names for de-queue

A

Deque (desk), double-ended queue

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

list the common pythoon data structures and operations on them, what are the times of the operations, e.g., constant time

A

https://realpython.com/python-data-structures/
The above looks very good

list, pop, append, insert
dequeue appendleft, popleft, append, constant time

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

Show an example of named tuples vs standard tuples and classes

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

show how to time operations

A

use %timeit in iPython

e.g., %timeit is_upper(‘Hello WORLD”)
from timeit import timeit

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

what is a good data structure to use for checking membership with an example

A

use sets

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

What is a good operation for checking if all values are the same?

A
17
Q

What does the itertools module allow you to do?

A

itertools.repeat
itertools.cycle
itertools.permutations
itertools.combinations

18
Q

Give an example of itertools usage

A

itertools.repeat example
itertools.cycle examples
itertools.permutations examples
itertools.combinations examples

19
Q

what is the property decorator?

A

https://www.freecodecamp.org/news/python-property-decorator/

@property can be considered the “pythonic” way of defining getters, setters, and deleters.
By defining properties, you can change the internal implementation of a class without affecting the program, so you can add getters, setters, and deleters that act as intermediaries “behind the scenes” to avoid accessing or modifying the data directly.

20
Q

Describe the functools module

A

You can use the cached_property decorator to avoid having to process the same information repeatedly (providing the returned data doesn’t change)

21
Q

what are the two functools cache decorators that you can use and what are the differences?

A

@cached_property and @lru_cache