新聞中心
內(nèi)置函數(shù)oct()用于獲取給定整數(shù)的八進(jìn)制值。此方法接受單個(gè)參數(shù),并返回前綴為“0o”的轉(zhuǎn)換后的八進(jìn)制字符串。

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


咨詢(xún)
建站咨詢(xún)
