Data Types
Python has a rich set of built-in data types. The main categories are: numeric types (int, float, complex), text type (str), boolean (bool), sequence types...
Overview of Python Data Types
Python has a rich set of built-in data types. The main categories are: numeric types (int, float, complex), text type (str), boolean (bool), sequence types (list, tuple, range), mapping type (dict), set types (set, frozenset), and binary types (bytes, bytearray, memoryview). Every value in Python is an object, and every object has a type. You can always inspect the type of any value with the built-in type() function.
Mutable vs Immutable Types
One of the most important distinctions in Python is between mutable and immutable types. Immutable objects cannot be changed after creation: int, float, complex, str, tuple, and frozenset are all immutable. Mutable objects can be changed in place: list, dict, set, and bytearray are mutable. This matters for function arguments (mutable arguments can be modified inside a function, affecting the caller) and for using objects as dictionary keys (only immutable objects are hashable and can be keys).
Type Conversion
Python provides built-in functions to convert between types. int("42") converts a string to an integer. float("3.14") converts to a float. str(100) converts an integer to a string. list("abc") converts a string to a list of characters ["a", "b", "c"]. tuple([1,2,3]) converts a list to a tuple. bool(0) returns False; bool(1) returns True. Conversion functions raise ValueError if the input is not in the expected format.
Checking Types
Use type(x) to get the exact type of an object. Use isinstance(x, SomeType) to check whether x is an instance of SomeType or any of its subclasses. Example: isinstance(True, int) returns True because bool is a subclass of int in Python. For most real-world code, isinstance is preferred over type() == because it correctly handles inheritance. isinstance also accepts a tuple of types: isinstance(x, (int, float)) returns True if x is either.
None Type
None is the sole value of the NoneType. It represents the absence of a value. Functions that do not explicitly return a value return None. To test whether a variable is None, use is None rather than == None, because is tests identity (there is only one None object) while == could be overridden by a custom class. Example: if result is None: print("No result found"). This idiomatic check is consistently recommended in PEP 8.
Exam Tip
A favourite exam question: is bool a subclass of int in Python? Yes — True equals 1 and False equals 0 in arithmetic contexts. True + True gives 2. Another question: what is the difference between type() and isinstance()? type() checks the exact class; isinstance() also returns True for subclasses. A third classic: what is the type of 3 + 4j? It is complex — Python has native complex number support with real and imaginary parts accessible via .real and .imag attributes.