Learning LibraryCore Coding LibraryKids

Numbers, text, and True/False: the kinds of values

Not every value is the same kind of thing. "brownies" is text, 12 is a number, and True is a yes. Python treats each kind differently, so it helps to know which one you're holding.

The big idea

Every value has a type: text is a string, a whole number is an integer, and a yes-or-no answer is a boolean.

See it in code

1Start simple

Text goes inside quotes. That's a string. A whole number has no quotes. That's an integer (a whole number). Here is one of each:

python
special = "brownies"
cookies = 12
print("Today's special:", special)
print("Cookies left:", cookies)
One string, one whole number:
Today's special: brownies
Cookies left: 12

The quotes are how Python tells them apart. "brownies" is words to show. 12 is a number it can count with.

2A step further

The type decides what + does. With two numbers, + adds. With two strings, + sticks them together end to end:

python
print(12 + 12)
print("12" + "12")
Same digits, very different answers:
24
1212

"12" in quotes is text that happens to look like a number. Python can't do math with it, so it glues the two pieces of text together instead.

3In the bakery

Anything a customer types arrives as a string, even digits. int() turns that text into a whole number. A check like == gives the third type, a yes-or-no value:

python
cookies_left = 3
how_many = int(input("How many cookies? "))
cookies_left = cookies_left - how_many
is_sold_out = cookies_left == 0
print("Cookies left:", cookies_left)
print("Sold out?", is_sold_out)
A customer types 3 into the output console:
How many cookies? 3
Cookies left: 0
Sold out? True

int() made the typed 3 a real number, so the subtraction worked. Then cookies_left == 0 asked a question, and Python answered with the boolean True.

The same idea, everywhere

These three types are everywhere. A painting's size=60 is a number and its color="gold" is a string. A game asks True-or-False questions sixty times a second.

Try it yourself

Change print("12" + "12") to print("12" * 3). Guess first: does it print 36? Run it, then explain to someone why it printed what it did.

The common mistake

Doing math with a number that's still text. input() always hands back a string, so input("How many? ") + 1 stops with a TypeError. Wrap it first: int(input("How many? ")).

What it unlocks

Numbers lead to doing math with code. Strings lead to working with text. And True/False values power every comparison and every if.

Older, or want more depth? Read the Teens version →