Methods and Functions

Python oct()

The Python oct() function in Python is a built-in function. This function takes an integer as its argument and returns its equivalent string format. 

The syntax of the oct() is:

oct(x)

oct() Parameters

The oct python function takes one argument x as the input. 

x- this argument is an integer. The integer can either be in the decimal, hexadecimal, or binary format. 

Python oct()

Source Code

 

Errors:

If the argument provided by the user is something other than the integer data type, then a TypeError exception is raised. 

Return value from oct()

The oct() is used to return the octal representation of the argument that is provided. 

Example 1: How oct() works in Python?

                    

# decimal to octal conversion

print('oct(10) is:', oct(10))



# binary to octal conversion

print('oct(0b101) is:', oct(0b101))



# hexadecimal number to octal conversion

print('oct(0XA) is:', oct(0XA))



Output

oct(10) is: 0o12

oct(0b101) is: 0o5

oct(0XA) is: 0o12

Example 2: oct() for custom objects

                    

class Lady:

    age = 23



    def __index__(self):

        return self.age



    def __int__(self):

        return self.age



lady = Lady()

print('The oct is:', oct(lady))


Output

The oct is: 0o27

Here are some more examples to demonstrate the use of the oct() in python.

Source Code:

                    

#This program demonstrates the use of the oct() in python

print("23 is represented in octal form as " + oct(23))

  

print("This is an octal representation of 'z' in ACSII " + oct(ord('z')))

  

# A number's binary form can be given as a parameter.

  

# 23 in Binary format is 0b10111

print("The binary number 23's octal representation is " + oct(0b10111))

  

# For 23, Hexadecimal is 0x17

print("The hexadecimal number 23's octal representation is " + oct(0x17))



Output :

23 is represented in octal form as 0o27



This is an octal representation of 'z' in ACSII' is 0o172



The binary number 23's octal representation is 0o27



The hexadecimal number 23's octal representation is 0o27

Code #2 : Demonstrate TypeError program in Python

The program given below generates a TypeError because we provide a floating-point number as the input. 

Source code:

                    

print("29.5's octal representation is " + oct(29.5))

  Output :

Traceback (most recent call last):

  File "/home/5bf02b72de26687389763e9133669972.py", line 3, in 

    print("29.5's octal representation is"+oct(29.5))

TypeError: 'float' object cannot be interpreted as an integer

How to convert integers of other formats into octal?

 

                    

print("i. Print Hexadecimal to Octal representation")

print("ii. Print Decimal to Octal representation")

print("iii. Print Binary to Octal representation")

  # The octal representation is generated by this function from its binary form

def bin_to_oct1():

    

    print("Enter an integer in the binary format:-")

      

    # receiving user input as a binary string and then converting it to the appropriate decimal format using int()

    a = int(input(), 2)

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

 

# The octal representation of the hexadecimal form passed as value is generated by this function.

def hex_to_oct1():

    print("Enter an integer in the hexadecimal format:-")

  

    # accepting user input as a 

# hexadecimal string and then 

# converting it to the appropriate 

# decimal format with int()



    a = int(input(), 16)

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

  

  

# The function transforms a decimal value to its octal equivalent.

def decimal_to_oct1():

  

    print("Enter base-10 format no:-")

  

    #converting user input to an integer from a simple user input



    a = int(input())

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

  

  

# Driver Code

ch = input("Enter an option :-\n")

  

if ch is 'a':

    hex_to_oct()

elif ch is 'b':

    decimal_to_oct()

elif ch is 'c':

    bin_to_oct()

Output :

Print Hexadecimal to Octal representation
Print Decimal to Octal representation
iii. Print Binary to Octal representation



Enter an option:-

a



Enter an integer in the hexadecimal format:-

0x13

Octal form of 19 in 0o23

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.