Type Conversions in Python :
Type conversion means converting one data type into another data type. python has many functions for typecasting.
There are two types of Type conversion :
- Implicit Conversion
- Explicit Conversion
Implicit Type Conversion :
In Implicit conversion, the python interpreter automatically converts one data type into another without any intervention of the user. Let’s look at an example :
Code:
Python Code
p = 3
print("type of p is : ", type(p))
q = 5.67
print("type of q is : ", type(q))
p = p+q
print(p)
print("type of p is : ", type(p))
Output:
type of p is : <class ‘int’>
type of q is : <class ‘float’>
8.67
type of p is : <class ‘float’>
Initially, the p is of type int, and q is of type float, after updating the p with p+q the data type of p changed to float. In expression p+q as p and q are of different types, type conversion happened and data type of p changed to float.
Explicit Conversion :
In Explicit Conversion user intentionally changes one data type into another data type. Let’s look at the various explicit conversions possible.
int() : This function converts provided data type into int data type. Data types like Sequence Type (List, Tuple), Set, and Dictionary cannot be converted into an int. In the Numeric data type, the float can be converted into an int, but not complex. Number strings can also be converted into int provided the base value. By default, the base is 10.
Code:
Python Code
# converting float into int
x = 3.4555
print("Type of x is : ", type(x))
y = int(x)
print(y)
print("Type of y is : ", type(y))
# converting the string into int with base 10
p = "3455"
print("\nType of p is : ", type(p))
q = int(p)
print(q)
print("Type of q is : ", type(q))
# converting the string into int with base 2
i = "10101"
print("\nType of i is : ", type(i))
j = int(i, 2)
print(j)
print("Type of j is : ", type(j))
Output:
Type of x is : <class ‘float’>
3
Type of y is : <class ‘int’>
Type of p is : <class ‘str’>
3455
Type of q is : <class ‘int’>
Type of i is : <class ‘str’>
21
Type of j is : <class ‘int’>
float(): This function is used to convert int and Number strings int floats. It doesn’t contain a base argument, so all the conversions are done with respective base 10.
Code :
Python Code
# converting int into float
x = 3
print("Type of x is : ", type(x))
y = float(x)
print(y)
print("Type of y is : ", type(y))
# converting the string into float
p = "3455"
print("\nType of p is : ", type(p))
q = float(p)
print(q)
print("Type of q is : ", type(q))
Output:
Type of x is : <class ‘int’>
3.0
Type of y is : <class ‘float’>
Type of p is : <class ‘str’>
3455.0
Type of q is : <class ‘float’>
ord(): This function is used to convert the character into an integer
hex(): This function converts the integer to a hexadecimal string
oct() : This function converts the integer to octal string.
Code :
Python Code
# converting character into integer
a = ord('5')
print("Integer value of character is : ", a)
# converting integer to hexadecimal string
b = hex(99)
print("\nHexadecimal string of 99 is :", b)
# converting integer to octal string
c = oct(45)
print("\nOctalstring of 45 is :", c)
Output:
Integer value of character is : 53
Hexadecimal string of 99 is : 0x63
Octalstring of 45 is : 0o55
Tuple(): This function is used to convert List, String, and Set into Tuple. If you pass the dictionary then all the keys in the dictionary will be part of the tuple. In this way, you can get all the keys in the dictionary.
Code:
Python Code
# converting List into Tuple
a = tuple([1, 2, "tuf", "kohli", 4])
print("After converting List into Tuple : ", a)
# converting Set into Tuple
b = tuple({"tuf", "India", 4.56, 7+8j})
print("\nAfter converting Set into Tuple : ", b)
# converting String into Tuple
c = tuple("India")
print("\nAfter converting String into Tuple : ", c)
#Getting all the keys in Dictonary
d = tuple({"striver": "tuf",
1: "Delhi"})
print("\nAfter converting Dictonary into Tuple : ", d)
Output:
After converting List into Tuple : 1, 2, ‘tuf’, ‘kohli’, 4)
After converting Set into Tuple : 7+8j), ‘India’, 4.56, ‘tuf’)
After converting String into Tuple : ‘I’, ‘n’, ‘d’, ‘i’, ‘a’)
After converting Dictonary into Tuple : ‘striver’, 1)
Set(): This function converts the iterable data types like List, String, and Tuple into Set. If we pass the dictionary we will get all the keys.
Note: Duplicates are omitted while converting into Set. Be careful while conversing.
Code:
Python Code
# converting List into Set
a = set([1, 2, "tuf", "kohli", 4, "tuf", 4, 1, 2])
print("After converting List into Set : ", a)
# converting Tuple into Set
b = set(("tuf", "India", 4.56, 7+8j, 2, 2, 1, 1))
print("\nAfter converting Tuple into Set : ", b)
# converting String into Set
c = set("aaabbbcddedfs")
print("\nAfter converting String into Set : ", c)
# Getting all the keys in Dictonary
d = set({"striver": "tuf",
1: "Delhi"})
print("\nAfter converting Dictonary into Set : ", d)
Output:
After converting List into Set : {1, 2, 4, ‘kohli’, ‘tuf’}
After converting Tuple into Set : {‘India’, 2, 1, 4.56, ‘tuf’, 7+8j)}
After converting String into Set : {‘e’, ‘c’, ‘f’, ‘s’, ‘d’, ‘a’, ‘b’}
After converting Dictonary into Set : {1, ‘striver’}
List(): This function converts the iterable data types like Tuple, String, and Set into List. If we pass the dictionary we will get all the keys.
Code :
Python Code
# converting Set into List
a = list({1, 2, "tuf", "kohli"})
print("After converting Set into List : ", a)
# converting Tuple into List
b = list(("tuf", "India", 4.56, 7+8j, 2, 2, 1, 1))
print("\nAfter converting Tuple into List : ", b)
# converting String into List
c = list("aaabbbcddedfs")
print("\nAfter converting String into List : ", c)
# Getting all the keys in Dictonary
d = list({"striver": "tuf",
1: "Delhi"})
print("\nAfter converting Dictonary into List : ", d)
Output:
After converting Set into List : [1, 2, ‘kohli’, ‘tuf’]
After converting Tuple into List : [‘tuf’, ‘India’, 4.56, 7+8j), 2, 2, 1, 1]
After converting String into List : [‘a’, ‘a’, ‘a’, ‘b’, ‘b’, ‘b’, ‘c’, ‘d’, ‘d’, ‘e’, ‘d’, ‘f’, ‘s’]
After converting Dictonary into List : [‘striver’, 1]
dict(): This function is used to convert a tuple of order (key, value) into a dictionary.
str(): This function is used to convert integer to string.
Complex(real, imaginary): This function is used to convert provided real numbers into complex numbers.
char(number): This converts a number to its corresponding ASCII character.
Code :
Python Code
# converting tuple order (key,value) to dictonary.
tup = (('a', 1), ('b', 2), ('c', 3))
a = dict(tup)
print("After converting tuple order pair to Dictonary : ", a)
b = str(5)
print("\nAfter converting integer to string :", b)
c = complex(4, 5)
print("\nAfter converting real Numbers to complex Number : ", c)
d = chr(105)
print("\nAfter converting Integer to ASCII character : ", d)
Output:
After converting tuple order pair to Dictonary : {‘a’: 1, ‘b’: 2, ‘c’: 3}
After converting integer to string : 5
After converting real Numbers to complex Number: 4+5j)
After converting Integer to ASCII character: i
Special thanks to SaiSri Angajala for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article