This article introduces the built-in function oct () in python, which is used to convert the decimal number to octal and then to the character. If you have any need, refer to the following English documents:
oct ( x )Convert an integer number to an octal string. The result is a valid Python expression. If x is not a Pythonobject, it has to define anmethod that returns an integer.
Note:
1. the Function converts an integer to an octal string. If a floating point or string is input, an error is returned.
>>> A = oct (10) >>> a '0o12' >>> type (a) # The type of the returned result is a string.
>>> Oct (10.0) # floating point numbers cannot be converted to octal Traceback (most recent call last): File"
", Line 1, in
Oct (10.0) TypeError: 'float' object cannot be interpreted as an integer >>> oct ('10') # The string cannot be converted to an octal Traceback (most recent call last): File"
", Line 1, in
Oct ('10') TypeError: 'str' object cannot be interpreted as an integer
2. if the input parameter is not an integer, it must be an instance object of the class that defines _ index _ and returns the integer function.
# The _ index _ function cannot be converted.
>>> Class Student:
Def _ init _ (self, name, age ):
Self. name = name
Self. age = age
>>> A = Student ('Kim ', 10)
>>> Oct ()
Traceback (most recent call last ):
File" ", Line 1, in
Oct ()
TypeError: 'Student 'object cannot be interpreted as an integer
# The _ index _ function is defined, but the return value is not of the int type and cannot be converted.
>>> Class Student: def _ init _ (self, name, age): self. name = name self. age = age def _ index _ (self): return self. name >>> a = Student ('Kim ', 10) >>> oct (a) Traceback (most recent call last): File"
", Line 1, in
Oct (a) TypeError: _ index _ returned non-int (type str) # defines the _ index _ function, and the return value is of the int type, conversion> class Student: def _ init _ (self, name, age): self. name = name self. age = age def _ index _ (self): return self. age >>>> a = Student ('Kim ', 10) >>> oct (a) '0o12'
The above is a detailed explanation of the Python built-in function OCT. For more articles, please follow the PHP Chinese network (www.php1.cn )!