创新互联Python教程:python oct()
内置函数oct()用于获取给定整数的八进制值。此方法接受单个参数,并返回前缀为“0o”的转换后的八进制字符串。

创新互联是一家集网站建设,伊吾企业网站建设,伊吾品牌网站建设,网站定制,伊吾网站建设报价,网络营销,网络优化,伊吾网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。
**oct(x)** #where x must be an integer number and can be binary,decimal or hexadecimal format
oct()参数:
接受单个参数。如果参数类型不是整数,此函数将引发类型错误。
| 参数 | 描述 | 必需/可选 |
|---|---|---|
| 整数 | 可能是二进制、十进制或十六进制 | 需要 |
十月()返回值
如果我们传递一个对象作为参数,在这种情况下,该对象必须有__index__()函数实现才能返回一个整数。
| 投入 | 返回值 | | 整数 | 八进制字符串 |
Python 中oct()方法的示例
示例oct()在 Python 中是如何工作的?
# decimal to octal
print('oct(10) is:', oct(10))
# binary to octal
print('oct(0b101) is:', oct(0b101))
# hexadecimal to octal
print('oct(0XA) is:', oct(0XA))
输出:
oct(10) is: 0o12
oct(0b101) is: 0o5
oct(0XA) is: 0o12示例 2:自定义对象的oct()
class Person:
age = 23
def __index__(self):
return self.age
def __int__(self):
return self.age
pers
print('The oct is:', oct(person))
输出:
The oct is: 0o27这里,Person 类实现了__index__()和__index__()。那是因为我们可以在 Person 的对象上使用__index__()。
示例oct()函数抛出一个错误
# Python `oct()` function example
# Calling function
val = oct(10.25)
# Displaying result
print("Octal value of 10.25:",val)
输出:
TypeError: 'float' object cannot be interpreted as an integer 文章题目:创新互联Python教程:python oct()
本文网址:http://www.jxjierui.cn/article/dhpcosi.html


咨询
建站咨询
