Basic Syntax
Data Types
int, float, str, bool, None
Interview: Fundamental knowledge
Python is dynamically typed (types inferred at runtime) and strongly typed (no implicit type coercion). Every value in Python is an object with a type, an identity, and a value.
Numeric Types
- int: Arbitrary precision integers — no overflow!
x = 10**100works fine. - float: IEEE 754 double precision (64-bit). Beware of floating-point precision:
0.1 + 0.2 != 0.3. - complex: Complex numbers with
jsuffix:z = 3 + 4j. - bool: Subclass of int.
Trueis 1 andFalseis 0.
Sequence Types
- str: Immutable sequence of Unicode characters
- list: Mutable, ordered sequence. Heterogeneous elements allowed.
- tuple: Immutable, ordered sequence. Hashable if all elements are hashable.
- range: Memory-efficient sequence of integers.
range(10)doesn't create a list.
Collection Types
- dict: Mutable mapping of key-value pairs. Keys must be hashable. Insertion-ordered since Python 3.7.
- set: Mutable, unordered collection of unique elements. Supports set operations (union, intersection).
- frozenset: Immutable version of set. Can be used as dict keys or set elements.
Special Types
- NoneType: The type of
None— Python's null/nil value. There is only one None object. - type: The type of all types.
type(int)returns<class 'type'>. - bytes/bytearray: Binary data types for working with raw bytes.
Floating-Point Gotcha
Never compare floats with ==. Use math.isclose(a, b) or abs(a - b) < epsilon. For precise decimal arithmetic, use the decimal module.
Use Cases
Choosing the right data type for memory efficiency
Working with precise decimals using the decimal module
Using sets for fast membership testing (O(1) vs O(n))
Understanding type hierarchy for proper type checking
Common Mistakes
Comparing floats with == instead of math.isclose()
Using type() instead of isinstance() for type checking
Forgetting that bool is a subclass of int
Assuming dict ordering in Python versions before 3.7