Data Types
Prerequisites: Variables
Every value in Python has a type, which determines what you can do with
it. The core built-in types are int (whole numbers), float (decimal
numbers), str (text), bool (True/False), and None (the absence
of a value).
type(25) # <class 'int'>
type(25.0) # <class 'float'>
type("25") # <class 'str'>
type(True) # <class 'bool'>
"25" (a string) and 25 (an integer) look similar but behave
completely differently — "25" + "5" joins the text into "255", while
25 + 5 adds the numbers into 30.
Common mistakes
- Assuming text that looks numeric behaves like a number. Convert
explicitly with
int("25")orfloat("25.0")first.