In our previous sessions, we explored Python’s fundamental data types including int
, float
, complex
, bool
, and str
. Now, let’s move on to the concept of typecasting—a crucial topic when working with different data types in Python.
What is Typecasting?
Typecasting (also called type conversion or type coercion) is the process of converting a value from one data type to another. For example, converting an int
to a float
, or a float
to a str
.
Python provides five built-in functions for performing typecasting:
int()
– converts a value to an integer typefloat()
– converts a value to a float typecomplex()
– converts a value to a complex typebool()
– converts a value to a boolean typestr()
– converts a value to a string type
The int()
Function
The int()
function is used to convert compatible values to an integer (int
) type. Its syntax is simple:
int(value)
Now let’s explore how int()
behaves with various input types.
1. Float to Int Conversion
If you pass a float
value to int()
, Python truncates the decimal part and returns only the integer portion.
Example:
print(int(10.989)) # Output: 10 print(int(-5.67)) # Output: -5
Key Point: The fractional digits after the decimal point are discarded, not rounded.
2. Complex to Int Conversion
Attempting to convert a complex
number to an int
will result in a TypeError. Python does not support direct conversion from complex to int.
Example:
print(int(10 + 20j)) # TypeError: can't convert complex to int
3. Boolean to Int Conversion
Booleans can be safely converted to integers:
True
becomes1
False
becomes0
Example:
print(int(True)) # Output: 1 print(int(False)) # Output: 0
4. String to Int Conversion
You can convert a str
to int
, but the string must meet strict conditions:
- The string must contain a valid integer literal.
- It must be written in base 10 (decimal).
- Strings representing binary (
0b...
), octal (0o...
), hexadecimal (0x...
), or float ("10.5"
) are not allowed.
print(int("15")) # Output: 15 print(int("0b1111")) # ValueError: invalid literal for int() with base 10 print(int("10.5")) # ValueError: invalid literal for int() with base 10
Important: int()
cannot convert a string that represents a float or a number in non-decimal bases.
The float() function
1. Converting Other Types to float
Python provides the built-in float()
function to convert various types of values to the float
type.
Supported Conversions:
a. Integer (int
) to Float
Any integer value can be converted to a float without restrictions, regardless of its base (decimal, binary, hexadecimal).
print(float(15)) # 15.0 print(float(0b1111)) # 15.0 (binary) print(float(0xFACE)) # 64206.0 (hexadecimal)
float(int)
simply appends.0
to the integer value.- Works even with binary (
0b
) and hexadecimal (0x
) formats.
b. Boolean (bool
) to Float
Python treats:
True
as1
False
as0
print(float(True)) # 1.0 print(float(False)) # 0.0
c. String (str
) to Float
You can convert strings to float only if:
- The string contains a valid int or float in base 10.
- It must not contain binary (
0b
) or hexadecimal (0x
) format.
print(float("10")) # 10.0 print(float("20.6")) # 20.6 print(float("0xFACE")) # ValueError print(float("Durga")) # ValueError
Note: Any string that does not represent a base-10 int or float will raise a ValueError
.
d. Complex to Float
Not allowed. Attempting to convert a complex number to a float will raise a TypeError
.
print(float(10 + 20j)) # TypeError: can't convert complex to float
The complex() function
To convert values to the complex
type, Python provides the built-in complex()
function. There are two forms of using this function.
complex(x) # Only real part is provided complex(x, y) # Both real and imaginary parts are provided
Form 1: complex(x)
– Only Real Part
In this form:
x
is treated as the real part- The imaginary part is automatically set to 0
Supported Conversions:
a. int to complex
print(complex(10)) # (10+0j) print(complex(0b1111)) # (15+0j)
b. float to complex
print(complex(10.5)) # (10.5+0j)
c. bool to complex
print(complex(True)) # (1+0j) print(complex(False)) # 0j
d. str to complex
Same rule as float()
:
- The string must represent an integer or float in base 10
- Binary (
0b
) or hexadecimal (0x
) is not allowed
print(complex("10")) # (10+0j) print(complex("10.5")) # (10.5+0j) print(complex("0b1111")) # ValueError: complex() arg is a malformed string print(complex("Durga")) # ValueError
Form 2: complex(x, y)
– Real and Imaginary Parts
In this form:
x
is treated as the real party
is treated as the imaginary part
Both x
and y
can be int
, float
, or bool
.
print(complex(10, 20)) # (10+20j) print(complex(10.5, 20.6)) # (10.5+20.6j) print(complex(True, False)) # (1+0j)