Python oct() Function – Tutorial with Examples

Python oct() Function

The Python oct() function is a built-in function that is used to convert an integer number to its equivalent octal string representation. This function takes an integer number as an argument and returns the octal string representation of the integer. The octal string representation of a number is the representation of the number in base 8. In base 8, there are only 8 digits: 0, 1, 2, 3, 4, 5, 6, and 7.

Syntax

oct(x)

Parameters

The oct() function takes only one argument:

  • x: The integer number to be converted to its octal string representation.

Return Value

The oct() function returns a string that represents the octal string representation of the integer number passed as an argument. The returned string starts with ‘0o’ (two zeros and a lowercase ‘o’) and followed by the octal representation of the number.

Examples

Example 1: Converting an integer to its octal representation

num = 10
print(oct(num))

Output:

'0o12'

In this example, we have defined a variable ‘num’ and assigned it the value 10. Then we have used the oct() function to convert the number 10 to its octal representation. The function returns a string ‘0o12’, which is the octal representation of the number 10.

Example 2: Converting a negative integer to its octal representation

num = -10
print(oct(num))

Output:

'-0o12'

In this example, we have defined a variable ‘num’ and assigned it the value -10. Then we have used the oct() function to convert the number -10 to its octal representation. The function returns a string ‘-0o12’, which is the octal representation of the number -10.

Example 3: Converting an octal string representation to integer

octal_num = '0o12'
print(int(octal_num, 8))

Output:

10

In this example, we have defined a variable ‘octal_num’ and assigned it the value ‘0o12’, which is the octal representation of the number 10. Then we have used the int() function to convert the octal representation to an integer. We have passed two arguments to the int() function: the first argument is the octal string representation and the second argument is 8, which represents the base of the number. The function returns the integer value 10, which is the equivalent of the octal representation ‘0o12’.

Use Cases

The oct() function can be used in several scenarios where we need to convert an integer to its octal representation. Some of the use cases are:

  • When we want to represent a number in octal notation for better readability or to make it more compact, we can use the oct() function.
  • When we need to perform bitwise operations on a number, we often need to convert the number to its octal representation first and then perform the operations, as octal notation is more suitable for bitwise operations.
  • When we need to write a program that deals with numbers in different bases, the oct() function can be used to convert numbers from decimal to octal representation.

In conclusion, the oct() function is a useful function that can be used to convert an integer to its octal representation and is widely used in many applications in the field of computer science and mathematics.

Leave a Reply

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