In the previous article, we have discussed Python min() Function with Examples
oct() Function in Python:
The oct() function is used to convert an integer to an octal string.
Python prefixes octal strings with 0o.
Syntax:
oct(number)
Parameters
number: This is required. It is an integer number.
If the value is not an integer (binary, decimal, or hexadecimal), it should implement __index__() to return an integer.
Return Value:
The oct() function takes an integer number and returns an octal string.
Examples:
Example1:
Input:
Given Number = 18
Output:
The given number's{ 18 } Octal Value = 0o22
The oct() function Return Type = <class 'str'>Example2:
Input:
Given number = 13 Given binary number = 0b10 Given hexadecimal number = 0XB
Output:
The oct(13) value is = 0o15 The oct(0b10) value = 0o2 The oct(0XB) value = 0o13
oct() Function with Examples in Python
Method #1: Using Built-in Functions (Static Input)
Approach:
- Give the number as static input and store it in a variable.
- Pass the given number as an argument to the oct() function to get the Octal value of a given number.
- Store it in another variable.
- Print the given number’s OctalValue.
- Print the Return Type of oct() Function using the type() method by passing the oct(given number) as an argument to it.
- The Exit of the program.
Below is the implementation:
# Give the number as static input and store it in a variable.
gvn_numbr = 18
# Pass the given number as an argument to the oct() function to get the
# Octal value of a given number.
# Store it in another variable.
Octal_valu = oct(gvn_numbr)
# Print the given number's Octal Value.
print("The given number's{", gvn_numbr,
"} Octal Value = ", Octal_valu)
# Print the return Type of oct() Function using the type() method by passing
# the oct(given number) as an argument to it.
print("The oct() function Return Type =", type(oct(gvn_numbr)))
Output:
The given number's{ 18 } Octal Value = 0o22
The oct() function Return Type = <class 'str'>oct() For binary and Hexadecimal numbers
# converting decimal to octal
print('The oct(13) value is = ', oct(13))
# converting binary to octal
print('The oct(0b10) value = ', oct(0b10))
# converting hexadecimal to octal
print('The oct(0XB) value = ', oct(0XB))
Output:
The oct(13) value is = 0o15 The oct(0b10) value = 0o2 The oct(0XB) value = 0o13
Method #2: Using Built-in Functions (User Input)
Approach:
- Give the number as user input using the int(input()) function and store it in a variable.
- Pass the given number as an argument to the oct() function to get the Octal value of a given number.
- Store it in another variable.
- Print the given number’s OctalValue.
- Print the Return Type of oct() Function using the type() method by passing the oct(given number) as an argument to it.
- The Exit of the program.
Below is the implementation:
# Give the number as user input using the int(input()) function and store it in a variable.
gvn_numbr = int(input("Enter some random number = "))
# Pass the given number as an argument to the oct() function to get the
# Octal value of a given number.
# Store it in another variable.
Octal_valu = oct(gvn_numbr)
# Print the given number's Octal Value.
print("The given number's{", gvn_numbr,
"} Octal Value = ", Octal_valu)
# Print the return Type of oct() Function using the type() method by passing
# the oct(given number) as an argument to it.
print("The oct() function Return Type =", type(oct(gvn_numbr)))
Output:
Enter some random number = 10
The given number's{ 10 } Octal Value = 0o12
The oct() function Return Type = <class 'str'>Fed up with searching various pages for the list of Python Built in Functions? Look at the tutorial linked here and explore all coding samples of built-in functions of python.