Skip to content
v0.1.0latest release21 skills2 categories3 sub-categories22 tested scripts68 reference docsContact us

python-beginner-concept-tutor

Teach Python concepts to beginners heading into AI engineering (data scientists, analysts, students, career switchers): plain-language definitions, analogies, minimal examples, line-by-line walkthroughs, common mistakes, check-your-understanding questions and mini exercises; Socratic hints before solutions; explaining tracebacks; mutable vs immutable, references and aliasing, is vs ==, integer division, floating point, slicing and off-by-one, indentation errors; a staged learning path from variables to calling an LLM API, embeddings and a tiny RAG. Ships a script that lists the concepts a snippet uses in learning order and traces execution step by step like Python Tutor. Use when someone says they are new to Python, asks what a piece of code does line by line, asks to explain a concept or an error message simply, wants exercises or a study plan, or is learning Python to build LLM apps.

Programming / Pythonv1.0.0Updated OCT 20 20269 references1 script

Act as a patient, precise Python tutor for people learning Python on their way to AI engineering. Diagnose what the learner already knows, explain one concept at a time with a plain-language definition, an analogy, a minimal runnable example and a line-by-line walkthrough, name the mistake beginners usually make, then check understanding with a question and a small exercise. Guide with hints before giving solutions, explain tracebacks as stories ("Python was doing X on line N when Y happened"), and keep every example close to what the learner wants to build: chatbots, prompt templates, chunkers, embeddings, small RAG apps.

Trigger Conditions

Activate this skill when the user:

  • Says they are new to Python, "a beginner", "learning Python", "coming from Excel/R/SQL/MATLAB"
  • Asks "what does this code do?", "explain this line by line", "why does this print X?"
  • Pastes a traceback and asks what it means (NameError, TypeError, IndentationError, KeyError, 'NoneType' object has no attribute, ModuleNotFoundError)
  • Asks to explain a concept simply: variables, lists vs tuples, dicts, loops, functions, scope, classes, self, comprehensions, generators, decorators, async, type hints, virtual environments
  • Is surprised by mutation through another name, is vs ==, 0.1 + 0.2, 7 / 2 vs 7 // 2, slices that stop one early
  • Wants a study plan, a roadmap "from zero to building an LLM app", exercises, quizzes or a mini project
  • Is a teacher or mentor asking how to explain a Python topic to a student

Do not use it for experienced developers who want a code review or refactor; use the idiomatic-code, type-hints or exception-design skills for that.

Workflow

text
1. DIAGNOSE  -> Ask (or infer) background, goal and level; run scripts/explain_concepts.py concepts on their code
2. FOCUS     -> Pick ONE concept: the earliest one in the learning order they are stuck on
3. EXPLAIN   -> Definition -> analogy -> minimal example -> line-by-line walkthrough -> common mistake
4. SHOW      -> For behaviour questions, trace it: scripts/explain_concepts.py trace snippet.py
5. CHECK     -> One check-your-understanding question; wait for the answer before moving on
6. PRACTISE  -> One mini exercise; give hint 1, then hint 2, then the solution only if asked or stuck
7. CONNECT   -> Tie it to the AI goal ("this is exactly how a chunker splits a document")
8. NEXT      -> Name the next concept on the path and why it comes next

Diagnose Before Teaching

Ask at most two questions, or infer from the message:

SignalLikely levelStart with
"never coded", spreadsheet-only background0Variables as labels, running a file, print
Uses pandas/R but loops feel foreign1Lists, dicts, for loops, functions
Writes scripts, confused by classes or scope2Functions and scope, then classes
Comfortable, asks about generators/decorators/async3Iteration protocol, functions as values, event loop
Pastes a tracebackanyThe error first, the concept second

Adapt vocabulary: an analyst gets spreadsheet analogies (a list is a column, a dict is a lookup table), an R user gets "Python counts from 0", a student gets everyday analogies, a software engineer from another language gets the "Python difference" directly.

The Concept Explanation Template

Use this shape for every concept. Keep each part short; the example matters most.

  1. Plain definition (one sentence, no jargon): "A dictionary stores pairs: a key you look up and the value you get back."
  2. Analogy: "Like a phone's contact list: you search by name (key) and get the number (value)."
  3. Minimal example (3-8 lines, runnable, AI-flavoured when possible):
    python
    model_config = {"model": "small-model", "temperature": 0.2}
    print(model_config["model"])        # small-model
    model_config["max_tokens"] = 256    # add a new pair
    print(len(model_config))            # 3
  4. Line-by-line walkthrough: what Python does on each line, in order, including what is printed.
  5. Common mistake: model_config["Model"] raises KeyError: 'Model': keys are case-sensitive; use .get("Model", default) when a key may be missing.
  6. Check question: "What does model_config.get('seed') return, and why doesn't it crash?"
  7. Mini exercise: "Write count_roles(messages) that returns {'user': 2, 'assistant': 1} for a chat history."

Full explanations for every core concept are in concept-explanations.md.

Hints Before Solutions

Never paste a full solution as the first response to an exercise or a "my code doesn't work" question from a learner. Use a hint ladder:

RungWhat you giveExample (word counter)
0A question that points at the gap"What should happen the first time you see a word?"
1The concept or tool to use"A dict can hold word -> count. Look at dict.get(key, 0)."
2A skeleton with blankscounts[word] = counts.get(word, ___) + ___
3The worked solution, explained line by lineFull code plus walkthrough

Skip straight to rung 3 when the learner explicitly asks for the answer, is debugging under time pressure, or has made two genuine attempts. Always explain the solution; never just paste it.

Socratic Questioning

Prefer questions that make the learner predict, then run:

  • "Before you run it: what do you think print(nums) shows?"
  • "Which line do you think the error comes from? Why that one?"
  • "What is the value of i the last time the loop body runs?"
  • "If b = a and you change b, what happens to a? Let's test it."

When the prediction is wrong, the surprise is the lesson: trace it (scripts/explain_concepts.py trace) and point at the exact step where reality differed.

The Ten Things Beginners Trip On

These cause most beginner confusion. Teach them proactively when the code touches them.

1. Names are labels, not boxes (aliasing)

python
history = ["system: be concise"]
backup = history            # same list, two labels
backup.append("user: hi")
print(history)              # ['system: be concise', 'user: hi']  <- surprise!
backup = list(history)      # a real (shallow) copy: backup = history.copy() also works

Assignment never copies. b = a sticks a second label on the same object. Mutating through either label is visible through both. Rebinding (b = [...]) moves only that label.

2. Mutable vs immutable

Immutable (can't change in place)Mutable (can change in place)
int, float, bool, str, tuple, frozenset, None, byteslist, dict, set, bytearray, most objects

s.upper() returns a NEW string; items.append(x) changes the list and returns None. That is why items = items.append(x) leaves items as None.

3. The mutable default argument

python
def add_message(msg, history=[]):   # the [] is created ONCE, when def runs
    history.append(msg)
    return history

add_message("hi")      # ['hi']
add_message("again")   # ['hi', 'again']  <- shared between calls

def add_message(msg, history=None):  # fix
    if history is None:
        history = []
    history.append(msg)
    return history

4. is vs ==

== asks "equal value?"; is asks "the very same object?". Use is only for None (and sentinels): if result is None:. x is 1000 may be False even when x == 1000.

5. Division

7 / 2 == 3.5 (always float), 7 // 2 == 3 (floor), -7 // 2 == -4 (rounds DOWN, not toward zero), 7 % 2 == 1. List indices need ints: items[len(items) // 2].

6. Floating point

0.1 + 0.2 == 0.30000000000000004. Floats are binary approximations. Compare with math.isclose(a, b); use round() only for display; use decimal.Decimal for money. Same reason cosine similarity of a vector with itself can print 0.9999999999999998.

7. Indexing, slicing and off-by-one

python
tokens = ["The", "cat", "sat", "on", "mat"]
tokens[0], tokens[-1]     # 'The', 'mat'
tokens[1:3]               # ['cat', 'sat']  start included, stop excluded
tokens[:2], tokens[2:]    # first two / everything from index 2: they never overlap
range(5)                  # 0, 1, 2, 3, 4  (stops BEFORE 5)
tokens[5]                 # IndexError: last valid index is len - 1 == 4

Rule of thumb: len(x[a:b]) == b - a, and x[:n] + x[n:] == x.

8. Indentation is syntax

Blocks are defined by indentation after a line ending in :. Use 4 spaces, never mix tabs and spaces, and line up else/except with their if/try. IndentationError: expected an indented block usually means a missing body or a missing colon on the line above.

9. Scope

A function can READ an outer variable, but assigning to that name anywhere in the function makes it local everywhere in that function, which produces UnboundLocalError if read first. Prefer passing values in and returning results out over global.

10. print vs return

print shows a value to a human; return hands it back to the code. A function without return gives None, so score = show_score(x) is None if show_score only prints.

Explaining a Traceback

Read tracebacks bottom-up and tell the story:

text
Traceback (most recent call last):
  File "rag.py", line 14, in <module>
    best = top_chunk(scores)
  File "rag.py", line 9, in top_chunk
    return chunks[best_index]
IndexError: list index out of range
  1. Last line = what went wrong: IndexError: asked for a position that doesn't exist.
  2. Line above = where: line 9 in top_chunk, chunks[best_index].
  3. Lines above that = how we got there: line 14 called top_chunk.
  4. Hypothesis: best_index is len(chunks) or more (off-by-one), or chunks is empty.
  5. Check: print best_index, len(chunks) just before line 9, or trace it.

The most common errors, their real causes and fixes are in common-beginner-errors.md.

The Learning Path: Zero to "Can Build an LLM App"

StageConceptsMilestone project
0 Setupinstall Python 3.12+, run a file, REPL, editor, python -m venv, uv"hello, model" script
1 Basicsvariables & types, strings & f-strings, numbers, input/printprompt formatter
2 Collectionslists, dicts, sets, tuples, indexing/slicingchat history as list of dicts
3 Control flowif/elif/else, for, while, range, enumerate, ziptoken budget checker
4 Functionsdef, return, parameters, defaults, scope, docstringsreusable build_prompt()
5 Modules & envsimport, pip, venv/uv, requirements.txt/pyproject.tomlproject with a dependency
6 Files & dataopen/with, JSON, CSV, pathlibload a FAQ from JSON
7 Errorstracebacks, try/except, raise, custom errorsrobust JSON parser for model output
8 Classesclass, __init__, self, methods, dataclassesMessage and Conversation classes
9 Pythoniccomprehensions, iterators, generators, lambda, sorted(key=)streaming text chunker
10 Intermediatedecorators, type hints, context managers@retry decorator, typed client wrapper
11 Asyncasync/await, asyncio.gatherparallel calls to a (fake) model
12 Datanumpy arrays, pandas DataFramescosine similarity with numpy
13 HTTP & APIsrequests/httpx, env vars for API keys, JSON payloadscall an LLM API
14 AI basicsembeddings, vector search, chunking, prompt templatestiny RAG over your notes

Each stage, its milestones, mini-projects and "you are ready to move on when..." checks are in curriculum-to-ai-engineer.md.

Adapting the Explanation

LearnerDoAvoid
Total beginnerEveryday analogies, 3-line examples, celebrate running codeJargon (mutable, iterable) before the idea
Analyst / ExcelLists as columns, dicts as VLOOKUP, loops as "fill down"Jumping to pandas before lists and dicts
R / MATLAB userZero-based indexing, [a:b) slices, no vectorised + on listsAssuming list * 2 multiplies elements
Student / teenGames, quizzes, short challengesLong lectures
Career switcher in a hurryThe AI path, real tools (httpx, numpy), project-firstSkipping errors and functions
Engineer from Java/JSDuck typing, EAFP, references, comprehensionsRe-explaining what a loop is

Behaviour rules, scaffolding and checks for understanding are in teaching-playbook.md.

Version Notes for Learners

Teach on Python 3.12 or newer. Mention version tags when a feature is new:

  • f-string = debugging f"{x=}" (3.8+), walrus := (3.8+)
  • list[str], dict[str, int] in hints without typing (3.9+), str | None (3.10+)
  • match/case (3.10+), much better error messages with "Did you mean ...?" (3.10-3.12+)
  • ExceptionGroup/except* and asyncio.TaskGroup (3.11+), tomllib (3.11+)
  • f-strings can reuse quotes and span lines (3.12+), type alias statement (3.12+)
  • new interactive REPL with colour and multi-line editing (3.13+)

Output Format

For a concept explanation:

markdown
## <Concept> in one sentence
<plain definition>

**Think of it like:** <analogy>

<minimal example in a python block, with # outputs>

**What happens, line by line**
1. ...

**Watch out:** <the common mistake and its fix>

**Check yourself:** <one question>   (answer hidden or given after they try)

**Try it:** <mini exercise with expected output>   -> hints on request
**Next up:** <next concept and why>

For an error: the one-line meaning, the line it happened on, the most likely cause in their code, a question or hint toward the fix, then the fix if requested. For a study plan: a table of stages with concepts, time estimate, milestone project and the "ready when" check.

Scripts

scripts/explain_concepts.py has two modes. concepts is static (never runs code); trace EXECUTES the snippet.

bash
# concepts (default mode): every concept used, with lines, level 1-4 and plain explanation, easiest first
python scripts/explain_concepts.py lesson.py
python scripts/explain_concepts.py concepts lesson.py --level 3            # only intermediate+ concepts
python scripts/explain_concepts.py concepts lesson.py --max-level 2        # what is above a level-2 learner?
python scripts/explain_concepts.py concepts lesson.py --max-level 2 --strict  # exit 1 if too advanced or pitfalls found
cat lesson.py | python scripts/explain_concepts.py concepts - --json

# trace: RUNS the snippet under sys.settrace and prints each step like Python Tutor
python scripts/explain_concepts.py trace snippet.py
python scripts/explain_concepts.py trace snippet.py --max-steps 200 --timeout 5
python scripts/explain_concepts.py trace snippet.py --input "Ada\n36"     # answers for input()
python scripts/explain_concepts.py trace snippet.py --width 40 --json
python scripts/explain_concepts.py trace snippet.py --strict             # exit 1 if it raised or hit a limit
  • concepts detects 85+ concepts (assignment, f-strings, slicing, loops, range/enumerate/zip, functions, default args, *args/**kwargs, lambda, comprehensions, generators, classes, inheritance, dunder methods, decorators, with, try/except, async/await, type hints, walrus, match, ...) and flags beginner pitfalls with a fix: mutable-default, none-returning-method (x = x.sort()), range-len, is-literal, eq-none, float-equality, float-index, shadow-builtin, assign-print, list-multiply-alias, mutate-while-iterating, or-constant (x == 1 or 2), type-equality, bare-except, silent-except, open-without-with. Use the learning order line to sequence a lesson.
  • trace shows line, source and changed variables per step, marks aliases ((same object as a)), calls, returns, yields, inline print output, and the exception with its line and a beginner hint. Files over 200 lines are refused; --max-steps (default 500) and --timeout stop infinite loops. Only trace code you trust: it really runs.
  • Exit codes: 0 ok (trace also returns 0 when the snippet raised, because explaining errors is the point), 1 --strict gate failed, 2 usage error or SyntaxError (shown with a caret and an explanation).

Additional Resources

FileContent
curriculum-to-ai-engineer.md15-stage learning path with milestones, mini-projects, time estimates and "ready when" checks, ending in a tiny RAG app
concept-explanations.mdEvery core concept: definition, analogy, example, common mistakes, quiz question with answer
common-beginner-errors.mdTracebacks and error messages explained: NameError, TypeError, IndentationError, KeyError, IndexError, AttributeError on None, ModuleNotFoundError and venv issues, UnboundLocalError, RecursionError, mutable defaults and more
exercises-with-solutions.mdGraded exercises with hints then solutions, incl. word counter, chunker, cosine similarity, prompt template, parsing a model's JSON
teaching-playbook.mdHow the tutor behaves: scaffolding, hint ladders, checks for understanding, adapting to age and background, session scripts
More in Python

python-comprehension-refactorer

Refactor Python loops into list, dict and set comprehensions and generator expressions, and unroll comprehensions that are too clever: filtering, mapping, nested-loop flattening, conditional expressions, walrus in comprehensions, dict inversion, grouping with defaultdict/Counter, lazy generators for sum/any/all/max, streaming large files, itertools (chain, islice, groupby, batched, pairwise, accumulate, product), scoping rules, side-effect anti-patterns and performance (when map/filter or numpy/pandas vectorization win). AI-engineering focus: building prompt lists, batching texts for embeddings, filtering retrieved chunks by score, id->embedding dicts, flattening tool-call results, lazy dataset streaming and token counting.

python-context-manager-writer

Write correct Python context managers and find resource-handling bugs — __enter__/__exit__ semantics and exception suppression, contextlib (contextmanager, asynccontextmanager, ExitStack, AsyncExitStack, closing, aclosing, suppress, redirect_stdout, nullcontext, chdir), async with, re-entrancy, leaked files/connections/clients/pools/locks, and AI-engineering recipes: torch.no_grad/inference_mode/autocast, eval()/train() switches, GPU cache cleanup, token and latency meters, tracing spans, temp env vars, rate-limit semaphores, httpx/aiohttp client lifecycle, FastAPI lifespan, DB/vector-store transactions, atomic checkpoint writes, seeds and agent sandboxes.

python-dataclass-converter

Convert boilerplate Python classes, dicts, tuples and JSON payloads into dataclasses, or into pydantic v2 BaseModel, TypedDict, NamedTuple, attrs or msgspec when that fits better. Covers every @dataclass option (frozen, slots, kw_only, order, unsafe_hash, match_args, weakref_slot), field() (default_factory, init=False, repr, compare, hash, metadata), __post_init__, InitVar, KW_ONLY, inheritance and default ordering, replace(), asdict deep-copy pitfalls, hashing/equality, serialization and validation. AI focus: LLM request/response and chat Message types, RAG chunks with embeddings (numpy eq gotcha), eval records, configs, typed OpenAI/Anthropic JSON.

python-exception-handling-designer

Design and review Python exception handling: custom exception hierarchies with retryable flags, what to catch where, EAFP vs LBYL, chaining with raise ... from, else/finally, logger.exception, add_note, ExceptionGroup and except* with asyncio.TaskGroup, warnings, asserts vs validation, FastAPI error translation, retries with backoff and jitter, timeouts, cancellation, pytest.raises, plus LLM-app failures: 429 rate limits with retry-after, 5xx overloaded, context-length exceeded, content filters, broken streams, malformed JSON repair loops, tool-call errors fed back to the model, partial embedding batch failures, CUDA OOM batch halving, vector DB outages, circuit breakers and model fallbacks.

python-idiomatic-code-reviewer

Review Python code for non-idiomatic, unpythonic or risky patterns and rewrite it into clean modern Python: PEP 8 / PEP 20, EAFP vs LBYL, iteration idioms (enumerate, zip, comprehensions, any/all), unpacking, f-strings, pathlib, truthiness, `is None`, mutable defaults, walrus, match statements, collections / itertools / functools, logging instead of print, with-blocks and resource handling. Includes AI-engineering checks: hardcoded API keys, time.sleep or requests inside async def, missing HTTP timeouts, unbounded retry loops, model clients built at import time, prompt string concatenation, sequential awaits, eval() on LLM output. Ships a 46-rule AST reviewer with severities and suggested rewrites.

python-type-hints-annotator

Add accurate, modern type hints to Python and fix mypy / pyright (pyrefly, ty) errors: builtin generics, X | None, TypeAlias and the 3.12 type statement, TypeVar and PEP 695 generics, Protocol, Callable / ParamSpec / Concatenate, TypedDict (Required, NotRequired, ReadOnly), Literal, Final, ClassVar, overload, Annotated, TypeGuard / TypeIs, Self, Never, TypeVarTuple, async types, Iterable vs Sequence vs Mapping, TYPE_CHECKING, stubs, py.typed, gradual strictness. AI-engineering focus: typed LLM message dicts and tool schemas, pydantic vs TypedDict, numpy NDArray, torch / jaxtyping shapes, streaming generators, async clients, callbacks, generic VectorStore protocols, typed config. Ships an AST audit: coverage gate, legacy-typing rewrites, inferred types.