當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Python oct()用法及代碼示例


oct()函數是Python3中的內置方法之一。 oct()方法采用整數,並以字符串格式返回其八進製表示形式。

用法: oct(x)

參數:


x-必須為整數,並且可以為二進製,十進製或十六進製格式。

返回值:值的八進製表示形式。

錯誤和異常:
TypeError:當將整數類型常量以外的任何內容作為參數傳遞時,返回TypeError。


代碼1:說明了oct()函數的用法。

# Python3 program to demonstrate 
# the use of oct() function 
  
print("The octal representation of 23 is " + oct(23)) 
  
print("The octal representation of the"
    " ascii value of 'z' is " + oct(ord('z'))) 
  
# Binary representation of a number 
# can be passed as parameter 
  
# For 23, Binary is 0b10111 
print("The octal representation of the binary"
                " of 23 is " + oct(0b10111)) 
  
# For 23, Hexadecimal is 0x17 
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


代碼2:展示TypeError

# Python3 program demonstrating TypeError 
  
print("The Octal representation of 29.5 is " + oct(29.5)) 
  
''' 
# Python doesn't have anything like float.oct() 
# to directly convert a floating type constant 
# to it's octal representation. Conversion of a 
# floating point value to it's octal is done manually. 
'''

輸出:

Traceback (most recent call last):
  File "/home/5bf02b72de26687389763e9133669972.py", line 3, in 
    print("The Octal representation of 29.5 is "+oct(29.5))
TypeError:'float' object cannot be interpreted as an integer


應用範圍:
oct()用於所有類型的標準轉換。例如,分別從十進製轉換為八進製,從二進製轉換為八進製,從十六進製轉換為八進製。代碼3:

# TypeConversions from decimal and binary 
# to their respective octal representations 
  
# The choices present to the user 
print("a. Hexadecimal to Octal ") 
print("b. Decimal to Octal") 
print("c. Binary to Octal") 
  
# Function generates octal representation  
# from it's binary from 
def bin_to_oct():
      
    print("Enter your input in BIN format:-") 
      
    # taking user input as binary string and  
    # then using int() to convert it into it's 
    # respective decimal format 
    x = int(input(), 2) 
    print("Octal form of " + str(x) + " is " + oct(x) ) 
  
  
# Function generates octal representation 
#  of it's hexadecimal form passed as value. 
def hex_to_oct():
    print("Enter your input in HEX format:-") 
  
    # taking user input as hexadecimal string and  
    # then using int() to convert it into it's 
    # respective decimal format 
    x = int(input(), 16) 
    print("Octal form of " + str(x) + " is " + oct(x)) 
  
  
# Function converts decimal form to it's  
# respective octal representation 
def decimal_to_oct():
  
    print("Enter a number with base-10 format:-") 
  
    # taking a simple user input and  
    # converting it to an integer 
    x = int(input()) 
    print("Octal form of " + str(x) + " is " + oct(x)) 
  
  
# Driver Code 
ch = input("Enter your choice:-\n") 
  
if ch is 'a':
    hex_to_oct() 
elif ch is 'b':
    decimal_to_oct() 
elif ch is 'c':
    bin_to_oct()

輸出:

a. Hexadecimal to Octal 
b. Decimal to Octal
c. Binary to Octal
Enter your choice:-
a
Enter your input in HEX format:-
0x13
Octal form of 19 is 0o23

a. Hexadecimal to Octal 
b. Decimal to Octal
c. Binary to Octal
Enter your choice:-
b
Enter a number with base-10 format:-
123
Octal form of 123 is 0o173


相關用法


注:本文由純淨天空篩選整理自retr0大神的英文原創作品 Python | oct() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。