Type conversion in Python is the process of changing a value from one data type to another.
- Helps ensure correct operations and calculations.
- Examples include converting an integer to a float or a numeric string to an integer.
- Python supports two types of type conversion: Implicit Conversion and Explicit Conversion.
Implicit Type Conversion
Implicit conversion in Python happens automatically when different data types are used together in an expression.
- Python converts a smaller data type to a larger one when needed.
- Commonly occurs when integers and floats are combined.
- Conversion happens at runtime to keep results accurate.
Outputx: <class 'int'>
y: <class 'float'>
z = 20.6
z : <class 'float'>
In the above example:
- "x" is an integer and "y" is a float.
- During "z = x + y", Python implicitly converts x to a float to avoid data loss.
- As a result, "z" becomes a float with the value 20.6.
Explicit Type Conversion
Explicit conversion, also called type casting, is when a programmer manually changes a value from one data type to another.
- Done using Python’s built-in functions like int(), float(), and str().
- Gives full control over how data is interpreted or processed.
- Used when automatic conversion is not suitable.
Common type casting functions
- int() converts a value to an integer
- float() converts a value to a floating point number
- str() converts a value to a string
- bool() converts a value to a Boolean (True/False)
In the above example:
- s is a string with the value "100".
- Using int(s), the string is explicitly converted into an integer.
- This manual conversion is called explicit type conversion, and a becomes the integer 100 with type <class 'int'>.
Examples of Common Type Conversion Functions
Example 1: Converting a binary string
In the above example:
- int(s, 2) interprets the binary string '10010' as the integer 18.
- float(s) converts the string to a floating-point number.
Example 2: ASCII, Hexadecimal and Octal Conversion
OutputASCII of '4': 52
56 in Hex: 0x38
56 in Octal: 0o70
In the above example:
- ord(c) returns the ASCII code of the character '4'.
- hex() and oct() convert the integer 56 to its hexadecimal and octal representations, respectively.
Example 3: String to Tuple, Set and List
OutputTo tuple: ('g', 'e', 'e', 'k', 's')
To set: {'e', 'g', 'k', 's'}
To list: ['g', 'e', 'e', 'k', 's']
In the above example:
- tuple(s) keeps all characters including duplicates in order.
- set(s) removes duplicates and returns an unordered collection.
- list(s) returns a list of characters from the string.
Example 4: Other Conversions – Complex, String, Dictionary