天天看點

python中oct函數_Python中的oct() 函數是什麼?

有小夥伴們對oct函數有了解的嗎?可能有小夥伴們反映說從來沒有見過的吧,更不知道是怎麼要用,我們都知道語言是一個非常連貫的整體,任何一個環節,我們不知道内容,都可能造成下面的内容不會銜接,是以對于生僻的内容,我們一定要學會掌握,或者了解下,知道意思即可,好啦,不多說了,一起來看下吧~

什麼是oct函數?

oct()函數是Python3中的内置方法之一。 oct()方法采用整數,并以字元串格式傳回其八進制表示形式。

oct()文法如下:Syntax : oct(x) #文法

Parameters :   #參數

x – Must be an integer number and can be in either binary, decimal or hexadecimal format.

#  x必須為整數,并且可以為二進制,十進制或十六進制格式。

Returns : octal representation of the value.#傳回值的八進制表示形式。

Errors and Exceptions :   #錯誤和異常:

TypeError : Returns TypeError when anything other than integer type constants are passed as parameters.

#TypeError:當将整數類型常量以外的任何内容作為參數傳遞時,傳回TypeError。

oct()函數的用法print("The octal representation of 23 is " + oct(23))

print("The octal representation of the"

" ascii value of 'z' is " + oct(ord('z')))

print("The octal representation of the binary"

" of 23 is " + oct(0b10111))

print("The octal representation of the binary"

" of 23 is " + oct(0x17))

輸出:The octal representation of 23 is 0o27

The octal representation of the ascii value of 'z' is 0o172

The octal representation of the binary of 23 is 0o27

The octal representation of the binary of 23 is 0o27

沒想到吧,就這簡簡單單三個字母的函數,居然有這麼強大的功能,多多掌握一些函數還是非常有益處的,關于這個函數還有無窮的使用技巧,大家先掌握部分,下部分内容跟随項目來學習吧~