Python Type Conversion
Introduction
In Python, every value has a data type, such as int, float, str, or bool. Sometimes, we need to convert a value from one data type to another to perform calculations or process data correctly.
This process is called type conversion or type casting.
Python supports two main types of conversion:
- Implicit Type Conversion
- Explicit Type Conversion
Let’s understand both with practical examples.
1. What Is Type Conversion in Python?
Type conversion means changing a value from one data type to another.
For example:
a = 5
b = 5.6
print(a + b)
Output:
10.6
Here, Python automatically converts the integer 5 into a float before performing the addition.
2. Implicit Type Conversion
Implicit type conversion happens when Python automatically converts one data type into another without requiring the programmer to write a conversion function.
Python commonly performs this conversion when combining compatible numeric types.
Example: Integer to Float
a = 5
b = 5.6
result = a + b
print(result)
print(type(a))
print(type(b))
print(type(result))
Output:
10.6
<class 'int'>
<class 'float'>
<class 'float'>
Explanation:
ais an integer.bis a float.- Python converts the integer to a float for the calculation.
- The result is a float.
Another Example
x = 10
y = 2.5
z = x * y
print(z)
print(type(z))
Output:
25.0
<class 'float'>
Important Point
Python does not automatically convert every data type.
For example:
a = 4
b = '4'
print(a + b)
Output:
TypeError
This happens because Python cannot directly add an integer and a string.
3. Explicit Type Conversion
Explicit type conversion happens when the programmer manually converts a value from one data type to another using built-in conversion functions.
It is also called type casting.
Common Type Conversion Functions
| Function | Purpose |
|---|---|
int() | Converts a compatible value to an integer |
float() | Converts a compatible value to a float |
str() | Converts a value to a string |
bool() | Converts a value to a Boolean |
complex() | Creates a complex number |
Example 1: Integer to String
a = 5
result = str(a)
print(result)
print(type(result))
Output:
5
<class 'str'>
Example 2: Integer to Float
a = 4
result = float(a)
print(result)
print(type(result))
Output:
4.0
<class 'float'>
Example 3: String to Integer
a = "100"
result = int(a)
print(result)
print(type(result))
Output:
100
<class 'int'>
Example 4: String to Float
a = "12.5"
result = float(a)
print(result)
print(type(result))
Output:
12.5
<class 'float'>
Example 5: Integer to Boolean
a = 1
b = 0
print(bool(a))
print(bool(b))
Output:
True
False
4. Difference Between Implicit and Explicit Type Conversion
| Implicit Conversion | Explicit Conversion |
|---|---|
| Performed automatically by Python | Performed manually by the programmer |
| No conversion function is required | Uses functions such as int(), float() and str() |
| Usually occurs between compatible numeric types | Can convert between several compatible types |
Example: 5 + 5.6 | Example: str(5) |
5. Common Errors in Type Conversion
Type conversion does not always succeed. The input value must be compatible with the target data type.
ValueError
a = "hello"
result = int(a)
print(result)
Output:
ValueError
Python cannot convert the text "hello" into an integer.
TypeError
a = 4
b = "4"
print(a + b)
Output:
TypeError
Python does not allow direct addition of an integer and a string.
Important Note
A conversion function may fail if the supplied value is incompatible with the requested type. Always check the input before converting it.
6. Practice Questions (MCQs)
Q1. What is type conversion in Python?
A. Changing variable names
B. Changing the data type of a value
C. Creating a function
D. Removing a variable
Correct Answer: B. Changing the data type of a value
Q2. Which function converts an integer into a float?
A. str()
B. int()
C. float()
D. bool()
Correct Answer: C. float()
Q3. What is the output of the following code?
print(5 + 5.6)
A. 10
B. 10.6
C. 55.6
D. TypeError
Correct Answer: B. 10.6
Q4. Which type conversion is performed automatically by Python?
A. Explicit conversion
B. Manual conversion
C. Implicit conversion
D. Forced conversion
Correct Answer: C. Implicit conversion
Q5. What is the output of the following code?
a = str(5)
print(type(a))
A. <class 'int'>
B. <class 'float'>
C. <class 'str'>
D. <class 'bool'>
Correct Answer: C. <class 'str'>
Q6. What happens when you execute int("hello")?
A. Returns 0
B. Returns 1
C. Returns "hello"
D. Raises ValueError
Correct Answer: D. Raises ValueError