Values and Their Types
Everything a program works with is a value: a number, a piece of text, a yes or a no. And every value has a type, which decides what you can do with it. Adding two numbers makes sense. Adding a number to a word does not, and Python will say so rather than guess.
Think of the ingredients in a kitchen. Flour, milk and eggs are all ingredients, but you pour one, crack another and weigh the third. The kind of ingredient tells you which handling applies. A value's type does the same job in a program: it tells Python which operations make sense.
Whole Numbers and Decimals
Python has two everyday kinds of number. A whole number such as 42 is an int, short for integer. A number with a decimal point, such as 3.5, is a float, a name that comes from the way computers store decimals. The dot is the whole difference: 3 is an int, and 3.0 is a float.
For most arithmetic you can mix the two freely and Python does the sensible thing. The difference starts to matter with money and with division, which is the subject of Topic 3.
Text
A piece of text is a str, short for string, as in a string of characters. You write it between quotation marks: "hello". The quotes are not part of the text. They mark where it starts and ends, so Python knows that what is inside is text to keep, not a name to look up. That is exactly why typing hello without quotes at the prompt, in Chapter 1, produced an error.
True and False
The last basic type is bool, named after the mathematician George Boole. It has only two values, True and False, written with a capital first letter and no quotes. Every yes-or-no question a program asks, such as whether a price is under 15, has one of these two as its answer. Chapter 3 is built on them.
Asking a Value Its Type
When you are unsure what kind of value you are holding, ask. The built-in type function tells you. At the prompt, each of the four answers names the type in a short form: int, float, str and bool.
>>> type(42)
<class 'int'>
>>> type(3.5)
<class 'float'>
>>> type("hello")
<class 'str'>
>>> type(True)
<class 'bool'>
Now the surprise that makes types worth learning. Five plus five is ten, as expected. But the text five plus the text five is the text fifty-five: for strings, the plus sign means join them together. And the text five plus the number five is an error. Read the last line of the message: a TypeError, saying Python can only join text to text, not to a whole number.
>>> 5 + 5
10
>>> "5" + "5"
'55'
>>> "5" + 5
Traceback (most recent call last):
File "<python-input-6>", line 1, in <module>
"5" + 5
~~~~^~~
TypeError: can only concatenate str (not "int") to str
Python refuses to guess whether you meant the number fifty-five or the number ten, and that refusal is a feature. A program that silently guessed would sometimes guess wrong, and you would find out much later. Concatenate, in the message, is simply the technical word for joining text.
| Type | Looks like | Example | Used for |
|---|---|---|---|
int | digits, no dot | 42 | counting, whole amounts |
float | digits with a dot | 3.5 | measurements, averages |
str | anything in quotes | "hello" | names, messages, text |
bool | True or False | True | answers to yes-or-no questions |
- "
"42"is a number because it has digits in it." The quotes make it text: to Python it is two characters, a 4 and a 2. It becomes a number only when you convert it, which Topic 5 shows. - "
trueworks the same asTrue." Python is case-sensitive. OnlyTruewith a capital T is the value;trueis just an unknown name. - "Whole numbers and decimals are the same type."
3is an int and3.0is a float. They mix well in arithmetic, but they are different types, and it shows in division and with money.
- Most early errors are type errors. Knowing the four basic types makes their messages readable instead of mysterious.
- Every value that arrives from a person, a file or the internet starts life as text. "What type is this really?" becomes a habit that saves hours from Topic 5 onwards.
Knowledge Check
What is the type of "42", written with the quotes?
- An int, because it is made only of digits
- A str, because the quotes make it plain text
- A float, because Python adds a decimal point
- A bool, because it is neither text nor a number
What does "5" + "5" give?
- The number 10, because both are fives
- An error, because text cannot be added at all
- The text
'55', because plus joins text together - The number 55, because Python joins the digits
Which of these is Python's value for "yes"?
true, written in lower case like other words"True", written inside quotation marksyes, which Python reads as a true answerTrue, with a capital T and no quotes
Why does Python refuse to run "5" + 5?
- It will not guess whether you meant joining or adding
- The number 5 is too small to be joined to text
- Plus signs are only allowed between two numbers
- It needs the values to be written on separate lines
You got correct