python的oct函数_oct()函数以及Python中的示例

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函数

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值