Python ยท Chapter 6 of 45
Python Data Types
Python has a small set of built-in types that cover almost every everyday need.
Core types are: `int`, `float`, `str`, `bool`, `list`, `tuple`, `dict`, `set` and `None`. Use `type(x)` to check any value's type.
Numeric, text, boolean
`int` for whole numbers, `float` for decimals, `str` for text, `bool` for True/False.
Collections
`list` (ordered, mutable), `tuple` (ordered, immutable), `dict` (key-value), `set` (unique values).
Example 1 (python)
print(type(10))
print(type(3.14))
print(type("hi"))
print(type(True))Output
<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>type() returns the class of any value.
Example 2 (python)
items = [1, 2, 3]
user = {"name": "Sam"}
print(type(items), type(user))Output
<class 'list'> <class 'dict'>list and dict are Python's most-used collection types.
Key points
- Numeric: int, float, complex.
- Text: str.
- Collections: list, tuple, set, dict.
- None represents 'no value'.
๐ก Note: Use `isinstance(x, int)` instead of `type(x) == int` when you also want to accept subclasses.
