Basic Syntax
Type Conversion
Converting between data types
Interview: Data manipulation
Python is strongly typed — it doesn't implicitly convert between incompatible types. You must use explicit type conversion (casting) to change a value's type. Understanding conversion rules prevents subtle bugs.
Explicit Conversion (Casting)
int(x)— converts to integer (truncates floats, parses numeric strings)float(x)— converts to floatstr(x)— converts to string representationbool(x)— converts to boolean using truthiness ruleslist(x),tuple(x),set(x)— convert between sequence typesdict(x)— converts from iterable of key-value pairs
Truthiness Rules
Understanding what evaluates to False is crucial:
False,None,0,0.0- Empty sequences:
"",[],(),{},set() - Everything else is truthy (including non-zero numbers and non-empty collections)
Implicit Conversion
Python performs limited implicit conversion in numeric contexts: int + float = float, bool + int works because bool is a subclass of int. But "5" + 3 raises TypeError.
Use Cases
Parsing user input from strings to appropriate types
Converting between number bases (binary, hex, octal)
Data cleaning and transformation pipelines
Safe type conversion with error handling
Common Mistakes
Assuming int() rounds — it truncates toward zero
Expecting bool("False") to be False (non-empty strings are truthy)
Not handling ValueError when converting user input
Confusing float("nan") behavior — nan != nan is True!