The process of converting one data type to another data type is called Typecasting or Type Coercion or Type Conversion.
The topics that I’ll be focusing on in this article are:
When the type conversion is performed automatically by the interpreter without the programmer’s intervention, that type of conversion is referred to as implicit type conversion.
myInt = 143 # Integer value. myFloat = 1.43 # Float value.myResult = myInt + myFloat # Sum result
print(“datatype of myInt:”,type(myInt))
print(“datatype of myFloat:”,type(myFloat))print(“Value of myResult:”,myResult)
print(“datatype of myResult:”,type(myResult))
The output for the above program will be:
datatype of myInt: <class ‘int’>
datatype of myFloat: <class ‘float’>
Value of myResult: 144.43
datatype of myResult: <class ‘float’>
In the above program,
integer
, the datatype of myFloat is a float
.float
data type because Python converts smaller data type to larger data type to avoid the loss of data.This type of conversion is called Implicit Type conversion (or) UpCasting.
In Explicit Type Conversion, users convert the data type of an object to the required data type. We use predefined in-built functions like:
The syntax for explicit type conversion is:
(required_datatype)(expression)
This type of conversion is called Explicit Type conversion (or) DownCasting.
We can use this function to convert values from other types to int.
For example:
>>> int(123.654)
123>>>int(False)
0>>> int(“10”)
10>>> int(“10.5”)
ValueError: invalid literal for int() with base 10: ‘10.5’>>> int(“ten”)
ValueError: invalid literal for int() with base 10: ‘ten’>>> int(“0B1111”)
ValueError: invalid literal for int() with base 10: ‘0B1111’>>> int(10+3j)
TypeError: can’t convert complex to int
This function is used to convert any data type to a floating point number.
For example:
>>> float(10)
10.0>>> float(True)
1.0>>> float(False)
0.0>>> float(“10”)
10.0>>> float(“10.5”)
10.5>>> float(“ten”)
ValueError: could not convert string to float: ‘ten’>>> float(10+5j)
TypeError: can’t convert complex to float>>> float(“0B1111”)
ValueError: could not convert string to float: ‘0B1111’
This functionis used to convert real numbers to a complex (real, imaginary) number.
You can use this function to convert a single value to a complex number with real part x and imaginary part 0.
For example:
>>> complex(10)
10+0j>>> complex(10.5)
10.5+0j>>> complex(True)
1+0j>>> complex(False)
0+0j>>> complex(“10”)
10+0j>>> complex(“10.5”)
10.5+0j>>> complex(“ten”)
ValueError: complex() arg is a malformed string
If you want to convert X and Y into complex number such that X will be real part and Y will be imaginary part.
For example:
>>> complex(10,-2)
10-2j>>> complex(True, False)
1+0j
This function is used to convert any data type to boolean data type easily. It is the most flexible data type in Python.
For example:
>>> bool(0)
False>>> bool(1)
True>>> bool(10)
True>>> bool(0.13332)
True>>> bool(0.0)
False>>> bool(10+6j)
True>>> bool(0+15j)
True>>> bool(0+0j)
False>>> bool(“Apple”)
True>>> bool(“”)
False
Note: With the help of bool function, you can convert any type of datatype into boolean and the output will be - For all values it will produce True except 0, 0+0j and for an Empty String.
This function is used to convert any type into a string type.
For example:
>>> str(10)
‘10’>>> str(10.5)
‘10.5’>>> str(True)
‘True’>>> str(False)
‘False’>>> str(10+5j)
‘10+5j’>>> str(False)
‘False’
integer_number = 123 # Int
string_number = “456” # Stringprint(“Data type of integer_number:”,type(integer_number))
print(“Data type of num_str before Type Casting:”,type(num_str))string_number = int(string_number)
print(“Data type of string_number after Type Casting:”,type(string_number))number_sum = integer_number + string_number
print(“Sum of integer_number and num_str:”,number_sum)
print(“Data type of the sum:”,type(number_sum))
When we run the above program the output will be:
Data type of integer_number: <class ‘int’>
Data type of num_str before Type Casting: <class ‘str’>
Data type of string_number after Type Casting: <class ‘int’>
Sum of integer_number and num_str: 579
Data type of the sum: <class ‘int’>
In the above program,
int()
function to perform addition.I covered pretty much everything that is required to perform any type of typecasting operation in Python3.
Hope this helped you learn about Python Typecasting in a quick and easy way.
If you liked this article please click on the clap and leave me your valuable feedback.
☞ Complete Python: Go from zero to hero in Python
☞ An A-Z of useful Python tricks
☞ A Complete Machine Learning Project Walk-Through in Python
☞ A Feature Selection Tool for Machine Learning in Python
☞ Learning Python: From Zero to Hero
☞ MongoDB with Python Crash Course - Tutorial for Beginners
☞ Introduction to PyTorch and Machine Learning
☞ Python Tutorial for Beginners (2019) - Learn Python for Machine Learning and Web Development
☞ Build Your First Open Source Python Project
☞ Complete Python Bootcamp: Go from zero to hero in Python 3
☞ Python and Django Full Stack Web Developer Bootcamp
☞ The Python Bible™ | Everything You Need to Program in Python
Originally published by PALAKOLLU SRI MANIKANTA at https://medium.freecodecamp.org
#python