- Syntax
- Example 1: Convert Integer to Float
- Example 2: Convert Float to Integer
- Example 3: Convert Integer to Complex
- Example 4: Convert Float to Complex
- Example 5: Convert String to Integer
- Example 6: Convert String to Float
- Example 7: User Input Conversion
- Example 8: Arithmetic After Conversion
- Example 9: Convert Boolean Values
- Example 10: Checking Multiple Conversions
- Common Mistakes
- Best Practices
- Key Points to Remember
Numeric type conversion is the process of converting a value from one numeric data type to another.
Python provides built-in functions for converting between numeric types.
The three most commonly used conversion functions are:
int()โ Converts a value to an integer.float()โ Converts a value to a floating-point number.complex()โ Converts a value to a complex number.
Type conversion is useful when working with user input, performing calculations, or ensuring that values have the correct data type.
Syntax #
int(value)
float(value)
complex(value)
Conversion Functions #
| Function | Description |
|---|---|
int() |
Converts a value to an integer. |
float() |
Converts a value to a floating-point number. |
complex() |
Converts a value to a complex number. |
Example 1: Convert Integer to Float #
main.py
number = 25
result = float(number)
print(result)
print(type(result))
Output #
25.0
<class 'float'>
Explanation #
numberstores an integer.float()converts it into a floating-point number.- The value becomes
25.0.
Example 2: Convert Float to Integer #
main.py
number = 15.98
result = int(number)
print(result)
print(type(result))
Output #
15
<class 'int'>
Explanation #
int()removes the fractional part.- It does not round the number.
- Therefore,
15.98becomes15.
Example 3: Convert Integer to Complex #
main.py
number = 12
result = complex(number)
print(result)
print(type(result))
Output #
(12+0j)
<class 'complex'>
Explanation #
complex()converts the integer into a complex number.- The imaginary part is automatically set to
0j.
Example 4: Convert Float to Complex #
main.py
number = 8.5
result = complex(number)
print(result)
Output #
(8.5+0j)
Explanation #
- A floating-point value can also be converted into a complex number.
- The imaginary part is
0j.
Example 5: Convert String to Integer #
main.py
number = "250"
result = int(number)
print(result)
print(type(result))
Output #
250
<class 'int'>
Explanation #
- The original value is a string.
int()converts the numeric string into an integer.- The data type changes from
strtoint.
Example 6: Convert String to Float #
main.py
number = "45.75"
result = float(number)
print(result)
print(type(result))
Output #
45.75
<class 'float'>
Explanation #
float()converts the numeric string into a floating-point number.- The decimal value is preserved.
Example 7: User Input Conversion #
main.py
age = int(input("Enter your age: "))
print(age)
print(type(age))
Sample Input #
21
Output #
21
<class 'int'>
Explanation #
input()always returns a string.int()converts the input into an integer.- The converted value can be used in calculations.
Example 8: Arithmetic After Conversion #
main.py
price = "100"
total = int(price) + 50
print(total)
Output #
150
Explanation #
- The string
"100"cannot be added directly to an integer. int()converts it into a numeric value.- Arithmetic can then be performed normally.
Example 9: Convert Boolean Values #
main.py
print(int(True))
print(float(False))
Output #
1
0.0
Explanation #
Trueis converted to1.Falseis converted to0.- Boolean values behave like integers during numeric conversion.
Example 10: Checking Multiple Conversions #
main.py
print(type(int(5.5)))
print(type(float(5)))
print(type(complex(5)))
Output #
<class 'int'>
<class 'float'>
<class 'complex'>
Explanation #
int(5.5)returns an integer.float(5)returns a floating-point number.complex(5)returns a complex number.- Each conversion changes the data type appropriately.
Common Mistakes #
1. Expecting int() to Round Numbers #
Incorrect
print(int(8.9))
Incorrect Expectation #
9
Actual Output #
8
Reason
int() removes the fractional part instead of rounding the value.
Correct
print(round(8.9))
2. Converting a Non-Numeric String #
Incorrect
print(int("hello"))
Output #
ValueError: invalid literal for int() with base 10: 'hello'
Reason
Only strings representing valid numbers can be converted to numeric types.
3. Passing a Complex Number to int() #
Incorrect
print(int(3 + 4j))
Output #
TypeError: can't convert complex to int
Reason
Complex numbers cannot be converted directly to integers.
4. Expecting Conversion Functions to Modify the Original Variable #
Incorrect
number = "25"
int(number)
print(type(number))
Output #
<class 'str'>
Reason
Conversion functions return a new value.
They do not modify the original variable.
Correct
number = int(number)
print(type(number))
Best Practices #
- Convert values to the required data type before performing calculations.
- Always validate user input before converting it.
- Remember that
int()truncates the fractional part instead of rounding. - Store the converted value if it will be used later.
- Use the conversion function that matches the required numeric type.
Key Points to Remember #
- Python provides
int(),float(), andcomplex()for numeric type conversion. int()converts values to integers by removing the fractional part.float()converts values to floating-point numbers.complex()converts values to complex numbers.- Numeric strings can be converted if they contain valid numeric data.
- Conversion functions return new values and do not modify the original variables.
- Invalid conversions raise exceptions such as
ValueErrororTypeError.