Skip to content
The AI Agent Factory

13.3 Values, types and variables

Approved by
Panaversity ·
11 min read

In everyday life. The characters 4-8-2-5-0-0 can be an amount, a postal code or a part number. You add amounts. You never add postal codes. What you may do depends on what kind of thing they are, not on how they look.

What you will learn. How to make a variable, what kinds of value it can hold, and how to spot text that only looks like a number.

Why it matters. A number read from a file is only text until the code turns it into a number, so check that the code does this before it adds anything.

A value's type decides what you can do with it.

How do I make a variable?

With a name, an = and a value. Think of a label on a folder: the name is the label, and the value is what the folder holds. The = means "store this value under this name".

In TypeScript, the first word says how the label is written. const is ink: the name cannot be given a new value. let is pencil: it can. Python has neither word. The name and = are enough, and every label is in pencil.

Two things help you read the code below. Text after // in TypeScript, or # in Python, is a comment, a note for people that the program skips. And a console.log or print with two values prints both on one line. Predict what each one prints.

const terms = "Net 30";   // const, ink: cannot be given a new value
let total = 0;            // let, pencil: can be given a new value
total = total + 482500;   // read total, add to it, store it again
console.log(terms, total);

Both print:

Net 30 482500

total started at 0. The third line read it, added 482500, and stored the result under the same name.

What kinds of value are there?

Five do most of the work.

Kind of valueTypeScriptPythonBrightline example
Textstringstr"INV-8301", "Net 30"
Whole numbernumberint482500 cents
Decimal numbernumberfloatNever used for money
True or falsebooleanboolIs the invoice open?
Nothingnull or undefinedNoneA record with no due date at all

These are the words a type label uses, such as cents: number or cents: int. An agent follows the labels you give it, so choosing them is part of the first 10 percent.

Is a Python list the same as a TypeScript array?

Yes, for reading code. They are the same idea under two names, and this book says "list" for both.

TypeScript arrayPython list
How many itemsids.lengthlen(ids)
Add one at the endids.push("V003")ids.append("V003")
Its type labelstring[]list[str]

const fixes the name, not what is inside the list. The ink label stays on its folder, and you can still add a page to the folder. So push is allowed. Predict what each one prints.

const ids = ["V001", "V002"];
ids.push("V003");
console.log(ids.length, ids[2]);

Both print:

3 V003

Why is an amount from a file still text?

Everything a program reads from a CSV file arrives as text, including the amounts. "482500" is six characters, not a number, until your code converts it.

Number(...) in TypeScript and int(...) in Python convert text to a number. Predict each line, and what happens on the last one.

const fromFile = "482500";                       // text, as a file gives it
const other = "61875";                           // text
console.log(fromFile + other);                   // text + text
console.log(Number(fromFile) + Number(other));   // number + number
console.log(0 + fromFile);                       // number + text
48250061875
544375
0482500

What + does depends on the kind of value, as with the amount and the postal code. On two strings it joins them, like two postal codes written side by side. On two numbers it adds. On a number and a string, TypeScript turns the 0 into text and joins. Python stops instead.

Text with a comma in it cannot be converted. Number("4,825.00") gives NaN in TypeScript, which means "not a number", and int("4,825.00") stops with a ValueError in Python.

The TypeScript behavior is the dangerous one, because the program keeps running with a wrong value.

Why whole cents?

Because decimal numbers can be very slightly off. A computer does not store most of them exactly. Predict what each one prints.

console.log(1.10 + 2.20, 110 + 220);

Both print:

3.3000000000000003 330

The whole numbers came out exact. So this book keeps every amount in whole cents, $48.25 as 4825, and turns cents into dollars only when it prints them. Never let an agent keep money in an ordinary decimal number.

Try it

The file in 13-3 is the text-or-number program. Run it and investigate, as in 13.1.

Modify. Real exports often write an amount as "4,825.00". Send: "Please change 482500 to 4,825.00 on the first line of the file. Don't run it yet." Write down what each line will print. Then ask the agent to run it.

Make. Send: "Please make a new file in 13-3 called mine that keeps two invoice amounts as whole cents in variables, adds them, and prints the total. Don't run it yet." Check the total yourself, then ask the agent to run it.

What you should see. TypeScript keeps going: the joined text, then NaN where it converts the amount. Python stops at its second print, with a ValueError, because "4,825.00" is not a whole number.

The operating rule: convert text from a file as soon as the code reads it, keep money in whole cents, and label the types so a checker can check them.

Figure 13.3. The same characters as text and as numbers. As text, "482500" plus "61875" joins into "48250061875". As numbers, 482500 plus 61875 is 544375 cents, which is $5,443.75. Converting checked text with Number() first gives 544375.

Figure 13.3. Text or number. The characters are the same. The type decides what plus means.

Next. 13.4 shows how a program decides what to do with each invoice.

Check yourself

Question 1 / 10

0 answered

fromFile is "482500" and other is "61875". What does fromFile + other print?

Owner
Panaversity