None and type
type() tells you what kind of value a name holds. None means there is no value.
- Read the kind of a value with type()
- Store None when a value is missing
- Tell text, numbers, True, and None apart by their kinds
"12", 12, and 12.0 can look alike in a print. type() reports the kind. You will see the word class and then str for text, int for a whole number, float for a decimal, or bool for True and False. When a program misbehaves, asking for the kind is often the fastest way to see why.
Run each example to see what it prints. Running examples doesn't count toward progress.
print(type("12")) print(type(12)) print(type(12.0)) print(type(True))
None means no value. A missing coupon, a missing note, a city that was not set. It is not the text "None", and it is not 0 or False. type(None) reports NoneType.
coupon = None print(coupon) print(type(coupon))
Mistake to avoid: coupon = "None" prints exactly the same word as coupon = None. Only type() shows the difference: the first is str, a piece of text that happens to spell None.
- type() prints the kind: str, int, float, bool, or NoneType.
- None means there is no value. It is not the text "None".
- "12" and 12 look alike when printed. Their kinds are str and int. Next module: operators that compute with these values.