Today our python built-in function(s) are bin(), oct() and hex() which are useful for converting any type of number like negative, integer and large to their respective base numbers.

Need of bin(), oct() and hex() function in Python:

When we are doing some math work it is require to convert different types of numbers to other number types and do calculations. The bin() function will help us to convert different number bases, types to corresponding binary value and oct() and hex() will convert to octal and hexadecimal numbers.

Python bin(), oct() and hex() function syntax:

	bin(any-number)

	oct(any-number)

	hex(any-number)

From above syntax any-number can be a real, positive, negative, binary, octal, hex etc.

Examples: Let us start with abs() function with some examples and see how we can use them in Python coding.

Example1: Convert a real, positive, negative, octal and hexadecimal to binary.

	>>> bin(23)
'0b10111'
>>> bin(-23)
'-0b10111'
>>> bin(+23)
'0b10111'
>>> bin(012)
'0b1010' >>> bin(0x1a)
'0b11010'

Example2: Convert a real, positive, negative, octal and hexadecimal to oct.

	>>> oct(23)
'027'
>>> oct(+23)
'027'
>>> oct(-23)
'-027'
>>> oct(0b010)
'02'
>>> oct(0x20)
'040'
>>> 

Example3: Convert a real, positive, negative, octal and hexadecimal to oct.

	>>> hex(23)
'0x17'
>>> hex(+23)
'0x17'
>>> hex(-23)
'-0x17'
>>> hex(0b110)
'0x6'
>>> hex(0110)
'0x48'
>>> 

Related functions: int()

Complete python built-in function list.

The following two tabs change content below.
Mr Surendra Anne is from Vijayawada, Andhra Pradesh, India. He is a Linux/Open source supporter who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. He works as Devops Engineer with Taggle systems, an IOT automatic water metering company, Sydney . You can contact him at surendra (@) linuxnix dot com.