Professional Documents
Culture Documents
Type Conversion
Type version conversion functions
Int()
Oct()
Hex()
Ord()
Chr()
Float()
String()
Eval()
Int(): Returnswhole number
Int(x, base): This function convert x to an
integer of specified base.
Ques: abc=‘0011’
Conversion of binary to int
a='11'
print(int(a,2))
Output: 3
Oct(): octal(0to7)
Oct() takes an integer and return its octal
representation
A=65
Print(oct(A))
Output: 0O101
Hex(): Hexadecimal(0-15)
Hex(): takes an integer and return its octal
hexadecimal representation
The hexadecimal number system is a type of
number system, that has a base value equal to
16.
Hex()
Print(hex(45))
Output: 0x2d
Encoding
The process of conversion of data from one
form to another is known as encoding.
Example: The key ‘B’ is pressed. It is internally
map to decimal value i.e. 66, which is then
convert into binary language for computer
undersatnding.
ORD()
The ord() function returns the number
representing the unicode code of a specified
character.
Example:
abc = 'b'
print(ord(abc))
Chr()
Python chr() function is used to get a string
representing of a character which points to a
Unicode code integer
Example: x=41
Print(chr(41))
Output: I
Float()
This function is used to convert any datatypeto
float.
X=28
Print(float(x))
Output: 28.0
Str()
The string function is used to convert x to
string representation.
X=‘100’
Print(str(100))
Output:100
Complex()
Comp(real, imaginary): returns real and
imaginary number:
Example: print(complex(100,3))
Output: 100+3j
Eval()
The eval() method parses the expression passed
to this method and runs expression (code)
within the program.
Example:
a=eval(input(“enter first number")) 56
b=eval(input(“enter second number")) 23.2
C=a+b
Print(c)
Output: 79.2
Practice Questions
1) Convert binary to int: 110 and 101
2) Convert into hexadecimal: 149, 78
3) Convert into octal:37,68