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.
'0b10111' >>> bin(012)
'0b1010' >>> bin(0x1a)
Example2: Convert a real, positive, negative, octal and hexadecimal to oct.
Example3: Convert a real, positive, negative, octal and hexadecimal to oct.
Related functions: int()
Latest posts by Surendra Anne (see all)
- FREE: JOIN OUR DEVOPS TELEGRAM GROUPS - August 2, 2019
- Review: Whizlabs Practice Tests for AWS Certified Solutions Architect Professional (CSAP) - August 27, 2018
- How to use ohai/chef-shell to get node attributes - July 19, 2018
- wget download a file to a directory in Linux/Unix - June 4, 2018
- GIT: How to compare two GIT branches? - June 3, 2018