In Python the type of a variable is originally determined at runtime, this means the type of a variable is based on the value assigned to it rather than being specified explicitly in the code. In certain cases, where we need to change the datatype of a variable to perform certain operations or calculations Typecasting is used.
Now let's dive right in!
What is Typecasting?
Typecasting, also known as type conversion, in Python Typecasting refers to the process of converting a value from one data type to another. This can be useful in situations where you want to perform operations or use functions that only work with certain data types. Python provides several built-in functions for typecasting, which makes it easy to convert different datatypes.
In Python, the most commonly used data types are integers, floats, strings, and booleans. Typecasting between these data types can be done using the following functions:
int() - Converts a value to an integer type.
float() - Converts a value to a floating-point type.
str() - Converts a value to a string type.
bool() - Converts a value to a boolean type.
Types of Typecasting in Python
There are mainly two categories of typecasting in Python
Implicit Typecasting
Explicit Typecasting
Implicit Typecasting
Implicit typecasting, also known as type coercion or type conversion, is the automatic conversion of one data type to another data type in Python. In this process, the user is not manually involved. It occurs when Python tries to operate on two values that have different data types.
Some examples of implicit typecasting in Python:
Converting Integer to Float
When an integer is divided by a float, the result is automatically converted to a float. This is because Python will automatically promote the integer to a float to perform the division operation.
a = 10
b = 3.0
c = a / b
print(c) # Output: 3.3333333333333335
print(type(c)) # Output: <class 'float'>
In this example, the variable a
is an integer and the variable b
is a float. When we divide a
by b
, Python implicitly converts a
to a float and returns the result as a float.
Converting Boolean to Integer
When a boolean value is used in an arithmetic operation, Python will automatically convert True to 1 and False to 0.
a = True
b = False
c = a + b
print(c) # Output: 1
print(type(c)) # Output: <class 'int'>
In this example, we add two boolean values a
and b
. Python implicitly converts True
to 1
and False
to 0
before performing the addition operation.
It's important to keep in mind that implicit typecasting can sometimes result in unexpected behavior, so it's a good practice to explicitly convert data types when necessary.
Explicit Typecasting
Explicit typecasting in Python refers to the process of converting a value of one data type to another data type with the user's involvement. In Python, you can use various built-in functions to perform explicit typecasting.
Here are some examples of explicit typecasting in Python:
Converting string to an Integer
age = "25"
print(type(age)) # output: <class 'str'>
age = int(age)
print(type(age)) # output: <class 'int'>
In the above example, we have a string variable "age"
with a value of "25"
. We use the int()
function to convert this string to an integer. The type()
function is used to check the type of the variable before and after typecasting.
Converting a Boolean to an Integer
is_active = True
print(type(is_active)) # output: <class 'bool'>
is_active = int(is_active)
print(type(is_active)) # output: <class 'int'>
In the above example, we have a boolean variable is_active
with a value of True
. We use the int()
function to convert this boolean to an integer. Since True
is equivalent to 1 and False
is equivalent to 0
in Python, the value of is_active
is changed to 1
after typecasting.
Conclusion
Typecasting in Python can be used to perform operations and calculations that are not possible with the original data type. Python provides several built-in functions for typecasting, which makes it easy to convert data types.
N/B: It is important to remember that not all types can be converted to each other, and typecasting should be used with caution to avoid any unexpected errors in your code.