oct() function is one of the built-in methods in Python3. The oct() method takes an integer and returns it’s octal representation in a string format.
Syntax : oct(x)
x – Must be an integer number and can be in either binary, decimal or hexadecimal format.
Returns : octal representation of the value.
Errors and Exceptions :
TypeError : Returns TypeError when anything other than integer type constants are passed as parameters.
Code #1 : Illustrates the use of oct() function.
The octal representation of 23 is 0o27 The octal representation of the ascii value of 'z' is 0o172 The octal representation of the binary of 23 is 0o27 The octal representation of the binary of 23 is 0o27
Code #2 : Demonstrate TypeError
Traceback (most recent call last): File "/home/5bf02b72de26687389763e9133669972.py", line 3, in print("The Octal representation of 29.5 is "+oct(29.5)) TypeError: 'float' object cannot be interpreted as an integer
oct() is used in all types of standard conversion. For example, Conversion from decimal to octal, binary to octal, hexadecimal to octal forms respectively.
Code #3 :
a. Hexadecimal to Octal b. Decimal to Octal c. Binary to Octal Enter your choice :- a Enter your input in HEX format :- 0x13 Octal form of 19 is 0o23
a. Hexadecimal to Octal b. Decimal to Octal c. Binary to Octal Enter your choice :- b Enter a number with base-10 format :- 123 Octal form of 123 is 0o173
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.