In this tutorial, you will be learning about the various different data types in Python and how you can perform implicit and explicit type conversion. Each concept will be explained with the help of relevant code examples.
Every variable you create in Python or any other programming language has a data type, which depends on the kind of data you are storing in that variable.
Table of Contents
You can skip to a specific section of this Python data type conversion tutorial using the table of contents below:
Some of the most well-known data types are int, float, bool and string. These data types store numbers, fractional numbers and alphanumeric values respectively. So, the data type that you select has to be according to the value that you intend on storing in the variable.
Python is a dynamically typed programming language, which gives you the freedom of not having to explicitly declare the data type while creating a variable. The opposite of this is statically typed programming languages like Java.
The following code block will run seamlessly in Python, but if you try to do something similar in Java, it would give a compilation error that the types have not been defined.
If you run this code, you’ll get the following output:
<class 'float'> 10.5
Here, you can see that an int one_int is added into a float one_float and the compiler handles the conversion of data types automatically. This results in the result variable having a type of float.
When data type conversion is taking place during run time or while compilation, similar to above, then this is known as implicit type conversion.
In the code block, the conversion occurred automatically, without having to explicitly mention the data type in the result variable.
Why did the compiler convert to float and not int?
If the compiler had assigned the data type int to result then this would have led to the removal of the fraction part of the number. This would lead to loss of information and, consequently, inaccurate storage of data. Therefore, to maintain the accuracy of information, the compiler chooses a data type that protects the integrity of the data.
Let’s take the example described in the earlier section and perform explicit conversion on the variable result and observe what happens. Keeping the rest of the code unchanged, just update the following line:
Running the complete code will output:
<class 'int'> 10
As you can see, result is now an integer and we have lost the fraction part of 0.5 from the data.
Explicit conversion is also known as type casting and it occurs when you have clearly defined which data type to convert to. Like, in the above example, result is explicitly type casted as an integer.
Primitive and Non-Primitive Data Structures
The basic data types defined earlier in the tutorial are also known as primitive data structures. In Python, there are four primitive data structures, which are:
Lists and tuples in Python are almost identical except that tuples are defined by enclosing elements in parenthesis (2,3,4); whereas, in a list, the values are defined in square brackets [2,3,4]. The other major difference is that tuples are immutable, meaning its values cannot be updated.
Dictionaries store values in key-value pairs, and a set is similar to a list but the values are immutable and are stored unindexed and in an unordered fashion.
This was just a brief overview of the non-primitive data structures, you can learn a lot more about how to use them and where to use them.
Converting Integers to Float and Back
Integers and floats are the primitive data structures in Python that deal with numbers. You can easily convert them into one another by using existing methods in Python.
The int() method was used in the earlier example, let’s see how you can convert from integer to float.
You can use the float() method to convert integer values into a float data type. If you run the code, you’ll get the following output:
<class 'float'> 10.0
This is explicit conversion of integer values into float values. Earlier we transformed a float value into an integer value in a similar fashion.
Type Conversion with Strings
In Python, strings hold alphanumeric values and more than often you will have to convert strings to other data types and vice versa.
Take the following code block for example, where you are printing the total value of a shopping cart.
soap_int=2shampoo_int=5cereal_int=10total=soap_int+shampoo_int+cereal_intprint("Your total is: "+total+"$")
Running this code will yield the following error:
print("Your total is: "+ total + "$")
TypeError: can only concatenate str (not "int") to str
The error occurs because you cannot implicitly concatenate integers into a string. The compiler does not support this behavior. For this code to work, you have to use the str() method that converts a value into a string.
Update the print statement to include the str() method:
soap_int=2shampoo_int=5cereal_int=10total=soap_int+shampoo_int+cereal_intprint("Your total is: "+str(total)+"$")
Now run the code and it will yield the following output:
Your total is: 17$
String to Integer
In the aforementioned example, you converted an integer to a string. Now let’s see how to convert a string that holds numeric values into an integer.
first_num='55'second_num='5'result=first_num+second_numprint("The result is:"+result)
In the code block above, you’re initializing two strings that contain numeric values and adding them up. If you run the code, you’ll get:
The result is: 555
This is because the code does not add up the numeric values, rather concatenates the string values.
To add the numeric values, you have to explicitly convert the string values into integers, like so:
first_num='55'second_num='5'result=int(first_num)+int(second_num)print("The result is:"+str(result))
Now if you run the code, you’ll get the output:
The result is: 60
Since the result is now an integer, you have to convert it back to a string while printing it.
The string to integer conversion only occurs when there are only numeric values in the string. If you try to convert a string containing other characters into an integer, you’ll get an error.
Let’s see this behavior in the following code block:
Run this code and you’ll get the following error:
one_int = int(one_string)
ValueError: invalid literal for int() with base 10: 'ten'
This is because the compiler does not know that ‘ten’ is equal to ‘10’, therefore, do not try to convert string into integers in such a way.
Converting Lists to Tuples
In Python, lists and tuples are similar, but have two major differences. First, lists are defined with square brackets  while tuples use parenthesis (). The other difference is that lists are mutable while tuples are immutable.
Take for example a tuple that stores the names of the user, now suppose you want to update the usernames. You can’t do so directly, if you try to update the value, like in the code ahead, you’ll get an error, shown below.
First, you convert the tuple into a list then change the value at the appropriate index. Finally, you convert the list back to a tuple and print it out. You have now successfully updated the value in the tuple.
('new John', 'jean', 'joe', 'janice')
Python supports a ton of other type conversion methods, you can convert decimal numbers to binary, octal or hexadecimal numbers. These methods make it easier for you to work with data that involves a lot of different types. This tutorial is limited to what has already been discussed and the rest will be covered in a separate post.
Working with Python is fun. The programming language makes it easy for you to work with different data structures. Its inbuilt methods allow for easy conversion and allow you to quickly convert your variables from one type to another.
However, type conversion should be handled carefully as it can lead to unexpected output if the data is not clearly understood. Moreover, it is always a good approach to explicitly declare your variable types, where possible, and have strong exception handling to avoid runaway variables during run time.