Python Flashcards
Create an array of 0s
temp = [0]*(r-l+1)
sort an array
arr.sort()
iterate over keys in dict
for key in dict: print(key,dict[key])
defaultdict
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
Declare a set
By default h = {} is a map so for set, we do h= set()
reverse list
list.reverse() -> in place
new_list = list[::-1] -> extra space
comparator
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))
iterate digits of a number if need to iterate msb to lsb
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])
iterate digits of a number if order doesnโt matter
n2 = n while n2 > 0: d = n2 % 10 n2 /= 10 foo(d)
other ways to iterate digits of a number using typecasting
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)
stack using deque
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.
min heap (default)
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))
Heap operations
import heapq
- heapify(iterable)
- heappush(heap, ele)
- heappop(heap)
- heappushpop(heap, ele)
- 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().
- nlargest(k, iterable, key = fun)
- nsmallest(k, iterable, key = fun)
Integer division of negative numbers
Because // is floor division, so rounding down, not towards zero.-22 / 10 is -2.2, rounding down makes that -3
deque operations
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.
Want to Stand Out in Python Job Interviews?
๐ญ. ๐จ๐๐ฒ โ๐๐๐ฝ๐ฒ ๐ต๐ถ๐ป๐๐โ ๐๐ผ ๐บ๐ฎ๐ธ๐ฒ ๐ฐ๐ผ๐ฑ๐ฒ ๐บ๐ผ๐ฟ๐ฒ ๐ฟ๐ฒ๐ฎ๐ฑ๐ฎ๐ฏ๐น๐ฒ: 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.