Python Flashcards

1
Q

Create an array of 0s

A

temp = [0]*(r-l+1)

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

sort an array

A

arr.sort()

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

iterate over keys in dict

A

for key in dict: print(key,dict[key])

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

defaultdict

A
from collections import defaultdict
d = defaultdict(int)

seen = defaultdict(set)
for x in range(1000):
for y in range(1000):
seen[x].add(y) # insert a new pair
assert y in seen[x] # check if pair in dictionary

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

Declare a set

A

By default h = {} is a map so for set, we do h= set()

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

reverse list

A

list.reverse() -> in place

new_list = list[::-1] -> extra space

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

comparator

A

from functools import cmp_to_key

key=cmp_to_key(lambda x,y: 1 if str(y)+str(x) > str(x)+str(y) else -1))

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

iterate digits of a number if need to iterate msb to lsb

A
dictmap = {
'0' : 0,
'1' : 1,
'2' : 2,
'3' : 3,
'8' : 8,
'9' : 9
}
def each_dig_in_num(n):
dm = dictmap #faster if local
s = repr(n) #repr is faster than str
for char in s:
foo(dm[char])
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

iterate digits of a number if order doesnโ€™t matter

A
n2 = n
while n2 > 0:
d = n2 % 10
n2 /= 10
foo(d)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

other ways to iterate digits of a number using typecasting

A

2.

n = 12345

#1.
s = str(n)
for i in s:
d = int(i)
foo(d)

nl = map(int, str(n))
for d in nl:
foo(d)

nl = [int(x) for x in str(n)]
for d in nl:
foo(d)

Of those, I have, a bit surprised, found that #1 is the fastest, and
that #2 using map() is faster than #3 using list comprehension. I also
registered that that repr(n) is about 8% faster than str(n)

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

stack using deque

A

from collections import deque
stack = deque()

Deque is preferred over list in the cases where we need quicker append and pop operations from both the ends of the container, as deque provides an O(1) time complexity for append and pop operations as compared to list which provides O(n) time complexity.

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

min heap (default)

A

heapify(), heappush() and heappop()

# importing "heapq" to implement heap queue 
import heapq 

li = [5, 7, 9, 1, 3] # initializing list

# using heapify to convert list into heap 
heapq.heapify(li) 
# printing created heap 
print (list(li)) 
# using heappush() to push elements into heap 
heapq.heappush(li,4) 
# using heappop() to pop smallest element 
print (heapq.heappop(li))
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Heap operations

A

import heapq

  1. heapify(iterable)
  2. heappush(heap, ele)
  3. heappop(heap)
  4. heappushpop(heap, ele)
  5. heapreplace(heap, ele): In this, element is first popped, then the element is pushed.i.e, the value larger than the pushed value can be returned. heapreplace() returns the smallest value originally in heap regardless of the pushed element as opposed to heappushpop().
  6. nlargest(k, iterable, key = fun)
  7. nsmallest(k, iterable, key = fun)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Integer division of negative numbers

A

Because // is floor division, so rounding down, not towards zero.-22 / 10 is -2.2, rounding down makes that -3

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

deque operations

A

append() :- This function is used to insert the value in its argument to the right end of deque.
appendleft() :- This function is used to insert the value in its argument to the left end of deque.
pop() :- This function is used to delete an argument from the right end of deque.
popleft() :- This function is used to delete an argument from the left end of deque.

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

Want to Stand Out in Python Job Interviews?

A

๐Ÿญ. ๐—จ๐˜€๐—ฒ โ€œ๐˜๐˜†๐—ฝ๐—ฒ ๐—ต๐—ถ๐—ป๐˜๐˜€โ€ ๐˜๐—ผ ๐—บ๐—ฎ๐—ธ๐—ฒ ๐—ฐ๐—ผ๐—ฑ๐—ฒ ๐—บ๐—ผ๐—ฟ๐—ฒ ๐—ฟ๐—ฒ๐—ฎ๐—ฑ๐—ฎ๐—ฏ๐—น๐—ฒ: Type hints make it easier to understand the expected types of function arguments and return values. For example, def demo(x: int, y: str, z: float = 100) -> bool:.

๐Ÿฎ. ๐—จ๐˜€๐—ฒ ๐—ฝ๐—ฟ๐—ผ๐—ฝ๐—ฒ๐—ฟ ๐—ฒ๐˜…๐—ฐ๐—ฒ๐—ฝ๐˜๐—ถ๐—ผ๐—ป ๐—ต๐—ฎ๐—ป๐—ฑ๐—น๐—ถ๐—ป๐—ด: Catch specific exceptions to understand what went wrong in the code. Avoid using bare except clause that catches all the exceptions.
For example,
try:
some_code()
except ValueError:
handle_value_error()

๐Ÿฏ. ๐—จ๐˜€๐—ฒ โ€œ๐—ฐ๐—ผ๐—ป๐˜๐—ฒ๐˜…๐˜ ๐—บ๐—ฎ๐—ป๐—ฎ๐—ด๐—ฒ๐—ฟ๐˜€โ€ ๐—ณ๐—ผ๐—ฟ ๐—ฟ๐—ฒ๐˜€๐—ผ๐˜‚๐—ฟ๐—ฐ๐—ฒ ๐—บ๐—ฎ๐—ป๐—ฎ๐—ด๐—ฒ๐—บ๐—ฒ๐—ป๐˜: This ensures that resources such as file handles and database connections are properly managed and closed.
For example, with open(โ€œdemo.txtโ€, mode=โ€wโ€) as file:

๐Ÿฐ. ๐—จ๐˜€๐—ฒ โ€œ๐—ฒ๐—น๐˜€๐—ฒโ€ ๐˜„๐—ถ๐˜๐—ต โ€œ๐—ณ๐—ผ๐—ฟโ€ ๐—ฎ๐—ป๐—ฑ โ€œ๐˜„๐—ต๐—ถ๐—น๐—ฒโ€ ๐—น๐—ผ๐—ผ๐—ฝ๐˜€: Itโ€™s the pythonic way of indicating that all items were processed in their natural course.

๐Ÿฑ. ๐—จ๐˜€๐—ฒ ๐—ฑ๐—ถ๐—ฐ๐˜.๐—ด๐—ฒ๐˜() ๐—บ๐—ฒ๐˜๐—ต๐—ผ๐—ฑ: It provides a way to avoid errors when a key is not present and allows you to return a default value when the key is not present. For example, my_dict.get(โ€˜keyโ€™, default_val).

๐Ÿฒ. ๐—จ๐˜€๐—ฒ โ€œ๐—ถ๐˜€๐—ถ๐—ป๐˜€๐˜๐—ฎ๐—ป๐—ฐ๐—ฒโ€ ๐˜๐—ผ ๐—ฐ๐—ต๐—ฒ๐—ฐ๐—ธ ๐—ณ๐—ผ๐—ฟ โ€œ๐—ผ๐—ฏ๐—ท๐—ฒ๐—ฐ๐˜ ๐˜๐˜†๐—ฝ๐—ฒโ€ ๐—ฒ๐—พ๐˜‚๐—ฎ๐—น๐—ถ๐˜๐˜†: Itโ€™s the recommended way to compare object types in Python, especially with inherited objects.

๐Ÿณ. ๐—จ๐˜€๐—ฒ โ€œ๐—ถ๐˜€โ€ ๐—ณ๐—ผ๐—ฟ ๐—ฒ๐—พ๐˜‚๐—ฎ๐—น๐—ถ๐˜๐˜† ๐˜„๐—ถ๐˜๐—ต ๐˜€๐—ถ๐—ป๐—ด๐—น๐—ฒ๐˜๐—ผ๐—ป: Use is to check for None, True, and False instead of ==. It checks if the two items reference the same object in memory, while == checks for value equality.

๐Ÿด. ๐—จ๐˜€๐—ฒ ๐—ณ๐˜‚๐—ป๐—ฐ๐˜๐—ถ๐—ผ๐—ป๐—ฎ๐—น ๐—ฝ๐—ฟ๐—ผ๐—ด๐—ฟ๐—ฎ๐—บ๐—บ๐—ถ๐—ป๐—ด ๐˜๐—ฒ๐—ฐ๐—ต๐—ป๐—ถ๐—พ๐˜‚๐—ฒ๐˜€ ๐˜€๐˜‚๐—ฐ๐—ต ๐—ฎ๐˜€ ๐—บ๐—ฎ๐—ฝ, ๐—ฟ๐—ฒ๐—ฑ๐˜‚๐—ฐ๐—ฒ, ๐—ฎ๐—ป๐—ฑ ๐—ณ๐—ถ๐—น๐˜๐—ฒ๐—ฟ ๐˜๐—ผ ๐—บ๐—ฎ๐—ธ๐—ฒ ๐—ฐ๐—ผ๐—ฑ๐—ฒ ๐—ฒ๐—ณ๐—ณ๐—ถ๐—ฐ๐—ถ๐—ฒ๐—ป๐˜: They provide a concise way of transforming and filtering data in a list. For example, squared = map(lambda x: x*x, [1, 2, 3]).

๐Ÿต. ๐—จ๐˜€๐—ฒ โ€œ๐—ฑ๐—ผ๐—ฐ๐˜€๐˜๐—ฟ๐—ถ๐—ป๐—ด๐˜€โ€ ๐—ฎ๐—ป๐—ฑ โ€œ๐—ฐ๐—ผ๐—บ๐—บ๐—ฒ๐—ป๐˜๐˜€โ€: Docstrings provide a way to document what a function or module does, as well as its input and output. Single-line comments can also help explain complex expressions.

๐Ÿญ๐Ÿฌ. ๐——๐—ฒ๐—ณ๐—ถ๐—ป๐—ฒ ๐˜๐—ต๐—ฒ โ€œ๐—ฟ๐—ฒ๐—ฝ๐—ฟ โ€œ๐—บ๐—ฒ๐˜๐—ต๐—ผ๐—ฑ ๐—ณ๐—ผ๐—ฟ ๐—ผ๐—ฏ๐—ท๐—ฒ๐—ฐ๐˜๐˜€: Implementing repr allows you to define a string representation for objects. This can be helpful for debugging and logging.

๐Ÿญ๐Ÿญ. ๐—จ๐˜€๐—ฒ ๐—น๐—ผ๐—ด๐—ด๐—ถ๐—ป๐—ด ๐—ถ๐—ป๐˜€๐˜๐—ฒ๐—ฎ๐—ฑ ๐—ผ๐—ณ ๐—ฝ๐—ฟ๐—ถ๐—ป๐˜: Logging provides a more consistent way of displaying messages, and allows you to filter messages by setting the correct log level.