Course

Every value in Python has a data type. Data types are a classification of data that tells the compiler or the interpreter how you want to use the data. The type defines the operations that can be done on the data and the structure in which you want the data to be stored. In data science, you will often need to change the type of your data to make it easier to use and work with.

Python has many data types. You must have already seen and worked with some of them. You have integers and float to deal with numerical values, boolean (`bool`

) to deal with true/false values and strings to work with alphanumeric characters. You can use lists, tuples, dictionaries, and sets, which are data structures where you can store a collection of values. To learn more about them, check out DataCamp's Data Types for Data Science Course.

To easily run all the example code in this tutorial yourself, you can create a DataLab workbook for free that has Python pre-installed and contains all code samples. For more practice on Python type conversion, check out this hands-on DataCamp exercise.

## Python Implicit and Explicit Data Type Conversion

Data conversion in Python can happen in two ways: either you explicitly tell the compiler to convert a data type to some other type, or the compiler understands this by itself and does it for you. In the former case, you're performing an explicit data type conversion, whereas, in the latter, you're doing an implicit data type conversion.

### Python implicit data type conversion

Implicit conversion or coercion is when data type conversion takes place either during compilation or during run time and is handled directly by Python for you. Let's see an example:

```
a_int = 1
b_float = 1.0
c_sum = a_int + b_float
print(c_sum)
print(type(c_sum))
```

```
2.0
<class 'float'>
```

**Tip**: You can use the `type()`

function in Python to check an object's data type.

## Learn Python From Scratch

In the example, an integer value `a_int`

was added to a float value `b_float`

, and the result was automatically converted to a float value `c_sum`

without you having to tell the compiler. This is the implicit data conversion.

Why was the float value not converted to an integer instead?

This is due to a broader concept of type promotion in computer science. Simply put, this is a defense mechanism of the compiler that allows you to perform operations whenever possible by converting your data into a different supertype without losing information.

That means that the conversion from float to integer is not done because then the compiler would need to remove the fractional part, leading to a loss of information.

### Python explicit data type conversion

Explicit conversion, also known as type casting*,* is when data type conversion takes place because you clearly defined it in your program. You basically force an expression to be of a specific type. The general form of an explicit data type conversion is as follows:

`(required_data_type)(expression)`

**Note**: as you can imagine, with explicit data type conversion, there is a risk of information loss since you're forcing an expression to be of a specific type.

With all of this in mind, let's dig into some of the commonly used explicit data type conversions.

## Python Primitive Versus Non-Primitive Data Structures

Primitive data structures are the building blocks for data manipulation and contain pure, simple data values. Python has four primitive variable types:

- Integers
- Float
- Strings
- Boolean

Non-primitive data structures don't just store a value but rather a collection of values in various formats. In Python, you have the following non-primitive data structures:

- Lists
- Tuples
- Dictionaries
- Sets

You can learn more about them with DataCamp's Data Structures in Python Tutorial.

## Primitive Data Structures Conversions

### Python integer and float conversions

Integers and floats are data types that deal with numbers.

To convert an integer to a float, use the `float()`

function in Python. Similarly, if you want to convert a float to an integer, you can use the `int()`

function.

```
a_int = 3
b_int = 2
# Explicit type conversion from int to float
c_float_sum = float(a_int + b_int)
print(c_float_sum)
```

`5.0`

```
a_float = 3.3
b_float = 2.0
```

```
# Explicit type conversion from float to int
c_int_sum = int(a_float + b_float)
print(c_int_sum)
c_float_sum = a_float + b_float
print(c_float_sum)
```

```
5
5.3
```

### Python real to complex data type conversion

You can convert integers to complex numbers by using `complex(real,imag)`

. It requires two integers (real and imaginary numbers) and converts real numbers to complex numbers.

```
real = 2
imag = 5
print(complex(real, imag))
```

`(2+5j)`

### Python data type conversion with strings

A string is a collection of one or more characters (letters, numbers, symbols). You may need to convert strings to numbers or numbers to strings fairly often.

```
price_cake = 15
price_cookie = 6
total = price_cake + price_cookie
print("The total is: " + total + "$")
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-12-54bd76b9b4bd> in <module>()
2 price_cookie = 6
3 total = price_cake + price_cookie
----> 4 print("The total is: " + total + "$")
TypeError: Can't convert 'int' object to str implicitly
```

The example above gives a `TypeError`

, informing that the compiler cannot implicitly convert an integer value to a string.

What the program should do here might seem intuitive to you. However, the compiler might not always be sure, so it provides a mechanism with explicit type casting so that you can clearly state what you want. This is how you can convert an integer to a string in Python using the `str()`

function:

```
price_cake = 15
price_cookie = 6
total = price_cake + price_cookie
print("The total is: " + str(total) + "$")
```

`The total is: 21$`

It works the same way when you convert float to string values.

In Python, you can also convert strings to integer and float values whenever possible. Let's see what this means:

```
price_cake = '15'
price_cookie = '6'
# String concatenation
total = price_cake + price_cookie
print("The total is: " + total + "$")
# Explicit type conversion to integer
total = int(price_cake) + int(price_cookie)
print("The total is: " + str(total) + "$")
```

```
The total is: 156$
The total is: 21$
```

Let's break down the code.

`price_cake`

and `price_cookie`

are initially strings. Then, you need to find the total, which means they have to be converted to their corresponding integer values first. Otherwise, the compiler will assume the operation that you want is a string concatenation rather than a numerical addition. You then need to put this value into the final display string and consequently need to convert the total to a string to concatenate it with the rest of the display message.

Hopefully, this example helps you to see the importance of data type conversions. Even though this is a very small example of data type conversion, you can already see how useful it can be.

**Note**: did you notice the "whenever possible" when converting a string to integers or float? This is because it is not always possible to convert strings to numbers and apply numerical operations to them. The compiler is aware of this and will, therefore, give you an error when you try to do so. Check out the example below:

```
price_cake = 'fifteen'
price_cookie = 'six'
total = int(price_cake) + int(price_cookie)
```

```
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-25-80591b8accda> in <module>()
1 price_cake = 'fifteen'
2 price_cookie = 'six'
----> 3 total = int(price_cake) + int(price_cookie)
ValueError: invalid literal for int() with base 10: 'fifteen'
```

## Summary: Python Primitive Data Type Conversions

Conversion | Function | Example |
---|---|---|

Integer to Float | `float()` |
`float(3) # Output: 3.0` |

Float to Integer | `int()` |
`int(3.9) # Output: 3` |

String to Integer | `int()` |
`int('123') # Output: 123` |

Integer to String | `str()` |
`str(123) # Output: '123'` |

## Non-Primitive Data Structures Conversions

### Python type conversion to tuples and lists

Like with integers and floats, you can also convert lists to tuples and tuples to lists.

Remember what tuples and lists are? Lists and Tuples in Python are used to store a collection of homogeneous items. The difference between tuples and lists is that tuples are immutable, which means that once defined, you cannot delete, add, or edit any values inside them.

Why would you convert lists to tuples?

That's because tuples are immutable data types and allow substantial optimization to the programs that you create.

And why would you convert tuples to lists?

Maybe you want to make changes to the initial tuple. Thus, you can convert them to lists, make the change, and then convert them back to tuples.

In Python, you can use the `tuple()`

function to return a tuple version of the value passed to it, and similarly the `list()`

function to convert a tuple to a list:

```
a_tuple = ('a', 1) ,('f', 2), ('g', 3)
b_list = [1,2,3,4,5]
print(tuple(b_list))
print(list(a_tuple))
```

```
(1, 2, 3, 4, 5)
[('a', 1), ('f', 2), ('g', 3)]
```

Similarly, here's how to convert a string into a list or a tuple in Python:

```
dessert = 'Cake'
# Convert the characters in a string to individual items in a tuple
print(tuple(dessert))
# Convert a string into a list
dessert_list = list(dessert)
dessert_list.append('s')
print(dessert_list)
```

```
('C', 'a', 'k', 'e')
['C', 'a', 'k', 'e', 's']
```

### Python type conversion to dictionaries and sets

You can use the `dict()`

function to convert a tuple to a dictionary and `set()`

to convert a list to a set.

```
print(dict(a_tuple))
print(set(b_list))
```

```
{'a': 1, 'f': 2, 'g': 3}
{1, 2, 3, 4, 5}
```

Simpler to other conversion functions, you just need to provide a function name to convert any datatype to dictionaries and sets.

## Summary: Python Non-Primitive Data Structures Conversions

Conversion | Function | Example | Output |
---|---|---|---|

List to Tuple | `tuple()` |
`tuple([1, 2, 3])` |
`(1, 2, 3)` |

Tuple to List | `list()` |
`list(('a', 1))` |
`['a', 1]` |

String to List | `list()` |
`list('Cake')` |
`['C', 'a', 'k', 'e']` |

String to Tuple | `tuple()` |
`tuple('Cake')` |
`('C', 'a', 'k', 'e')` |

Tuple to Dictionary | `dict()` |
`dict((('a', 1), ('b', 2)))` |
`{'a': 1, 'b': 2}` |

List to Set | `set()` |
`set([1, 2, 3, 4, 5])` |
`{1, 2, 3, 4, 5}` |

## Unicode, Binary, Octal, and Hexadecimal Integers in Python

The number systems refer to the number of symbols or characters representing any numerical value. The number system that you typically use every day is called decimal. In the decimal system, you use ten different symbols: 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9. With these ten symbols, you can represent any quantity. Unicode, Binary, Hexadecimal, and Octal refer to different number systems.

You go to the next digit placement when you run out of symbols. In the decimal system, to represent one higher than 9, you use 10, which means one unit of ten and zero units of one. However, it is different in other number systems. For example, when you consider a binary system that only uses two symbols, 0 and 1, you need to go to the next digit placement when you run out of symbols. So this is how you will count in binary: 0, 1, 10, 11, 100, 101, etc.

Let's check out some of the number systems in more detail in the next sections.

### Convert binary to decimal in Python

Binary integers are the numbers represented with base two. This means in the binary number system, there are only two symbols used to represent numbers: 0 and 1. When you count up from zero in binary, you run out of symbols more quickly: 0, 1, ???

Furthermore, there are no more symbols left. You do not go to the digit 2 because 2 doesn't exist in binary. Instead, you use a special combination of 1s and 0s. In a binary system, 1000 is equal to 8 in decimal. In binary, you use powers of two, which means 8 is basically: (1(2^3)) + (0(2^2)) + (0(2^1)) + (0(2^0))= 8. The position of the 1 and 0 defines the power to which 2 is to be raised to.

Let's see this with a more complex example to make it clear:

```
Binary Number = 1001111
Decimal value = (1*(2^6)) + (0*(2^5)) + (0*(2^4)) + (1*(2^3)) + (1*(2^2)) + (1*(2^1)) + (1*(2^0))
= 79
```

In Python, you can simply use the `bin()`

function to convert from a decimal value to its corresponding binary value.

Similarly, the `int()`

function to convert a binary to its decimal value. The `int()`

function takes as a second argument the base of the number to be converted, which is 2 in the case of binary numbers.

```
a = 79
# Base 2(binary)
bin_a = bin(a)
print(bin_a)
print(int(bin_a, 2)) #Base 2(binary)
```

```
0b1001111
79
```

### Convert octal to decimal in Python

Octal is another number system with fewer symbols than the conventional decimal system. It is base eight, which means that eight symbols are used to represent all the quantities. They are 0, 1, 2, 3, 4, 5, 6, and 7. After 7 is 10 since 8 doesn't exist.

Just like you used powers of two in binary to determine the value of a number, you will use powers of 8 since this is base eight.

In a binary system, 10 is equal to 8 in octal. Let's break it down: (1(*8^1*)) + (*0*(8^0))= 8.

A more complex example:

```
Octal Number = 117
Decimal value = (1*(8^2)) + (1*(8^1)) + (7*(8^0))
= 79
```

In Python, you can use the `oct()`

function to convert from a decimal value to its corresponding octal value. Alternatively, you can also use the `int()`

function and the correct base, which is 8 for the octal number system.

```
a = 79
# Base 8(octal)
oct_a = oct(a)
print(oct_a)
print(int(oct_a, 8))
```

```
0o117
79
```

### Convert hexadecimal to decimal in Python

Hexadecimal is a base 16-number system. Unlike binary and octal, it uses six additional symbols beyond the numbers found in the decimal number system.

But what comes after 9?

Once additional symbols beyond the normal numerical values are needed, letters will be used. So in hexadecimal, the total list of symbols used is 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F.

Using the same example as earlier:

```
Hexadecimal Number = 4F
Decimal value = (4*(16^1)) + (F*(16^0))
= 79
```

In Python, you can use the `hex()`

function to convert from a decimal value to its corresponding hexadecimal value, or the `int()`

function with base 16 for the hexadecimal number system.

```
a = 79
# Base 16(hexadecimal)
hex_a = hex(a)
print(hex_a)
print(int(hex_a, 16))
```

```
0x4f
79
```

### Convert Unicode to character

Python `chr()`

converts unicode integers to a string. Unicode is a universally accepted character coding for displaying the written texts of diverse languages.

We have provided various Unicode integers to the `chr()`

function to display “DATACAMP.” You can also use `ord()`

to convert a single character to a Unicode integer.

```
print(
chr(68),
chr(65),
chr(84),
chr(65),
chr(67),
chr(65),
chr(77),
chr(80),
)
```

`D A T A C A M P`

## Summary: Python Number Systems Conversions

Conversion | Function | Example | Output |
---|---|---|---|

Decimal to Binary | `bin()` |
`bin(79)` |
`0b1001111` |

Binary to Decimal | `int()` |
`int('1001111', 2)` |
`79` |

Decimal to Octal | `oct()` |
`oct(79)` |
`0o117` |

Octal to Decimal | `int()` |
`int('117', 8)` |
`79` |

Decimal to Hex | `hex()` |
`hex(79)` |
`0x4f` |

Hex to Decimal | `int()` |
`int('4F', 16)` |
`79` |

Unicode to Character | `chr()` |
`chr(68)` |
`'D'` |

Character to Unicode | `ord()` |
`ord('D')` |
`68` |

## You Made It!

Congrats! You learned about data type conversions in Python, primarily using built-in methods. This will definitely help you work around various data types, providing you with more flexibility when writing your programs.

We have an excellent course on Intermediate Python for Data Science where you can learn how to plot your data using matplotlib, and work with dictionaries and the famous pandas DataFrame. You will also see how you can control the flow of your program with loops. There is also a case study at the end of the course where you can apply all that you have learned and put your knowledge to work!

## Get certified in your dream Data Scientist role

Our certification programs help you stand out and prove your skills are job-ready to potential employers.

## FAQs

### How do you convert a list of integers to a single string in Python?

You can use the `join()`

method combined with `map()`

to convert each integer to a string and concatenate them.

```
int_list = [1, 2, 3, 4]
result = ''.join(map(str, int_list))
print(result) # Output: '1234'
```

### What happens if you try to convert a non-numeric string to an integer in Python?

A `ValueError`

will be raised indicating that the string is not a valid integer.

```
int('abc') # Raises ValueError: invalid literal for int() with base 10: 'abc'
```

### Can you convert a set to a list in Python? How?

Yes, you can convert a set to a list using the `list()`

function.

```
a_set = {1, 2, 3}
a_list = list(a_set)
print(a_list) # Output: [1, 2, 3]
```

### How do you convert a string of space-separated numbers into a list of integers?

Use the `split()`

method to split the string and `map()`

to convert each part to an integer.

```
numbers = "1 2 3 4"
int_list = list(map(int, numbers.split()))
print(int_list) # Output: [1, 2, 3, 4]
```

### What function can you use to check the data type of a variable in Python?

You can use the `type()`

function to check the data type of a variable.

```
x = 5
print(type(x)) # Output: <class 'int'>
```

I have worked in various industry and have worn multiple hats: software developer, machine learning researcher, data scientist, product manager. But at the core of it all, I am a programmer who loves to learn and share the knowledge!

Learn more about Python with these courses!

Course

### Introduction to Python for Developers

Course

### Intermediate Python

tutorial

### Python Data Structures Tutorial

tutorial

### Type Checking in Python Tutorial

Olivia Smith

7 min

tutorial

### Python String Tutorial

tutorial

### Operators in Python

tutorial

### Python Data Classes: A Comprehensive Tutorial

tutorial