Python 3 Quick Reference
========================

DATA TYPES
  x = 42                  int
  x = 3.14                float
  s = "hello"             str
  b = b"\x41\x42"         bytes
  L = [1, 2, 3]           list
  T = (1, 2, 3)           tuple
  D = {"a": 1}            dict
  S = {1, 2, 3}           set

STRINGS
  s.upper() / s.lower()   Case conversion
  s.strip()               Remove whitespace
  s.split(",")            Split to list
  ",".join(L)             Join list to string
  s.replace("a", "b")     Replace
  s.startswith("he")      Check prefix
  s.encode()              str → bytes
  b.decode()              bytes → str
  f"Value: {x}"           F-string formatting

BYTES & ENCODING
  bytes.fromhex("4142")   Hex string → bytes
  b.hex()                 Bytes → hex string
  import base64
  base64.b64encode(b)     Base64 encode
  base64.b64decode(s)     Base64 decode

LIST OPERATIONS
  L.append(x)             Add to end
  L.extend([4,5])         Extend list
  L.pop()                 Remove last
  L[1:3]                  Slice
  L[::-1]                 Reverse
  sorted(L)               Sort (new list)
  [x*2 for x in L]        List comprehension
  len(L)                  Length

DICT OPERATIONS
  D["key"]                Get value
  D.get("key", default)   Get with default
  D.keys()                All keys
  D.values()              All values
  D.items()               Key-value pairs
  {**D1, **D2}            Merge dicts

FILE I/O
  with open("f.txt") as f:
      content = f.read()

  with open("f.txt", "w") as f:
      f.write("data")

  with open("f.bin", "rb") as f:
      data = f.read()

USEFUL MODULES
  import os                OS operations
  import sys               System-specific
  import re                Regular expressions
  import json              JSON parsing
  import hashlib           Hash functions
  import struct            Binary packing
  import socket            Network sockets
  import subprocess        Run commands
  import itertools         Iteration tools
  import collections       Specialized containers

COMMON PATTERNS
  # Read binary file
  data = open("file", "rb").read()

  # Decode an attached-data file (exam practicals save the blob to
  # challenges/q<N>.txt inside the sandbox — base64, hex, or plain text)
  import base64, re
  blob = open("challenges/q36.txt").read().strip()  # replace N
  raw = base64.b64decode(blob)           # if base64
  # raw = bytes.fromhex(blob)            # if hex
  m = re.search(rb"ICOA\{[^}]+\}", raw)
  print(m.group().decode() if m else "no flag in raw bytes")

  # Hex dump
  print(data.hex())

  # XOR bytes
  result = bytes(a ^ b for a, b in zip(d1, d2))

  # HTTP request
  import requests
  r = requests.get(url)
  r = requests.post(url, data={"key": "val"})

  # Run command
  import subprocess
  out = subprocess.check_output(["cmd", "arg"])

  # Regex
  import re
  m = re.search(r"pattern", text)
  matches = re.findall(r"pattern", text)
