Python Convert Data Types

Table of Contents

We are going to be looking at:

type casting
converting data types
type conversion

we use the type() methd to see what type something is.

Here we can see the type of “11.5” is a float.

				
					type(11.5)
				
			

Implicit: Automatically handled by Python (no manual conversion needed)
Explicit: Manually converting one data type to another

Example 1 - Addition

				
					num_int = 123  # integer
num_float = 1.23  # float
				
			

Here, the result would be a float.

When you add an int and a float, Python automatically converts the int to a float.(implicit type conversion).

Python does this to avoid losing decimal precision.

				
					result = num_int + num_float  # result will be float
				
			
				
					print("Result:", result)
				
			

Example 2 - Division

				
					num1 = 7
num2 = 11
				
			

Here also the result would be a float.

				
					result_division = num1/num2  # result will be float
				
			
				
					print("Result:", result_division)
				
			

Lets look at the Explicit.

Example 3 - Converting string to integer

				
					num_str = "123"
				
			

Here we convert the num_str from a string to an interger using the int() method

				
					num_int = int(num_str)
				
			
				
					print("Integer:", num_int)
				
			

Example 4 String to Float

Here we are converting a string “num_str” to a float using the float() methond

				
					# Converting string to float
num_str = "123.45"
num_float = float(num_str)
print("Float:", num_float)
				
			

Example 5 Int to String

Here we convert interger  “num_int” to string using the str() method

				
					# Converting integer to string
num_int = 123
num_str = str(num_int)
print("String:", num_str)
				
			

Example 6 - hexadecimal, octal , Complex

Here we convert an int “num_int” to hex() which is a hexadecimal (base-16), 

we also convert the int “num_int” to oct()  which is an octal(base-8) and complex() which is a complex number

 

				
					num_int = 123
hexadecimal = hex(num_int)
octal = oct(num_int)
complex = complex(num_int)
				
			

Example 7 List to Tuple

				
					my_list = [1, 2, 3, 4, 5]
				
			

Here we convert a list “my_list” to a tuple. 

				
					my_tuple = tuple(my_list)
				
			

Example 8 List to set

				
					my_set = set(my_list)
				
			

Example 9 List to Dictionary

				
					my_list_pairs = [(1, 'a'), (2, 'b'), (3, 'c')]
				
			

Here we convert a list of key-value pairs(tuples) into a dictionary, where each first element becomes a key and the second element becomes it’s corresponding value.

				
					my_dict = dict(my_list_pairs)
				
			

Example 10 Converting Tuple to List

				
					my_tuple = (1, 2, 3, 4, 5)
				
			

Here we convert a tuple to a list.

using the list() method.

				
					my_list = list(my_tuple)

				
			

Example 11 Converting Tuple to Set

We use the set() method to convert a tuple “my_tuple” to a set.

				
					my_set = set(my_tuple)

				
			

Example 12 Converting Tuple to Dictionary

				
					my_tuple_pairs = ((1, 'a'), (2, 'b'), (3, 'c'))
				
			

Here we cinvert tuple “my_tuple_pairs” to a dictionary using the dict(0 method.

				
					my_dict = dict(my_tuple_pairs)

				
			

Example 13 Converting Set to List

				
					my_set = {1, 2, 3, 4, 5}
				
			

We convert a set “my_set” to a list using the list() method

				
					my_list = list(my_set)

				
			

Example 14 Converting Set to Tuple

We convert a set “my_set” to a tuple using the tuple() method.

				
					my_tuple = tuple(my_set)
				
			

Example 15 Converting Set to Dictionary

				
					my_set_pairs = {(1, 'a'), (2, 'b'), (3, 'c')}
				
			

Here we convert a set “my_set_pairs” to a dictionary using the dict() method.

				
					my_dict = dict(my_set_pairs)

				
			

Example 16 Converting Dictionary to List

				
					my_dict = {1: 'a', 2: 'b', 3: 'c'}
				
			

Here we convert a dictionary “my_dict” to a list using the list() method. we use the .items() to grab the items in the dict

				
					my_list = list(my_dict.items())
				
			

Example 17 Converting Dictionary to Tuple

Here we convert the dictionary to a tuple using the tuple() method.

				
					my_tuple = tuple(my_dict.items())
				
			

Example 18 Converting Dictionary to Set

				
					my_set = set(my_dict.items())
				
			

Example 19 string to list

				
					team = 'Rays'
				
			

Here we convert a string “team” to a list using the list() method.

				
					list(team)
				
			

Example 20 string to set

Here we convert the string “team” to a set using the set() method.

				
					set(team)
				
			

Example 21 string to tuple

Here we convert the string “team” to a tuple using the tuple() method

				
					team_tuple = tuple(team)
				
			
				
					team_tuple
				
			

Free Community

Join 1,000+ AI Automation Builders

Weekly tutorials, live calls & direct access to Ryan & Matt.

Join Free →

Keep Learning

Streamlit Async

Streamlit runs Python scripts top-to-bottom when ever a user interacts with widget.Streamlit is synchronous by default, meaning each function waits for the...

Streamlit Caching

Streamlit runs your script from top to bottom whenever you interact with the app.This execution model makes development super easy. But it...

Streamlit Tutorial

Streamlit can help businesses automate a ton of tasks in a short amount of time. It essentially is a quick UI you...

Gradient boosting classifier

Gradient Boosting is an ensemble technique that builds a strong model by combining multiple weak decision trees. While it may seem similar...