



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




if x > 0: elif x == 0: else:
result = "yes" if cond else "no"
match value: case 1: ... case _: ...
python
for item in items: if item == target: break # exit loop entirely if item < 0: continue # skip to next iteration else:
print("not found")
while not done: done = check()
for i, val in enumerate(items, start=1): print(i, val)
for a, b in zip(list1, list2): print(a, b)
squares = [x**2 for x in range(10) if x % 2 == 0] lookup = {k: v for k, v in pairs} unique = {x for x in items}
3. Data Structure Selection Logic
list
tuple
set
risky() except (ValueError, KeyError) as e: # catch specific, not bare log(e) except Exception as e: # last resort raise RuntimeError("context") from e else:
celebrate() finally: cleanup() # always runs
6. Common Algorithm Patterns
python
total = 0 for x in nums: total += x
result = sum(x**2 for x in nums if x > 0)
l, r = 0, len(arr)- while l < r: if arr[l] + arr[r] == target: break elif arr[l] + arr[r] < target: l += 1 else: r -= 1
from collections import Counter freq = Counter(items).most_common(3)
def process(x): if x is None: return None # exit early if x < 0: raise ValueError
return x * 2 # main path unindented
7. Complexity Quick Reference