A value’s type determines which operations it supports. Common types include:
| Example | Type | Meaning |
|---|---|---|
-8 |
int |
whole number |
2.5 |
float |
floating-point number |
"8" |
str |
string, or text |
True |
bool |
Boolean; the other value is False |
None |
NoneType |
special value often used for absence |
Quotes matter: 8 is a number; "8" is text. Single and double quotes both create strings. Boolean literals are spelled True and False with capitals.
reading = None
print(reading)
reading = 18
print(reading, type(reading))
None
18 <class 'int'>
None is a value bound to reading, not an undefined name. It might mean a reading has not arrived; the program decides that meaning. None, 0, False, "", and "None" are distinct values. print("None") and print(None) both display None, so display alone does not reveal the type; type(value) helps distinguish them.
Python is dynamically typed: values have types, and a name can later refer to a value of another type. Type compatibility is generally checked when an operation runs, so code on an unexecuted path may contain an error that has not yet appeared. In statically typed languages, a type checker examines code before it runs and may reject incompatible operations; the exact rules vary by language, and some checks still happen at runtime. Optional Python type checkers can also examine annotations before execution, but annotations do not make ordinary Python runtime-enforced. Separate names for unrelated kinds of data often make code easier to follow. Dynamic typing does not permit every operation: "8" + 2 raises TypeError.
Converting text to a number
int can interpret a string of integer digits as a number. It creates a new value; it does not change the original string.
count_text = "7"
count = int(count_text)
print(count + 2)
print(count_text + "2")
9
72
The first addition uses numbers; the second joins strings. When reading from a person, input("Count: ") displays a prompt and returns the entered line as a string, even if the person types digits. For example, if they enter 7, int(input("Count: ")) converts that text to the integer 7; without int, adding a number to the returned string raises TypeError. An end-of-file condition instead raises EOFError. Conversion can fail: int("many") raises ValueError, because that text is not an integer. Later we will handle this kind of failure.