Variables and Names
Assignment gives a value a name. Learn naming rules, snake_case, rebinding, and the names every agent loop uses.
A variable is a name stuck on a value. You write name = value. After that, the name means that value until you stick the name on something else.
The = sign is not math. It does not ask “are these equal?” That question uses ==, which you will meet in the next comparison lesson. = means assignment: “this name now points to this value.”
Agent code is full of names. Good names make the story easy to read: goal, step, budget. Bad names hide the story: x, data, tmp. When a trace prints x=3, you do not know if 3 is a step, a retry count, or a price. When it prints step=3, you do.
Assignment
Assignment is the act of giving a name to a value. Python evaluates the right side first, then attaches the name on the left.
The left chip is the name. The right chip is the value. Assignment sticks the name on that value.
A name points at a valueagent_name = class="tok-s">"atlas"
step = 0
step = step + 1
print(agent_name)
print(step)The third line reads the old step (0), adds one, and points step at the new number 1. You did not change the number 0. You moved the name to 1. Numbers do not get edited in place. Names get moved.
You can change a name as many times as you need. That is how a loop counts. Each turn does step = step + 1 and maybe budget = budget - 1.
You may also assign several names in one line: step, budget = 0, 8. That is two assignments. Beginners can skip it until unpacking. One name per line is clearer while you learn.
Naming rules
Python names:
- Start with a letter or an underscore
- Can hold letters, digits, and underscores
- Care about upper and lower case:
Goalandgoalare different - Cannot be special words like
if,for,class, orreturn
2step is illegal because it starts with a digit. max-steps is illegal as a name because - means subtract. max_steps is legal.
Python’s common style guide is called PEP 8. You do not need to memorize it. One useful rule is snake_case: lowercase words joined with underscores, like max_steps.
| Kind | Style | Example |
|---|---|---|
| Variable | snake_case | max_steps |
| Function | snake_case | call_tool |
| Constant | UPPER_SNAKE | DEFAULT_BUDGET |
A constant is a name you do not plan to change. Python does not lock it. The uppercase is a hint for humans. You can still write DEFAULT_BUDGET = 99. The language will not stop you. The hint is for readers.
Name the thing, not the type. Prefer trace over my_list. Prefer budget over num. Prefer tool_name over s. If you need the type, Python can tell you later with type(). The name should tell the job.
Underscore-only names like _ sometimes mean “I do not need this value.” You will see that in unpacking. Do not use _ for an important counter.
Names that agents use
Three names show up again and again:
goal— what “done” meansstep— how many turns the loop has runbudget— how much you can still spend (calls, tokens, or dollars)
Later you will add transcript or memory (a list of what happened), error (missing, or a message), and tool (the name of the function to run). If those names are vague, a printed trace is hard to read. Pick names that match the loop.
goal = class="tok-s">"find the weather"
step = 0
budget = 5
print(goal, step, budget)Read that print as a sentence: the goal is find the weather, we are on step 0, five calls remain. That sentence is the state of a tiny agent.
Two names can share one value
A name is a label, not a box that owns a private copy. Two labels can point at the same value.
label = agent_name does not copy the text into a new box. It sticks a second label on the same value. For numbers and text you rarely notice, because you cannot change a number in place. For a list (a row of values), a change through one name can show up through the other. We will use lists more later. Remember the rule now: assignment copies the label, not a deep clone of the value.
agent_name = class="tok-s">"atlas"
label = agent_name
print(label)
agent_name = class="tok-s">"bolt"
print(class="tok-s">"label still", label)
print(class="tok-s">"agent_name now", agent_name)After the third assignment, agent_name points at "bolt". label still points at "atlas". Rebinding one name does not rebind the other. That is different from editing a shared list, which you will see in the lists lesson.
Common mistakes
- A typo makes a new name.
max_stepandmax_stepsare different. Python will not always warn you. Print your state. - Using a name before you assign it causes
NameError. Set counters to0first. Set text to""if you need empty text. Set “missing” toNonelater. class = "Agent"is a syntax error.classis a special word. Useagent_class.==instead of=on an assignment line does not store a value. It asks a question and throws the answer away unless you print it or use it inif.- Putting spaces in a name:
max steps = 8is two names and a syntax error.
Run to execute this in your browser. Nothing is sent to a server.
Change goal and run again. Watch how every print follows the names. Then change step = step + 1 to step = step + 2 and see the printed step jump. The prints are following the labels, not a hidden calculator.
How agents use this
The agent loop is a handful of names updated every turn: goal, step, budget, and later a list of messages. A trace prints those names so you can see the story. If the names are x and tmp, you will not understand the log, and neither will the next person who debugs a failed run.
When you read someone else’s agent, start by listing the names that change each turn. Those names are the state. Everything else is helpers. If a name is assigned in three different places with three different meanings, that is a bug waiting to happen. Use one name for one job.
Passing state into functions comes later. The habit starts here: pick budget not n, update it on purpose, print it after you update it. An agent that cannot say its own step and budget out loud is already hard to operate.
Check your understanding