Python Typecasting - Converting Data Types
What is Typecasting?
Typecasting is the process of converting a value from one data type to another data type.
For example:
- Converting an integer to a float
- Converting a string to an integer
- Converting a boolean to an integer
In Python, typecasting is straightforward because the language provides built-in functions that make these conversions easy and explicit.
Built-in Typecasting Functions in Python
Python provides five important built-in functions for type conversion:
int()→ Converts a value to integer typefloat()→ Converts a value to floating-point typecomplex()→ Converts a value to complex typebool()→ Converts a value to boolean typestr()→ Converts a value to string type
Understanding the int() Function
The int() function is used to convert values of other data types into integers.
Syntax
int(x)
Where x can be a float, boolean, or string (with certain rules).
1. Float to Integer Conversion
One of the most common conversions is from float to integer.
Example:
x = 10.989
print(int(x)) # 10
When a float is converted to an integer, the digits after the decimal point are simply removed (not rounded).
- 10.989 becomes 10
- -3.7 becomes -3
This behavior is called truncation, not rounding.
2. Complex to Integer Conversion
Now let us consider whether we can convert a complex number into an integer.
Example:
x = 10 + 20j
print(int(x)) # TypeError: can't convert complex to int
Python does not allow conversion of complex numbers to integers, and attempting to do so results in a TypeError.
This is because a complex number has both real and imaginary parts, and Python cannot decide which part to retain.
3. Boolean to Integer Conversion
Boolean values can be easily converted into integers.
print(int(True)) # 1
print(int(False)) # 0
In Python, True is treated as 1 and False is treated as 0.
This is very useful in conditions, counters, and logical operations.
4. String to Integer Conversion
String to integer conversion is slightly tricky because it follows strict rules.
print(int("15")) # 15
For a string to be converted into an integer:
- The string must contain only numeric digits (0–9)
- The number must be in base 10 (decimal form)
Invalid Examples
1. Binary format (not allowed directly as string)
print(int("0b1111")) # ValueError: invalid literal for int() with base 10
2. Floating-point string
print(int("10.5")) # ValueError: invalid literal for int() with base 10
Understanding the float() Function
The float() function is used to convert values of other data types into floating-point numbers.
Floating-point numbers are numbers that contain decimal points, such as 10.5, 3.14, etc.
1. Integer to Float Conversion
print(float(15)) # 15.0
When an integer is converted into a float, Python simply adds a decimal point and represents it as a floating-point number.
Interestingly, Python allows integers in different bases (binary, hexadecimal, octal) to be converted as well.
print(float(0b1111)) # Binary 15.0
print(float(0xFF)) # Hexadecimal 255.0
There are no restrictions when converting integers to float; Python handles all valid integer representations seamlessly.
2. Complex to Float Conversion
print(float(10 + 20j)) # TypeError: can't convert complex to float
Python does not allow conversion of complex numbers into float values, and attempting to do so results in a TypeError. This restriction exists because a complex number has both real and imaginary components, and a float cannot represent both simultaneously.
3. Boolean to Float Conversion
print(float(True)) # 1.0
print(float(False)) # 0.0
Boolean values are internally treated as integers (True = 1, False = 0), and when converted to float, they become 1.0 and 0.0 respectively.
4. String to Float Conversion
String conversion to float is allowed, but it follows strict rules.
print(float("10")) # 10.0
print(float("20.6")) # 20.6
The string must satisfy the following conditions:
- It should contain either an integer value or a floating-point value
- The number must be in base 10 (decimal format only)
Invalid Examples
1. Hexadecimal string
print(float("0xFF")) # ValueError
2. Non-numeric string
print(float("James")) # ValueError
Understanding the complex() Function
Python provides two ways to use the complex() function:
Form 1: Single Argument
complex(x)
Here, x becomes the real part, and the imaginary part is automatically set to 0.
print(complex(10)) # (10+0j)
print(complex(10.5)) # (10.5+0j)
print(complex(True)) # (1+0j)
String to Complex (Single Argument)
print(complex("10")) # (10+0j)
print(complex("10.5")) # (10.5+0j)
If a string is passed, it must contain a valid integer or float in base 10.
Form 2: Two Arguments
complex(x, y)
- x → Real part
- y → Imaginary part
print(complex(10, 20)) # (10+20j)
print(complex(10.5, 20.6)) # (10.5+20.6j)
Important Rules for Two-Argument Form
Rule 1: If First Argument is String, Second Argument Not Allowed
print(complex("10", 20)) # TypeError
If the first argument is a string, you cannot pass the second argument.
Rule 2: Second Argument Cannot Be String
print(complex(10, "20")) # TypeError
The second argument must always be a numeric value (int or float), not a string.
Understanding the bool() function
The bool() function is used to convert any given value into a boolean type, which means the result will always be either True or False.
** If a value evaluates to zero or is considered "empty", it becomes False; otherwise, it becomes True.**
1. Integer to Boolean Conversion
print(bool(10)) # True
print(bool(0)) # False
print(bool(-5)) # True
Any non-zero integer value is converted to True, while zero is converted to False.
It does not matter whether the number is positive or negative; only zero matters.
2. Float to Boolean Conversion
print(bool(0.0)) # False
print(bool(0.0001)) # True
print(bool(-0.00001)) # True
The same rule applies to floating-point numbers: zero becomes False, and any non-zero value becomes True.
Even extremely small values like 0.0001 are considered non-zero and therefore evaluate to True.
3. Complex Number to Boolean Conversion
print(bool(0 + 0j)) # False
print(bool(1 + 0j)) # True
print(bool(0 + 2j)) # True
A complex number is considered False only when both its real and imaginary parts are zero; otherwise, it is considered True.
4. String to Boolean Conversion
print(bool("True")) # True
print(bool("False")) # True
print(bool("Yes")) # True
print(bool("")) # False
For strings, the rule is completely different:
- If the string is empty (""), it evaluates to False
- If the string is non-empty (even if it contains "False"), it evaluates to True