python的oct函数
Python oct()函数 (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.
oct()函数是一个库函数,用于获取给定数字的八进制值,它接受一个整数并以字符串格式返回其八进制值。
Syntax:
句法:
oct(number)
Parameter(s): number – an integer number whose value to be converted in an octal number.
参数: number –一个整数,其值将转换为八进制数。
Return value: str – it returns the converted octal value of number in string format.
返回值: str –以字符串格式返回转换后的数字八进制值。
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
示例1:Python代码以八进制格式转换数字
# 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
示例2:Python代码来打印oct()函数的返回类型并使用错误的输入测试函数
# 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
翻译自: https://www.includehelp.com/python/oct-function-with-example.aspx
python的oct函数