What are the main built-in data types in Python?
Numbers (int, float, complex, bool), text (str), sequences (list, tuple, range), mappings (dict), sets (set, frozenset), bytes types and NoneType. Lists, dicts and sets are mutable; the rest are immutable.
Explain it simply
Think of a stationery shop. A notebook (list) lets you add and tear out pages. A printed marksheet (tuple) is fixed once issued. A register that maps roll number to name is a dict. A bag of unique raffle tickets is a set. Python ships all of these ready to use.
The groups to remember:
- Numeric:
int,float,complex, andbool(a subclass of int) - Text:
str - Sequences:
list,tuple,range - Mapping:
dict - Sets:
set,frozenset - Binary:
bytes,bytearray - Nothing:
NoneType(the valueNone)
Example
print(type(7)) # Output: <class 'int'>
print(type(7.0)) # Output: <class 'float'>
print(type("7")) # Output: <class 'str'>
print(type([7])) # Output: <class 'list'>
print(type((7,))) # Output: <class 'tuple'>
print(type({7: "seven"})) # Output: <class 'dict'>
print(type({7})) # Output: <class 'set'>
print(isinstance(True, int)) # Output: TrueWhat to say in the interview
"Python has numeric types, str, the sequence types list, tuple and range, dict for key-value data, set and frozenset, bytes types and None. list, dict, set and bytearray are mutable; int, float, str, tuple, frozenset and bytes are immutable. bool is a subclass of int, so True equals 1."
Common mistakes
- Calling
{}an empty set. It creates an empty dict; useset().