← Home

Ch 1 — Variables & Naming

Ch 1 — Variables & Naming

Aryan wants to build a tool that watches his computer’s RAM usage in real time. Before he can read a single byte of memory data, he needs to understand how Python stores and names information. Variables are the foundation — every piece of RAM data the tool tracks will live in one.


Variables

A variable is a named container for a value. In Python you do not declare a type — you just assign and go.

total_ram = 16  # GB
used_ram = 6.4
label = "RAM Manager v0.1"

Python binds the name total_ram to the integer 16. Change the assignment and the binding updates instantly.

total_ram = 32   # upgraded RAM — same name, new value

Comments

Comments explain why, not just what. They start with # and are ignored by the interpreter.

# Total physical RAM installed on this machine (GB)
total_ram = 16

used_ram = 6.4  # updated every second by psutil

Good comments make a RAM manager readable six months later, even at 2 AM during an incident.


Indentation

Python uses indentation (4 spaces per level) instead of braces to define blocks. Mixing tabs and spaces causes IndentationError.

if used_ram > 12:
    print("Warning: RAM usage critical")   # indented block
    print("Consider killing background apps")

Keywords

Keywords are reserved words that Python owns. You cannot use them as variable names.

# BAD — 'if' is a keyword
# if = 10   → SyntaxError

# GOOD
threshold = 10

Common keywords: if, else, for, while, def, class, import, return, True, False, None, and, or, not, in, is.


Identifiers

An identifier is any name you invent — for variables, functions, or classes.

Rules:

  • Start with a letter or underscore _
  • Contain letters, digits, underscores — no spaces or hyphens
  • Case-sensitive: Ram and ram are different names
_internal_counter = 0   # valid — leading underscore
ram2  = 8               # valid — digit after first char
# 2ram = 8              # INVALID — cannot start with digit

Dynamic Typing

Python figures out the type at runtime. The same name can point to different types across assignments.

status = "ok"         # str
status = 200          # now int — Python is fine with this
status = True         # now bool

For the RAM manager this means you can prototype fast, but you should still name variables clearly so the type is obvious from context.


Naming Conventions

StyleExampleUsed for
snake_caseused_ram_mbvariables, functions
SCREAMING_SNAKEMAX_RAM_GBconstants
PascalCaseRamSnapshotclasses
_single_leading_raw_bytesinternal / private
MAX_RAM_GB = 16          # constant — won't change at runtime
used_ram_mb = 6553.6     # variable — updated every second
process_name = "chrome"  # descriptive snake_case

Concept Map

flowchart LR
    A[Python Program] --> B[Variables]
    B --> C[Name]
    B --> D[Value]
    D --> E[Integer]
    D --> F[Float]
    D --> G[String]
    D --> H[Boolean]
    C --> I[Naming Rules]
    I --> J[snake_case]
    I --> K[No keywords]
    I --> L[Letters / digits / _]

Variable Lifecycle in the RAM Manager

flowchart TD
    A[Program starts] --> B[total_ram = 16]
    B --> C[used_ram = psutil.read]
    C --> D{used_ram > threshold?}
    D -->|Yes| E[Trigger alert]
    D -->|No| F[Log to file]
    E --> C
    F --> C

Key Takeaways

  • Variables bind a name to a value — no type declaration needed.
  • Use # for comments; they are ignored at runtime but vital for humans.
  • Python enforces indentation as syntax — 4 spaces per level.
  • Keywords (if, for, def, …) are off-limits as variable names.
  • Python is dynamically typed — the same name can hold different types over time.
  • Follow snake_case for variables/functions, SCREAMING_SNAKE for constants, and PascalCase for classes.
  • Clear, descriptive names (used_ram_mb vs x) make a RAM manager script self-documenting.