Change Data Type in Python
Python is a dynamic language where it is not always required to consider every variable type. Python supports a wide range of data types, but There are mainly six data types in Python Language: Numerical, Set, String, List, and Tuple. Every input we give is stored as a data type.
The variable type plays a vital role while converting one variable into another data type. The data type of a variable differs from code to code based on the purpose of the code. The interpreter of Python doesn't make any changes to the variable data types in advance. It is possible to change a variable's data type in Python. There are mainly two types of conversion, which are:
Implicit type conversion: This type of conversion will convert one data type to another without involving the user's commands.
Let us look at an example code to understand the implicit type conversion.
#Implicit type conversion # Python itself converts an into an integer a = 10 print(type(a)) # Python itself converts b into float b = 5.0 print(type(b)) # Python itself converts c into a float as its a floor division c = a//b print(c) print(type(c))
<class 'int'> <class 'float'> 2.0 <class 'float'>
Through the above code, we understood that Python declares the data type of a variable by itself.
Explicit type conversion: In this type of conversion, the user must convert the variable's data type. We can convert the data type by using functions like int(), str(), float(), etc. to understand this conversion better, let us look at an example code.
In this code, let us convert integer to string.
a= 50 # we used the str() function to convert int to string b= str(a) print(type(b)) print(b)
<class 'str'> 50
We have converted integer to string using the str() function in this code. And in this conversion, the user must be involved in adding the function.
String to number conversion:
We can convert a string into a number by using int() or float() functions. We have to give an appropriate string containing numerical values for proper conversions. Let us now look at an example code for the string to number conversion
# type conversion of string to number a = "2002" # Converting using int() b = int(a) print(b) print(type(b)) # Converting using float() c = float(a) print(c) print(type(c))
2002 <class 'int'> 2002.0 <class 'float'>
There are two data types in numbers in Python, which are integers and floats. When required, we change in between these data types. Let us look at these conversions one by one, including some example codes.
Float to integer conversion:
A float can be converted into an integer using the function int(). Let us get this concept by taking an example code using this int() function.
#float to integer conversion with int() function a= 23.45 b= int(a) print(b) print(type(b))
23 <class 'int'>
Integer to float conversion:
An integer can be converted into float using the function float(). Let us get this concept by taking an example code using this float() function.
# integer to float conversion with float() function a= 234 b= float(a) print(b) print(type(b))
234.0 <class 'float'>
Tuple to list conversions:
We can convert a tuple to a list by using the function list(). Let us understand this concept by using the list() function as an example code.
# converting tuple to list using the list() function a=(3,5,7,9,10) b= list(a) print(b) print(type(b))
[3, 5, 7, 9, 10] <class 'list'>
List to tuple conversions:
We can convert a list to a tuple by using the function tuple(). Let us get this concept by taking an example code using the tuple() function.
# converting the list to a tuple using the tuple() function a=[2,4,6,8,10] b= tuple(a) print(b) print(type(b))
(2, 4, 6, 8, 10) <class 'tuple'> >
In this article, we learned how to convert data types in python language. We first learned about the data types in Python. And we also discussed the implicit and explicit type conversions. We saw all the codes of integer to float, float to integer, list to a tuple, tuple to list, string to an integer, and integer to string conversions.