JJoeven

Curriculum/Python

Types: Numbers, Text, True/False, and None

Every value has a type. Learn int, float, str, bool, and None, how to convert, and why bool('False') is True.

beginner18 min3 / 37

Every value in Python has a type. The type is the kind of value it is. The type decides what you can do: add it, slice it, or turn it into JSON. "3" + "1" is "31" because both sides are text. 3 + 1 is 4 because both sides are numbers. "3" + 1 crashes, because Python will not guess.

Agents live on a small set of types. Tool arguments often arrive as JSON. JSON becomes numbers, text, true/false, lists, or None. If you do not know which one you have, your code will surprise you. A model that sends "8" (text) where you expected 8 (int) will break a budget check until you convert.

The five types you will see most

TypeExamplesTypical use
int0, 8, -1step counts, token counts
float0.002, 3.14prices, temperatures
str"search", ""prompts, names, JSON text
boolTrue, Falseyes/no flags
NoneNonemissing value, no result yet

An int is a whole number. It has no decimal point. A float is a number with a decimal point, even if the extra part is zero: 3.0 is a float. A str (string) is text. A bool (boolean) is True or False. None means missing.

Five types you will see most
intfloatstrboolNone

int for counts. float for prices. str for text. bool for yes or no. None for missing.

Five types you will see most

True, False, and None are not strings. "True" is four characters of text. True is a boolean. "None" is four characters. None is missing. Beginners mix these constantly when they read model output, because models emit text.

Quotes make a string. 128 is an int. "128" is a string of three characters. You can count tokens with the int. You cannot subtract 1 from the string without converting first.

type() tells you the kind

type(x) returns the type of x. Print it when you are confused. The printed form looks like <class 'int'>. Read the word inside: int, str, bool, NoneType.

python
print(type(128))     class="tok-c"># int
print(type(0.5))     class="tok-c"># float
print(type(class="tok-s">"hi"))    class="tok-c"># str
print(type(True))    class="tok-c"># bool
print(type(None))    class="tok-c"># NoneType

Read the output. Match it to the table above. This is the first debugging move when a tool argument “looks like a number” but will not add.

You can also compare types, but the usual check later is isinstance(x, int). type() is the flashlight. isinstance is the gate. Use the flashlight while you learn.

Convert with int() and str()

You can ask Python to change a value into another type when the change makes sense.

  • int("42")42
  • str(7)"7"
  • float("3.5")3.5
  • bool(0)False, bool(1)True

int("3.5") fails, because that text is not a whole number. Go through float first, or clean the text. int(float("3.5")) is 3.

int(3.9) cuts off the extra part toward zero. It does not round. int(3.9) is 3. round(3.9) is 4. int(-3.9) is -3, still toward zero, not toward more negative.

int("42") works. int(" 42 ") also works; extra spaces around a whole number are allowed. int("42abc") fails. int("") fails. When a model might send junk, convert inside try later. For now, convert known-clean text.

python
print(int(class="tok-s">"42"))
print(str(7))
print(int(3.9))
print(round(3.9))
print(float(class="tok-s">"3.5"))

To build a log line from a number, wrap the number: "step " + str(step). print("step", step) also works because print converts each argument. The + operator on strings does not convert for you.

A trap: bool("False")

bool("False") is True. A string with any characters in it counts as yes. The letters F-a-l-s-e do not matter. bool("") is False. bool("0") is True, because "0" is not empty.

If a flag arrives as text, compare it yourself: flag == "true" or flag.lower() == "true". Do not trust bool(flag) for English words. JSON true becomes Python True when you use json.loads. JSON "true" (with quotes) becomes the string "true". Those are different.

bool(None) is False. bool(0) is False. bool([]) is False. We go deeper on that in the True/False lesson. Remember the string trap now, because tool args are often strings.

A name can change type

This is legal: n = 3 and later n = "three".

It is usually a mistake. Keep step as an int. Keep goal as a str. Keep ready as a bool. Keep error as None or a string message, on purpose. The running program will still let you switch types. Your future self will not thank you.

JSON numbers that look whole become int. Numbers with a dot become float. A tool argument "8" is still a str until you convert it. Convert once, before the rest of the loop uses the value. Do not convert in five different ifs.

Common mistakes

  • Adding a string to a number: "step " + 3 raises TypeError. Use str(3) or print("step", 3).
  • Comparing "8" == 8, which is False. Convert first.
  • Using int("3.5") instead of int(float("3.5")).
  • Treating "None" as missing. It is text. Missing is None with no quotes.
  • Calling bool("False") and believing the English.
Live PythonOpen full playgroundpython
Output
Run to execute this in your browser. Nothing is sent to a server.

Run it. Read each type(...) line. Then change tokens = 128 to tokens = "128" and run again. The first type line should change. That is the same surprise a JSON string will give your budget math.

How agents use this

JSON uses these same types: numbers, text, true/false, lists, and null. After Python reads JSON, null becomes None. Tool arguments arrive as these types. If the model sends "3" (text) where you need a count, call int() before you use it. One clear conversion is better than silent guessing in the middle of the loop.

A tool result should use one type per field, every time. ok should be a bool, not "yes". error should be None or a string, not False. hits should be a list, even if it has one item. Mixed types make if tests lie and make traces hard to compare in tests.

When you print a trace, print the type of any value that looks “wrong.” print(type(k), k) is a complete debugging sentence. Later, isinstance will turn that sentence into a gate at the edge of call_tool. The type lesson is that gate in slow motion.

Check your understanding

What is bool("False") in Python?