int, float, complex
bool
str
list, tuple, dict, set
n = 14
b = bin(n) # str '0b1110'
b_trim = b[2:] # '1110'
b_trim.zfill(32) # prepending 0 to width 32, + and - count for one length as well
res = int(b_trim, 2) # convert to int. '0b' is optional
Sorting with lambda
sorted_by_second = sorted(<collection>, key=lambda el: el[1])
sorted_by_both = sorted(<collection>, key=lambda el: (el[1], el[0]))
Sorted Object
from functools import total_ordering
@total_ordering
class MySortable:
def __init__(self, a):
self.a = a
def __eq__(self, other):
if isinstance(other, type(self)):
return self.a == other.a
return NotImplemented
def __lt__(self, other):
if isinstance(other, type(self)):
return self.a < other.a
return NotImplemented
queue = deque()
queue.append(a)
queue.appendleft(a)
queue.pop()
queue.popleft()
raise Exception('something wrong')
try:
<code>
except Exception as e:
print(e.args[0)