Python | Basics | Priority Flashcards

1
Q

What is the fastest way to reverse a string?

A

backward = text[::-1]

https://datagy.io/python-reverse-string/

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

Returns the __dict__ attribute for a module, class, instance, or other object that has a __dict__ attribute. Let’s say you only wanted to print the object’s instance attributes as well as their values, we can use the vars() function.

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

Without any arguments, the function returns the list of names in the current local scope. If an argument is passed in, returns a list of valid attributes for that object. Prints out of all the attributes of a Python object, including the ones that are defined in the class definition.

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

Python built-in to call pdb debugger.

python-data-structures

A

breakpoint()

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

How to sort a list of complex record objects by a specific key of a record.

python-data-structures

A
sorted(animals, key=lambda x: x[specific_key])
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How to randomly choose a member of a set.

python-standard-library

A
import random
random.choice(set)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Example of how to get the sum of squares of ints with constant time complexity.

python-data-structures

A

sum((i * i for i in range(1, 1001)))

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

Example of how to get an item from a dict with a default.

python-data-structures

A
name = cowboy.get('name', 'The Man with No Name')
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Example of how to set a default in a dict.

python-data-structures

A
name = cowboy.setdefault('name', 'The Man with No Name')
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How to initialize a dict with list values.

python-standard-library

A
from collections import defaultdict; student_grades = defaultdict(list)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Example of how to: get the frequencies of words in a list: get the most frequent k words.

A
from collections import Counter; counts = Counter(words); counts.most_common(k)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

List several string constants.

A

string.ascii_letters
string.ascii_uppercase
string.ascii_lowercase
string.digits
string.hexdigits
string.octdigits
string.punctuation
string.printable
string.whitespace

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

Example of how to get permutations from a list.

A
list(itertools.permutations(friends, r=2))
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does a with statement do?

2.3 p35

A

Simplifies exception handling by encapsulating
standard uses of try/finally statements in so-called context
managers.

2.3 p35

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

How to make a class/object callable?

3.1 p66

A

def __call__(self, …):

3.1 p66

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

Example of how to create a namedtuple object. Example of how to use the created object as a factory function.

Python Tricks 4.6 What Namedtuples Are Good For

A

”»> from collections import namedtuple
»> Car = namedtuple(‘Car’ , ‘color mileage’); my_car = Car(‘red’, 3812.4)”

17
Q

Class representing an enum.

A
from enum import Enum
class SUIT(Enum):
    HEART, SPADE, CLUB, DIAMOND = 1, 2, 3, 4
18
Q

Get the max int.

A

sys.maxint

19
Q

Example of how to create an Enum object with the functional API.

realpython https://realpython.com/python-enum/

A

”»> HTTPMethod = Enum(
… ““HTTPMethod””, ["”GET””, ““POST””, ““PUSH””, ““PATCH””, ““DELETE””]
… )”

20
Q

Example of how to get a namedtuple as a dict; how to create a JSON string; replace a property;

Python Tricks 4.6 What Namedtuples Are Good For

A

> > > my_car._asdict();&raquo_space;> json.dumps(my_car._asdict());&raquo_space;> my_car._replace(color=’blue’)

21
Q

Functions to get the top/bottom k elements from a heapq.

A

nlargest(), nsmallest()

22
Q

Function to merge lists into a heap. What does this return?

A

heapq.merge(); iterator (not a list)

23
Q

Functions to add to and remove from heapq.

A

heapq.heappush(), heapq.heappop()

24
Q

How to initialize a heap.

A

pq = [] OR heapq.heapify(a) # a is a list