新聞中心
這里有您想知道的互聯(lián)網營銷解決方案
創(chuàng)新互聯(lián)Python教程:python oct()
內置函數(shù)oct()用于獲取給定整數(shù)的八進制值。此方法接受單個參數(shù),并返回前綴為“0o”的轉換后的八進制字符串。

創(chuàng)新互聯(lián)是一家集網站建設,伊吾企業(yè)網站建設,伊吾品牌網站建設,網站定制,伊吾網站建設報價,網絡營銷,網絡優(yōu)化,伊吾網站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學習、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網站。
**oct(x)** #where x must be an integer number and can be binary,decimal or hexadecimal format
oct()參數(shù):
接受單個參數(shù)。如果參數(shù)類型不是整數(shù),此函數(shù)將引發(fā)類型錯誤。
| 參數(shù) | 描述 | 必需/可選 |
|---|---|---|
| 整數(shù) | 可能是二進制、十進制或十六進制 | 需要 |
十月()返回值
如果我們傳遞一個對象作為參數(shù),在這種情況下,該對象必須有__index__()函數(shù)實現(xiàn)才能返回一個整數(shù)。
| 投入 | 返回值 | | 整數(shù) | 八進制字符串 |
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 類實現(xiàn)了__index__()和__index__()。那是因為我們可以在 Person 的對象上使用__index__()。
示例oct()函數(shù)拋出一個錯誤
# 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 本文名稱:創(chuàng)新互聯(lián)Python教程:python oct()
文章分享:http://m.fisionsoft.com.cn/article/dhpcosi.html


咨詢
建站咨詢
