Type Conversion
"Practicality beats purity."
— The Zen of Python
Introduction
Type conversion is the process of converting one data type into another.
Python provides several built-in functions for converting between common data types.
Type conversion appears frequently in coding interviews, especially when working with user input, strings, numbers, lists, sets, and dictionaries.
Implicit Type Conversion
Python automatically converts compatible types when it is safe to do so.
Output
Python automatically converts the integer into a float.
Explicit Type Conversion
Explicit conversion is performed using built-in functions.
Example
int()
Converts a compatible value into an integer.
Output
Output
Notice that it truncates the decimal part instead of rounding.
Invalid conversion
Raises
Common Interview Use Cases
Convert user input
Convert digits while iterating over a string
float()
Converts a value into a floating-point number.
Output
str()
Converts any object into its string representation.
Output
Useful when building output strings.
list()
Creates a list from an iterable.
Output
Common interview trick
tuple()
Creates a tuple.
Output
set()
Creates a set.
Output
Very common for removing duplicates.
Interview example
dict()
Creates a dictionary.
Output
bool()
Converts values into boolean form.
Output
Output
Output
Output
ord()
Returns the Unicode code point of a character.
Output
Very useful in string problems.
Example
We'll cover this in detail in the Strings section.
chr()
Converts a Unicode value back into a character.
Output
Binary, Octal and Hexadecimal
Convert to binary
Output
Convert to hexadecimal
Output
Convert to octal
Output
Parsing Different Bases
Binary
Output
Hexadecimal
Output
Common Interview Examples
Reverse Integer
Remove Duplicate Characters
Convert String to Character List
Frequency Array
Time Complexity
| Operation | Complexity |
|---|---|
int() |
O(n) (for strings) |
float() |
O(n) |
str() |
O(n) |
list() |
O(n) |
tuple() |
O(n) |
set() |
O(n) |
dict() |
O(n) |
bool() |
O(1) |
ord() |
O(1) |
chr() |
O(1) |
Common Mistakes
Mistake 1
Using
Raises
Instead
Mistake 2
Expecting
to work.
It raises
Only iterables can be converted using list().
Mistake 3
Assuming
returns False.
It returns
Any non-empty string is truthy.
Best Practices
- Use
set()to remove duplicates. - Use
list()when string mutation is required. - Use
ord()andchr()for character arithmetic. - Avoid unnecessary conversions inside loops.
- Validate user input before conversion.
Key Takeaways
- Python provides built-in functions for converting between common data types.
ord()andchr()are extremely useful for string algorithms.set()is one of the easiest ways to remove duplicates.- Be aware that some conversions may raise exceptions.
- Avoid repeated conversions inside performance-critical loops.
Related Topics
- Truthy and Falsy Values
- Strings
- Sets
- Dictionaries
- ASCII and Unicode
Practice Questions
- What is the difference between implicit and explicit conversion?
- Why does
int(3.9)return3? - What does
list("hello")produce? - How do you remove duplicates using
set()? - Why is
ord()useful in interview questions?
Summary
Type conversion is a fundamental skill for Python programmers.
Understanding when and how to convert between data types allows you to write cleaner, more efficient solutions and makes many interview problems significantly easier to solve.
The next chapter introduces the operators that appear most frequently in coding interviews, including arithmetic, comparison, logical, membership, identity, and bitwise operators.