Python Visualized

Exploring code mechanics using the Mermaid.js DSL (Domain Specific Language).

01 Variables as References

Python Source
# Variables point to objects
user = "Alice"
age = 30
# Multiple names, one object
admin = user
Mermaid DSL (Graph Logic)
graph LR
  user(Name: user) -->|Refers to| str["String: 'Alice'"]
  age(Name: age) -->|Refers to| int((Int: 30))
  admin(Name: admin) -.->|Alias| str
  
  style str fill:#1e293b,stroke:#4ade80,stroke-width:2px
  style int fill:#1e293b,stroke:#fbbf24,stroke-width:2px
graph LR user(Name: user) -->|Refers to| str["String: 'Alice'"] age(Name: age) -->|Refers to| int((Int: 30)) admin(Name: admin) -.->|Alias| str style user fill:#0f172a,stroke:#38bdf8,color:#fff style age fill:#0f172a,stroke:#38bdf8,color:#fff style admin fill:#0f172a,stroke:#c084fc,color:#fff style str fill:#1e293b,stroke:#4ade80,stroke-width:2px,color:#fff style int fill:#1e293b,stroke:#fbbf24,stroke-width:2px,color:#fff

02 Lists & Indices

Python Source
data = [10, 20, 30]

# Accessing Index 1
print(data[1])
Mermaid DSL (Class Logic)
classDiagram
    class List {
        index 0
        index 1
        index 2
    }
    List --|> Val1 : contains
    List --|> Val2 : contains
    List --|> Val3 : contains
classDiagram note "The List Container" class List { Index [0] Index [1] Index [2] } class Val1["10"] class Val2["20"] class Val3["30"] List --> Val1 : [0] List --> Val2 : [1] List --> Val3 : [2] style List fill:#1e293b,stroke:#c084fc,color:#fff style Val2 fill:#334155,stroke:#fbbf24,stroke-width:4px,color:#fff

03 If / Else Logic

Python Source
if temperature > 30:
    print("Hot")
else:
    print("Cold")
Mermaid DSL (Flowchart)
graph TD
    Start --> Check{Temp > 30?}
    Check -->|True| Hot[Print 'Hot']
    Check -->|False| Cold[Print 'Cold']
graph TD Start((Start)) --> Check{"Temp > 30?"} Check -->|True| Hot["🔥 Print 'Hot'"] Check -->|False| Cold["❄️ Print 'Cold'"] Hot --> End((End)) Cold --> End style Check fill:#334155,stroke:#38bdf8,stroke-width:2px,color:#fff style Hot fill:#1e293b,stroke:#f87171,color:#fff style Cold fill:#1e293b,stroke:#60a5fa,color:#fff style Start fill:#0f172a,stroke:#94a3b8,color:#fff style End fill:#0f172a,stroke:#94a3b8,color:#fff

04 Function Execution

Python Source
def add(a, b):
    return a + b

result = add(5, 3)
Mermaid DSL (Sequence)
sequenceDiagram
    Main->>Add: call(5, 3)
    activate Add
    Add-->>Main: return 8
    deactivate Add
sequenceDiagram participant Main as Main Program participant Func as add(a, b) Note left of Main: result = ... Main->>Func: Pass inputs (5, 3) activate Func Note right of Func: a=5, b=3 Note right of Func: 5 + 3 = 8 Func-->>Main: Return 8 deactivate Func Note left of Main: result is now 8