Home »
Python
oct() function with example in Python
Python oct() function: Here, we are going to learn about the oct() function in Python with example.
Submitted by IncludeHelp, on April 04, 2019
Python oct() function
oct() function is a library function, 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:
oct(number)
Parameter(s): number – an integer number whose value to be converted in an octal number.
Return value: str – it returns the converted octal value of number in string format.
Example:
Input:
num = 12345
print("octal value of ", num, " is = ", oct(num))
Output:
octal value of 12345 is = 0o30071
Example 1: Python code to convert a number in octal format
# 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
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
TOP Interview Coding Problems/Challenges