0% found this document useful (0 votes)
21 views17 pages

Type Conversion

The document discusses various type conversion functions in Python including Int(), Oct(), Hex(), Ord(), Chr(), Float(), String(), Eval(), and Complex(). It provides examples of how to use each function to convert between different numeric bases and data types.

Uploaded by

satyamsingh99344
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views17 pages

Type Conversion

The document discusses various type conversion functions in Python including Int(), Oct(), Hex(), Ord(), Chr(), Float(), String(), Eval(), and Complex(). It provides examples of how to use each function to convert between different numeric bases and data types.

Uploaded by

satyamsingh99344
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Type conversion

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

You might also like