Python Python 3.12 (Pyodide)
Python 3 Quick Reference
Essential Python 3 syntax, f-strings, lists, dictionaries, comprehensions, and built-in functions.
def fn(x=1): return [n for n in items if n > 0]
Core Syntax & Types
| Syntax | Purpose | Example |
|---|---|---|
f"{var}" | String formatting (f-string) | f"Score: {pct}%" |
if / elif / else | Conditional branching | status = "Pass" if s >= 80 else "Fail" |
for x in items: | Iterate over any sequence | for i, x in enumerate(items, 1): |
def fn(a, b=0): | Define reusable function | def add(a, b=0): return a + b |
Collections & Built-Ins
| API | Purpose | Example |
|---|---|---|
items[-1] / items[:3] | Last item / first 3 items | [10, 20, 30][-1] -> 30 |
d.get(key, default) | Safe dictionary lookup | course.get("level", "Beginner") |
[f(x) for x in xs if p(x)] | List comprehension | [n * 2 for n in nums if n > 0] |
set(items) | Unique values | list(set(["a", "b", "a"])) |
sum() / len() / sorted() | Common aggregations | round(sum(xs) / len(xs), 1) |