What is type conversion?
The process
of converting values from one data type to another data type is called type
conversion. In python programming, there are two categories of type conversions:
- Implicit type conversion
- Explicit type conversion
Let’s
discuss them in detail.
Implicit type conversion:
In implicit
type conversion, python interpreter converts lower-level data type to higher-level data type of same nature automatically without any need of user participation.
Let’s
consider the following example of adding an integer and float number to
understand it the implicit type conversion:
number_int = 22 number_float = 2.2 final_answer = number_int +
number_float print("Type of int:
", type(number_int)) print("Type of float:
", type(number_float)) print("Final answer:
", final_answer) print("Type of final
answer: ", type(final_answer)) OUTPUT: Type of int: <class 'int'> Type of float: <class 'float'> Final answer: 24.2 Type of final answer: <class 'float'> |
In implicit
type conversion, python interpreter take care so no loss of data occurs. Now,
let’s take another example to convert the high-level data type to low-level and see
whether it implicitly works or not!
number_int = 22 number_string =
"33" final_answer = number_int +
number_string print("Type of int:
", type(number_int)) print("Type of string:
", type(number_string)) OUTPUT: Traceback (most recent call
last): File "./prog.py", line 4, in
<module> TypeError: unsupported
operand type(s) for +: 'int' and 'str' |
Python
interpreter did not convert the string to int implicitly so, it needs user
involvement to do so.
Explicit
type conversion:
Conversion
of the object of one data type to another data type by the user is called explicit type
conversion.
Some predefined
functions like int(), float() and str() etc are used to perform type
conversion. It is also called typecasting as a user cast the type of object.
In this
type of conversion there are chances of data loss as the object is forced to
convert its type. Consider the example below of adding string and integer by
converting the sting to integer first:
number_int = 22 number_string = "33" string_to_int = int(number_string) final_answer = number_int + string_to_int print("Type of int: ", type(number_int)) print("Type of string: ", type(number_string)) print("Type of string after conversion: ", type(string_to_int)) print("Final answer: ", final_answer) print("Type of final answer: ", type(final_answer)) OUTPUT: Type of int: <class
'int'> Type of string: <class
'str'> Type of string after conversion:
<class 'int'> Final answer: 55 Type of final answer: <class
'int'> |
That’s all
about type conversion and typecasting in python. If you found this article
helpful share it with others. Let me know your queries and suggestions in the
comment section below. Do subscribe my site and channel as I’m going to upload
a series of articles and videos on programming in python and I don’t want you
to miss any update.
Moreover,
you can also contact me and share your queries on Instagram, Facebook or
Twitter. I would love to provide a solution to your issues.
Stay tuned
and strive to learn. Keep learning, keep growing!
0 Comments