Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
communitycreator

What is the oct() function in Python?

Kainat Asif

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The oct() function in Python is used to return the octal representation of an integer. The octal representation starts with 0o.

Syntax

The oct() function is declared as shown in the code snippet below:

oct(x)

Parameters

  • x: The number to be converted into octal representation. The number can be in binary, decimal, or hexadecimal format.

Return value

The oct() function returns the octal representation of the number.

Note: The oct()function retuns TypeError if anything other than integer type constant is passed as parameter.

Code

Consider the code snippet below, which demonstrates the use of the oct() function.

print("oct(0b11001) = " + oct(0b11001)) #binary
print("oct(50) = " + oct(50)) #decimal
print("oct(0x11) = " + oct(0x11)) #hexadecimal

Explanation

  • The oct() function is used in line 1 to convert a binary number to octal.
  • The oct() function is used in line 3 to convert a decimal number to octal.
  • The oct() function is used in line 5 to convert a hexadecimal number to octal.

RELATED TAGS

python
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring