当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。