Python | oct () function

Python Methods and Functions

The oct () function is one of the built-in methods in Python3. The oct () method takes an integer and returns its octal representation in string format.

Syntax: oct(x)

Parameters:

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 the oct () function.

# Python3 demo program
# using the oct () function

 

print ( "The octal representation of 23 is " + oct ( 23 ))

 

print ( "The octal representation of the"

"ascii value of' z' is " + oct ( ord ( 'z' )))

  
# Binary representation of a number
# can be passed as a parameter

 
# For 23 binary 0b10111

print ( " The octal representation of the binary "

"of 23 is" + oct ( 0b10111 ))

 
# For 23 hex 0x17

print ( "The octal representation of the binary"

  " of 23 is " + oct ( 0x17 ))

Output:

 The octal representation of 23 is 0o27 The octal representation of the ascii val ue 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: Demonstration of type error

# Python3 program demonstrating TypeError

 

print ( "The Octal representation of 29.5 is" + oct ( 29.5 ))

 
"" "
# Python has nothing like float.oct ()
# to directly convert the constant to float like
# in the eighth erratic representation. Conversion
# the octal floating point value is done manually.
"" "

Output:

 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 

Applications:
oct () is used in all types of standard conversion . For example, Convert from decimal to octal, binary to octal, hexadecimal to octal, respectively. 
Code # 3:

# Decimal and Binary TypeConversions
# their respective octal representations

 
# The options provided user

print ( "a. Hexadecimal to Octal " )

print ( "b. Decimal to Octal" )

print ( "c. Binary to Octal" )

 
# The function generates octal representation
  # from binary from

def bin_to_oct ():

 

print ( "Enter your input in BIN format: -" )

 

# accepting user input as a binary string and

# then using int () to convert it to

# appropriate decimal format

x = int ( input < code class = "plain"> (), 2 )

print ( "Octal form of" + str (x) + " is " + oct (x))

 

 
# The function generates an octal representation
# its hexadecimal form, passed as a value.

def hex_to_oct ():

print ( " Enter your i nput in HEX format: - " )

  

# accepting user input as a hex string and

  # then using int () to convert it to

# appropriate decimal format

x = int ( input (), 16 )

print ( "Octal form of" + str (x) + "is" + oct (x))

 

 
# The function converts decimal to
# corresponding octal representation

def decimal_to_oct ():

 

print ( "Enter a number with base-10 format: -" )

 

# accepting simple user input and

  # convert it to integer

x = int ( input ())

print ( "Octal form of" + str (x) + "is" + oct (x))

 

 
Driver code

ch = input ( " En ter your choice: - " )

  

if ch is 'a' :

hex_to_oct ()

elif ch is 'b' :

  decimal_to_oct ()

elif ch is ' c' :

  bin_to_oct ()

Exit:

 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 




Get Solution for free from DataCamp guru