Data Types
Every value in Python has a data type that decides what the value is and what you can do with it. You can add two numbers, but you can't sensibly divide a name by a phone number. Knowing the core types is the foundation for everything else in Python.
The core built-in types
| Type | Name | Examples | Used for |
|---|---|---|---|
int | Integer | 21, -5, 0, 1000000 | Whole numbers: counts, ages, KES amounts without cents |
float | Floating-point number | 3.14, -0.5, 2.0 | Numbers with a decimal point: measurements, averages |
str | String | "Kisumu", 'A', "" | Text of any length |
bool | Boolean | True, False | Yes/no answers and conditions |
NoneType | None | None | "No value yet" or "nothing here" |
Use type() to check the type of any value:
print(type(42))
print(type(3.14))
print(type("Nairobi"))
print(type(True))
print(type(None))
Numbers: int and float
An int is a whole number with no decimal point. Python integers can be as large as your computer's memory allows. A float has a decimal point, even if the part after it is zero.
students = 45 # int height = 1.72 # float whole = 2.0 # still a float, because of the decimal point print(type(whole)) print(students * 2) print(height * 2)
There's also a complex type for numbers such as 3 + 4j, used in engineering and science. You won't need it in this series. Numbers get their own page: Numbers & Arithmetic.
Text: str
A string is a sequence of characters inside single or double quotes. Anything in quotes is text, even if it looks like a number.
print(42 * 2)
print("42" * 2)
The first line multiplies a number. The second repeats a string twice, which is a very different result. Strings are covered in depth on the Strings page.
Yes or no: bool
A Boolean has only two possible values: True and False (always with a capital letter). Comparisons produce Booleans, and they drive every decision your program makes.
age = 17 is_adult = age >= 18 print(is_adult) print(type(is_adult)) print(10 > 3)
Behind the scenes, bool is a special kind of int: True behaves like 1 and False like 0. So True + True gives 2. This is occasionally handy for counting, but don't rely on it to make code clever.
Nothing: None
None is Python's way of saying "there is no value here". It is useful as a placeholder, for example before a user has entered their phone number. It is not the same as 0, False or an empty string; it is its own special value.
phone = None print(phone) print(phone is None)
To check for None, use is None rather than == None. You'll see why on the Operators page.
Checking types with isinstance()
isinstance(value, type) answers "is this value of this type?" with True or False. It is the usual way to check types inside programs.
print(isinstance(7, int))
print(isinstance(7.0, int))
print(isinstance("7", str))
print(isinstance(True, int))
The last result surprises many people: because bool is a kind of int, True counts as an integer too.
Collection types (a preview)
Python also has types that hold many values at once. Each has its own page in the Data Structures chapter.
| Type | Example | Key feature |
|---|---|---|
list | ["maize", "beans"] | Ordered and changeable |
tuple | (-1.29, 36.82) | Ordered and unchangeable |
set | {1, 2, 3} | Unique items, no order |
dict | {"name": "Amina"} | Key–value pairs |
Types are also either mutable (can be changed after creation, like lists and dictionaries) or immutable (can't be changed, like ints, floats, strings and tuples). This distinction matters more as your programs grow.
type() reports.Mixing text and numbers without converting them. "Age: " + 21 raises a TypeError because Python won't guess whether you want text or maths. Convert first with str(21), or pass both to print() separated by a comma. See Type Conversion.
Writing true, false or none in lowercase. Python only recognises True, False and None with capital first letters; the lowercase versions cause a NameError.
Key points
- The core types are
int,float,str,boolandNoneType. - A value's type decides what you can do with it;
"42"and42behave differently. - Use
type()to inspect a value andisinstance()to test it. Nonemeans "no value"; check it withis None.- Lists, tuples, sets and dictionaries hold collections of values.
Tip: use the ← and → keys to move between pages.
