Skip to content

Commit 4288fe4

Browse files
travisjneumanclaude
andcommitted
feat: add Mermaid diagram files for 8 beginner Python concepts
Create concepts/diagrams/ with visual explanations for variables, loops, types, functions, collections, files, errors, and error messages. Each file contains 2-4 Mermaid diagrams targeting absolute beginners with zero programming experience, and links back to its parent concept doc. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent ac49cce commit 4288fe4

File tree

8 files changed

+685
-0
lines changed

8 files changed

+685
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Diagrams: Collections Explained
2+
3+
[Back to concept](../collections-explained.md)
4+
5+
---
6+
7+
## Side-by-Side Comparison
8+
9+
Each collection type has different properties. Pick the one that fits your needs.
10+
11+
```mermaid
12+
flowchart TD
13+
subgraph LIST ["list [1, 2, 3]"]
14+
L1["Mutable: YES"]
15+
L2["Ordered: YES"]
16+
L3["Indexed: YES - items[0]"]
17+
L4["Duplicates: YES"]
18+
end
19+
20+
subgraph TUPLE ["tuple (1, 2, 3)"]
21+
T1["Mutable: NO"]
22+
T2["Ordered: YES"]
23+
T3["Indexed: YES - items[0]"]
24+
T4["Duplicates: YES"]
25+
end
26+
27+
subgraph DICT ["dict {'a': 1, 'b': 2}"]
28+
D1["Mutable: YES"]
29+
D2["Ordered: YES (3.7+)"]
30+
D3["Indexed: BY KEY - items['a']"]
31+
D4["Duplicate keys: NO"]
32+
end
33+
34+
subgraph SET ["set {1, 2, 3}"]
35+
S1["Mutable: YES"]
36+
S2["Ordered: NO"]
37+
S3["Indexed: NO"]
38+
S4["Duplicates: NO"]
39+
end
40+
41+
style LIST fill:#4a9eff,stroke:#2670c2,color:#fff
42+
style TUPLE fill:#20c997,stroke:#0ca678,color:#fff
43+
style DICT fill:#ff922b,stroke:#e8590c,color:#fff
44+
style SET fill:#cc5de8,stroke:#9c36b5,color:#fff
45+
```
46+
47+
## Decision Tree: Which Collection Should I Use?
48+
49+
```mermaid
50+
flowchart TD
51+
START{"What do you<br/>need to store?"}
52+
START -->|"Key-value pairs<br/>(name -> value)"| DICT_Q{"Will you<br/>change it?"}
53+
DICT_Q -->|Yes| DICT["Use a DICT<br/>{&quot;name&quot;: &quot;Alice&quot;}"]
54+
DICT_Q -->|No| DICT
55+
56+
START -->|"A group of<br/>unique items"| SET["Use a SET<br/>{&quot;a&quot;, &quot;b&quot;, &quot;c&quot;}"]
57+
58+
START -->|"An ordered<br/>sequence of items"| SEQ_Q{"Will you need<br/>to change it later?"}
59+
SEQ_Q -->|"Yes — add, remove,<br/>or change items"| LIST["Use a LIST<br/>[1, 2, 3]"]
60+
SEQ_Q -->|"No — it should<br/>never change"| TUPLE["Use a TUPLE<br/>(1, 2, 3)"]
61+
62+
style LIST fill:#4a9eff,stroke:#2670c2,color:#fff
63+
style TUPLE fill:#20c997,stroke:#0ca678,color:#fff
64+
style DICT fill:#ff922b,stroke:#e8590c,color:#fff
65+
style SET fill:#cc5de8,stroke:#9c36b5,color:#fff
66+
```
67+
68+
## How You Access Data in Each Collection
69+
70+
```mermaid
71+
flowchart LR
72+
subgraph "List / Tuple: By Position"
73+
LA["items[0]"] --> LV["first item"]
74+
LB["items[2]"] --> LW["third item"]
75+
LC["items[-1]"] --> LX["last item"]
76+
end
77+
78+
subgraph "Dict: By Key Name"
79+
DA["person[&quot;name&quot;]"] --> DV["&quot;Alice&quot;"]
80+
DB["person[&quot;age&quot;]"] --> DW["25"]
81+
end
82+
83+
subgraph "Set: No Direct Access"
84+
SA["Loop through it"] --> SV["for item in my_set:"]
85+
SB["Check membership"] --> SW["&quot;a&quot; in my_set"]
86+
end
87+
```
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Diagrams: Errors and Debugging
2+
3+
[Back to concept](../errors-and-debugging.md)
4+
5+
---
6+
7+
## Exception Hierarchy
8+
9+
Python errors form a family tree. Catching a parent catches all its children too.
10+
11+
```mermaid
12+
flowchart TD
13+
BASE["BaseException<br/>(do not catch this)"]
14+
BASE --> KBI["KeyboardInterrupt<br/>Ctrl+C pressed"]
15+
BASE --> SYS["SystemExit<br/>Program told to quit"]
16+
BASE --> EXC["Exception<br/>(catch this one)"]
17+
18+
EXC --> VAL["ValueError<br/>Right type,<br/>wrong value"]
19+
EXC --> TYPE["TypeError<br/>Wrong type<br/>entirely"]
20+
EXC --> NAME["NameError<br/>Name not<br/>found"]
21+
EXC --> IDX["IndexError<br/>List index<br/>out of range"]
22+
EXC --> KEY["KeyError<br/>Dict key<br/>not found"]
23+
EXC --> ATTR["AttributeError<br/>Object has no<br/>such attribute"]
24+
EXC --> FILE["FileNotFoundError<br/>File does<br/>not exist"]
25+
EXC --> ZERO["ZeroDivisionError<br/>Divided<br/>by zero"]
26+
EXC --> IMPORT["ImportError<br/>Module not<br/>found"]
27+
EXC --> SYNTAX["SyntaxError<br/>Code is not<br/>valid Python"]
28+
29+
style BASE fill:#888,stroke:#666,color:#fff
30+
style EXC fill:#4a9eff,stroke:#2670c2,color:#fff
31+
style SYNTAX fill:#ff6b6b,stroke:#c0392b,color:#fff
32+
```
33+
34+
## Try / Except / Else / Finally Flow
35+
36+
```mermaid
37+
flowchart TD
38+
START(["Start"]) --> TRY["TRY block<br/>Run the risky code"]
39+
TRY --> ERR{"Did an<br/>error happen?"}
40+
41+
ERR -->|"Yes"| EXCEPT["EXCEPT block<br/>Handle the error"]
42+
ERR -->|"No"| ELSE["ELSE block<br/>Runs only if<br/>NO error happened"]
43+
44+
EXCEPT --> FINALLY["FINALLY block<br/>ALWAYS runs<br/>(cleanup goes here)"]
45+
ELSE --> FINALLY
46+
47+
FINALLY --> END(["Continue program"])
48+
49+
style TRY fill:#4a9eff,stroke:#2670c2,color:#fff
50+
style EXCEPT fill:#ff6b6b,stroke:#c0392b,color:#fff
51+
style ELSE fill:#51cf66,stroke:#27ae60,color:#fff
52+
style FINALLY fill:#ffd43b,stroke:#f59f00,color:#000
53+
```
54+
55+
## Debugging Decision Tree: What Kind of Error?
56+
57+
```mermaid
58+
flowchart TD
59+
START{"Your program<br/>has a problem"}
60+
61+
START -->|"Red error<br/>message appears"| RUNTIME["RUNTIME ERROR<br/>Program crashed"]
62+
START -->|"No error, but<br/>wrong output"| LOGIC["LOGIC ERROR<br/>Code runs but<br/>does the wrong thing"]
63+
START -->|"Error before<br/>code even runs"| SYNTAX["SYNTAX ERROR<br/>Python cannot<br/>read your code"]
64+
65+
SYNTAX --> S1["Check for:<br/>missing colons :<br/>unmatched ( ) [ ]<br/>bad indentation"]
66+
67+
RUNTIME --> R1["Read the error<br/>message carefully"]
68+
R1 --> R2["Find the line<br/>number it points to"]
69+
R2 --> R3["Check: what type<br/>of error is it?"]
70+
71+
LOGIC --> L1["Add print() statements<br/>to see what values<br/>your variables have"]
72+
L1 --> L2["Compare expected<br/>vs actual output"]
73+
L2 --> L3["Find where they<br/>first differ"]
74+
75+
style RUNTIME fill:#ff6b6b,stroke:#c0392b,color:#fff
76+
style LOGIC fill:#ffd43b,stroke:#f59f00,color:#000
77+
style SYNTAX fill:#cc5de8,stroke:#9c36b5,color:#fff
78+
```
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Diagrams: Files and Paths
2+
3+
[Back to concept](../files-and-paths.md)
4+
5+
---
6+
7+
## File I/O Flowchart
8+
9+
The safe way to work with files uses a `with` statement, which closes the file automatically.
10+
11+
```mermaid
12+
flowchart TD
13+
subgraph "WITHOUT with (risky)"
14+
A1["f = open(&quot;data.txt&quot;)"] --> A2["data = f.read()"]
15+
A2 --> A3{"Error<br/>happens?"}
16+
A3 -->|No| A4["f.close()"]
17+
A3 -->|Yes| A5["FILE LEFT OPEN!<br/>Resource leak"]
18+
style A5 fill:#ff6b6b,stroke:#c0392b,color:#fff
19+
end
20+
21+
subgraph "WITH with (safe)"
22+
B1["with open(&quot;data.txt&quot;) as f:"] --> B2["data = f.read()"]
23+
B2 --> B3{"Error<br/>happens?"}
24+
B3 -->|No| B4["File closed<br/>automatically"]
25+
B3 -->|Yes| B5["File STILL closed<br/>automatically"]
26+
style B4 fill:#51cf66,stroke:#27ae60,color:#fff
27+
style B5 fill:#51cf66,stroke:#27ae60,color:#fff
28+
end
29+
```
30+
31+
## Path Types: Absolute vs Relative
32+
33+
```mermaid
34+
flowchart TD
35+
subgraph "Absolute Path (full address)"
36+
ABS["/home/user/projects/data.txt"]
37+
ABS_W["C:\Users\user\projects\data.txt"]
38+
ABS_NOTE["Always starts from the root<br/>Works from anywhere"]
39+
end
40+
41+
subgraph "Relative Path (directions from here)"
42+
REL1["data.txt<br/>(same folder)"]
43+
REL2["../data.txt<br/>(one folder up)"]
44+
REL3["sub/data.txt<br/>(inside subfolder)"]
45+
REL_NOTE["Depends on where<br/>you are right now"]
46+
end
47+
48+
CWD["Your current<br/>working directory:<br/>/home/user/projects/"]
49+
CWD -->|"+ data.txt"| RESULT1["/home/user/projects/data.txt"]
50+
CWD -->|"+ ../data.txt"| RESULT2["/home/user/data.txt"]
51+
CWD -->|"+ sub/data.txt"| RESULT3["/home/user/projects/sub/data.txt"]
52+
53+
style ABS_NOTE fill:#4a9eff,stroke:#2670c2,color:#fff
54+
style REL_NOTE fill:#ffd43b,stroke:#f59f00,color:#000
55+
```
56+
57+
## File Mode Decision Tree
58+
59+
The `mode` argument in `open()` controls what you can do with the file.
60+
61+
```mermaid
62+
flowchart TD
63+
START{"What do you<br/>want to do?"}
64+
65+
START -->|"Read existing<br/>content"| R["mode=&quot;r&quot;<br/>(default)<br/>Read only"]
66+
START -->|"Write new content<br/>(erase old)"| W["mode=&quot;w&quot;<br/>Write — erases<br/>existing content!"]
67+
START -->|"Add to the end<br/>(keep old)"| A_MODE["mode=&quot;a&quot;<br/>Append — adds<br/>to the end"]
68+
START -->|"Create new file<br/>(fail if exists)"| X["mode=&quot;x&quot;<br/>Exclusive create"]
69+
70+
R --> TEXT_Q{"Text or<br/>binary data?"}
71+
W --> TEXT_Q
72+
A_MODE --> TEXT_Q
73+
X --> TEXT_Q
74+
75+
TEXT_Q -->|"Text<br/>(words, CSV, JSON)"| TEXT["No extra flag<br/>open(&quot;f.txt&quot;, &quot;r&quot;)"]
76+
TEXT_Q -->|"Binary<br/>(images, PDFs)"| BIN["Add b<br/>open(&quot;f.png&quot;, &quot;rb&quot;)"]
77+
78+
style W fill:#ff6b6b,stroke:#c0392b,color:#fff
79+
style A_MODE fill:#51cf66,stroke:#27ae60,color:#fff
80+
style R fill:#4a9eff,stroke:#2670c2,color:#fff
81+
style X fill:#ffd43b,stroke:#f59f00,color:#000
82+
```
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# Diagrams: Functions Explained
2+
3+
[Back to concept](../functions-explained.md)
4+
5+
---
6+
7+
## Function Call Sequence
8+
9+
When you call a function, Python jumps into it, does work, then comes back with a result.
10+
11+
```mermaid
12+
sequenceDiagram
13+
participant Caller as Your Code
14+
participant Func as Function
15+
16+
Caller->>Func: Call: greet("Alice")
17+
Note over Func: name = "Alice"
18+
Note over Func: Run the function body
19+
Note over Func: Build result: "Hello, Alice!"
20+
Func-->>Caller: Return: "Hello, Alice!"
21+
Note over Caller: Continue with the result
22+
```
23+
24+
## Parameter Passing Flow
25+
26+
Arguments are values you send IN. The return value is what comes back OUT.
27+
28+
```mermaid
29+
flowchart LR
30+
subgraph Caller
31+
A1["area = calculate(5, 3)"]
32+
end
33+
34+
subgraph "Function: calculate(width, height)"
35+
B1["width = 5"]
36+
B2["height = 3"]
37+
B3["result = 5 * 3"]
38+
B4["return 15"]
39+
B1 --> B2 --> B3 --> B4
40+
end
41+
42+
A1 -->|"arguments go IN<br/>5 and 3"| B1
43+
B4 -->|"return value<br/>comes OUT: 15"| A1
44+
45+
style A1 fill:#4a9eff,stroke:#2670c2,color:#fff
46+
style B4 fill:#51cf66,stroke:#27ae60,color:#fff
47+
```
48+
49+
## Call Stack: Nested Function Calls
50+
51+
When functions call other functions, Python stacks them up and unwinds when each one finishes.
52+
53+
```mermaid
54+
flowchart TD
55+
subgraph "Step 1: main() runs"
56+
S1["main() calls greet()"]
57+
end
58+
59+
subgraph "Step 2: greet() runs"
60+
S2["greet() calls format_name()"]
61+
end
62+
63+
subgraph "Step 3: format_name() runs"
64+
S3["format_name() returns &quot;ALICE&quot;"]
65+
end
66+
67+
subgraph "Step 4: Unwinding"
68+
S4["greet() gets &quot;ALICE&quot;, returns &quot;Hello, ALICE!&quot;"]
69+
end
70+
71+
subgraph "Step 5: Done"
72+
S5["main() gets &quot;Hello, ALICE!&quot;"]
73+
end
74+
75+
S1 --> S2 --> S3 --> S4 --> S5
76+
```
77+
78+
## Scope Chain: Where Python Looks for Names
79+
80+
When you use a variable name, Python searches in this order (LEGB rule).
81+
82+
```mermaid
83+
flowchart TD
84+
LOOKUP["Python sees a name<br/>like x"] --> L{"1. Local?<br/>Inside this function?"}
85+
L -->|Found| DONE(["Use that value"])
86+
L -->|Not found| E{"2. Enclosing?<br/>Inside an outer function?"}
87+
E -->|Found| DONE
88+
E -->|Not found| G{"3. Global?<br/>At the top level<br/>of the file?"}
89+
G -->|Found| DONE
90+
G -->|Not found| B{"4. Built-in?<br/>Python built-in names?<br/>(print, len, range)"}
91+
B -->|Found| DONE
92+
B -->|Not found| ERR["NameError!<br/>Python cannot find it"]
93+
94+
style L fill:#4a9eff,stroke:#2670c2,color:#fff
95+
style E fill:#51cf66,stroke:#27ae60,color:#fff
96+
style G fill:#ffd43b,stroke:#f59f00,color:#000
97+
style B fill:#cc5de8,stroke:#9c36b5,color:#fff
98+
style ERR fill:#ff6b6b,stroke:#c0392b,color:#fff
99+
```

0 commit comments

Comments
 (0)