Python Flashcards

1
Q

What are the logical operators in Python

A

and, or, and not

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

What are the bitwise operators in Python

A

&, |, and ^

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

What are the priorities of the sequence operations in Python

A

x in s, x not in s
s + t, s * n or n * s
s[i], s[i:j], s[i:j:k]
len(s)
min(s)
max(s)
s.index(x[, i[, j]])
s.count(x)

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

What sequence operation is used to test if an item exists in a sequence

A

x in seq True if x exists in seq, False otherwise
x not in seq False if x exists in seq, True otherwise

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

What sequence operation is used to concatenate two sequences

A

seq1 + seq2

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

What sequence operation is used to repeat a sequence

A

seq * n or n*seq

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

What sequence operation is used to return an element or slice of a sequence

A

seq[i] return the ith element
seq[i:j] slice from i to j, not including j
seq[i:j:k] slice from i to j, not including j, with step k

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

What sequence operation is used to get the length of a sequence

A

len(seq) the length of seq

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

What sequence operation is used to get the minimum value of a sequence

A

min(seq) the smallest item of seq

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

What sequence operation is used to get the maximum value of a sequence

A

max(seq) the largest item of seq

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

What sequence operation is returns the index of an object in a sequence

A

seq.index(x[,i[, j]]) index of the first occurrence of x (at or after index i and before index j)

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

What sequence operation is counts the number of occurrences of an object in a sequence

A

seq.count(x) total number of occurrences of x in seq

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

What are the basic sequence types in Python

A

list, tuple, range

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

What are Falsey values in Python

A

Constants defined to be false: None and False
Numeric types that are zero: 0, 0.0, 0j, Decimal(0), Fraction(0, 1)
Empty sequences and collections: '', (), [], {}, set(), range(0)

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

What are Truthy values in Python

A

Constants defined to be true: True
Numeric types that are not zero
Non-empty sequences or collections

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

What data types are valid keys for a hash map/dictionary

A

Objects: Integer, Float, String, Boolean
Immutable sequences: String, Tuple, Range, Frozenset

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

For small inputs, an array can be faster than a hash map. Why?

A

There is overhead associated with a hash map such as the hash function

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

When designing a hash map what are some good design choices

A
  1. Use a prime number inf your hash function which helps spread the keys more evenly
  2. Design a hash function that spreads hashes evenly to avoid collisions
  3. Implement a method to handle collisions: chaining or open addressing
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

How can simulate a stack using a list

A
stack = []
stack.append(3) # push
stack.pop() # pop
len(stack) # num of element
stack[-1] # peek
if not stack # check empty
20
Q

How can you simulate a queue

A
# Initialize a queue
from collections import deque
queue = deque()
queue = deque([1,2,3])

queue.append(4) # Enqueue
queue.pop()     # deque
queue[0]        # peek
len(queue)      # get size
21
Q

What built-in function returns the absolute value of a number

A

abs(x)

>>> abs(-10)
10
>>> abs(-3.145)
3.145
22
Q

What built-in function converts a number to a binary string

A

bin(x)

>>> bin(10)
'0b1010'
>>> format(10,'b')
'1010'
23
Q

What built-in function returns the string representing a character whose Unicode code point is the integer i

A

chr(i)

>>> chr(97)
'a'
>>> chr(ord('a'))
'a'
24
Q

What built-in function takes two (non-complex) numbers as arguments and returns a pair of numbers consisting of their quotient and remainder when using integer division

A

divmod(a, b)

>>> divmod(30,4)
(7, 2)
25
Q

What built-in function returns an enumerate object

A

enumerate(iterable, start=0)

>>> list(enumerate(['a','b','c']))
[(0, 'a'), (1, 'b'), (2, 'c')]
26
Q

What built-in function returns a floating point number from a number or a string

A

float(x)

>>> float(1)
1.0
>>> float("   -1.345\n")
-1.345
27
Q

What built-in function returns a new frozenset object

A

frozenset(iterable=set())

>>> frozenset( [1,2,2,3,4,5,5])
frozenset({1, 2, 3, 4, 5})
28
Q

What built-in function returns the hash value of an object

A

hash(object)

>>> hash('foo')
9059890961789401645
>>> hash(1)
1
>>> hash(2)
2
>>> hash(True)
1
>>> hash(False)
0
>>> hash((1,2,5))
2143910794424799510
29
Q

What built-in function returns the hexadecimal string of an integer

A

hex(x)

>>> hex(420)
'0x1a4'
>>> format(420, 'x')
'1a4'
>>> format(420, 'X')
'1A4'
30
Q

What built-in function returns an integer object from a number or a string

A

int(number=0)
int(string, base=10)

>>> int("1234")
1234
>>> int(123.45)
123
>>> int("1234")
1234
>>> int('   -12_345\n')
-12345
>>> int('0x1a4', 0)
420
>>> int('0b110100100', 0)
420
>>> int('110100100', 2)
420
31
Q

What built-in function returns the length (number of items) in an object

A

len(s)

32
Q

What built-in function returns a new list object from an iterable

A

list(iterable)

33
Q

What built-in function returns a map object from a function and multiple iterables

A

map(function, iterable, *iterables)

If additional iterables arguments are passed, function must take that many arguments and is applied to the items from all iterables in parallel.

34
Q

What built-in function returns the largest item in an iterable or the largest of two or more arguments

A

max(iterable, *, key=None)
max(iterable, *, default, key=None)
max(arg1, arg2, *args, key=None)

35
Q

What built-in function returns the smallest item in an iterable or the smallest of two or more arguments

A

min(iterable, *, key=None)
min(iterable, *, default, key=None)
min(arg1, arg2, *args, key=None)

36
Q

What built-in function converts an integer to an octal string

A

oct(x)

>>> oct(8)
'0o10'
>>> oct(-56)
'-0o70'
>>> format(10, 'o')
'12'
37
Q

What built-in function returns an integer representing a Unicode character

A

ord(c)

>>> ord('a')
97
38
Q

What built-in function returns the base to the power, e.g. 2^4

A

pow(base, exp, mod=None)

>>> pow(2,4)
16
>>> 2**4
16
39
Q

What built-in function returns a range object

A

class range(stop)
class range(start, stop, step=1)

40
Q

What built-in function reverses an sequence

A

reversed(seq)

seq must implement the \_\_reversed\_\_() method

41
Q

What built-in function returns a new set object from an iterable

A

set(iterable)

42
Q

What built-in function returns a slice of an object

A

slice(stop)
slice(start, stop, step=None)

Slice objects are also generated when extend indexing syntax is being used, e.g. a[start:stop:step]

43
Q

What built-in function returns a new sorted list from an iterable

A

sorted(iterable, /, *, key=None, reverse=False)

44
Q

What built-in function returns a string version of an object

A

str(object='')
str(object=b'', encoding='utf-8', errors='strict')

45
Q

What built-in function returns sum of an iterable

A

sum(iterable)
sum(iterable /, start =0)

46
Q

What built-in function returns a new tuple object from an iterable

A

tuple(iterable)

47
Q

What built-in function is used to iterate over several iterables in parallel

A

zip(*iterables, strict=False)

>>> for item in zip([1,2,3], ['a','b','c']):
...     print(item)
... 
(1, 'a')
(2, 'b')
(3, 'c')