Python oct() Function: Use, Syntax, and Examples

Python oct() function: In this tutorial, we will learn about the oct() function in Python with its use, syntax, parameters, returns type, and examples. By IncludeHelp Last updated : June 23, 2023

Python oct() function

The oct() function is a library function in Python, it is used to get the octal value of a given number, it accepts an integer number and returns its octal value in string format.

Syntax

The following is the syntax of oct() function:

oct(number)

Parameter(s):

The following are the parameter(s):

  • number – an integer number whose value to be converted in an octal number.

Return Value

The return type of oct() function is <class 'str'>, it returns the converted octal value of number in string format.

Python oct() Function: Example 1

# python code to demonstrate example of 
# oct() number

# number
num = 12345
print("octal value of ", num, " is = ", oct(num))

num = 0
print("octal value of ", num, " is = ", oct(num))

num = 2712
print("octal value of ", num, " is = ", oct(num))

Output

octal value of  12345  is =  0o30071
octal value of  0  is =  0o0
octal value of  2712  is =  0o5230

Python oct() Function: Example 2

Python code to print the returns type of oct() function and also test function with wrong input.

# python code to demonstrate example of 
# oct() number

# number 
num = 12345

# printing return type of the function oct()
print("return type is: ", type(oct(num)))

# testing with invalid values
print(oct(10.23))  # returns error because it's a float values

Output

return type is:  <class 'str'>
Traceback (most recent call last):
  File "/home/main.py", line 11, in <module>
    print(oct(10.23))  # returns error because it's a flaot values
TypeError: 'float' object cannot be interpreted as an integer


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.