Chapter 1c: Python as a Computer Language
1. Built-in Collections (Python)
# List
array = [ None ] * n
# alternative to array map
array = [ x*2 for x in range(i, j) ]
# 2D Array, dont use [[0]*cols] * rows as
# it will use the same object for all subarrays
_2d_array = [ [0]*cols for x in range(rows) ]
flattened = [ ele for subarr in _2d_array for ele in subarr]
# Use List as a stack
array.append(x)
curr = array.pop()
# Use List as a queue
queue = deque(array)
queue.append(x)
curr = queue.popleft()
# Use List as heap
heap = heapq.heapify(array)
heapq.heappush(heap, x)
curr = heapq.heappop(heap)2. Mutate String
3. Iterators and Iterable
4. Decimal Precision Limitation
5. Inline Function
6. Instance variable
Last updated