Type Conversion
After converting to an integer of base 2 : 34
After converting to an float : 100010.0
78
120
92
25.897
3. ord() : This function is used to convert a character to integer.
4. hex() : This function is to convert integer to hexadecimal string.
5. oct() : This function is to convert integer to octal string.
6. bin() : This function is to convert integer to binary number.
# Python code to show Type conversion
# using ord(), hex(), oct()
# initializing integer
s = '8'
# printing character converting to integer
c = ord(s)
print ("after converting character to integer : ",end="")
print (c)
# printing integer converting to hexadecimal string
c = hex(56)
print ("after converting 56 to hexadecimal string : ",end="")
print (c)
# printing integer converting to octal string
c = oct(56)
print (" after converting 56 to octal string : ",end="")
print (c)
after converting character to integer : 56 after converting 56 to hexadecimal string : 0x38 after converting 56 to octal string : 0o70
Lets see an another example related to this
# Python code to show Type conversion
# using ord(), hex(), oct(), bin()
print('ASCII value of "G" is: ' + str(ord('G')))
print('Hexadecimal value of 254 is: ' + str(hex(256)))
print('Octal value of 62 is: ' + str(oct(54)))
print('Binary value of 56 is: ' + str(bin(68)))
ASCII value of "G" is: 71
Hexadecimal value of 254 is: 0x100
Octal value of 62 is: 0o66
Binary value of 56 is: 0b1000100
7. tuple() : This function is used to convert to a tuple.
8. set() : This function returns the type after converting to set.
9. list() : This function is used to convert any data type to a list type.
# initializing string
s = 'code'
# printing string converting to tuple
c = tuple(s)
print ("After converting string to tuple : ",end="")
print (c)
# printing string converting to set
c = set(s)
print ("After converting string to set : ",end="")
print (c)
# printing string converting to list
c = list(s)
print ("After converting string to list : ",end="")
print (c)
After converting string to tuple : ('c', 'o', 'd', 'e')
After converting string to set : {'d', 'e', 'o', 'c'}
After converting string to list : ['c', 'o', 'd', 'e']
Lets have a look on a one more example
cod_list = [10, 25, 35, 46, 59, 64]
cod_set = {10, 11, 22, 33, 55, 28, 26, 79}
print('From list to tuple: ' + str(tuple(cod_list)))
print('From list to set: ' + str(set(cod_list)))
print('From set to list: ' + str(list(cod_set)))
From list to tuple: (10, 25, 35, 46, 59, 64)
From list to set: {64, 35, 10, 46, 25, 59}
From set to list: [33, 10, 11, 79, 22, 55, 26, 28]
11. str() : Used to convert integer into a string.
12. complex(real,img) : : This function converts real numbers to complex(real,img) number. we can convert two integers (real and imaginary part) to complex numbers.
A simple example to demonstrate this.
#convert to complex number
my_complex = complex(10, 5)
print(my_complex)
(10+5j)
After converting integer to complex number : (1+2j)
After converting integer to string : 1
After converting tuple to dictionary : {'c': 1, 'o': 2, 'd': 3}
#learn python
#python requests
#introduction to data science in python
#free python tutorial
#learn python basics
#python study
#python and ai
1 Comments
thanks for teaching
ReplyDeleteIf you have any doubt, Please let me know.