• 技术文章 >Python技术 >Python基础教程

    Python中的oct() 函数是什么?

    2020-11-13 15:35:33原创3279
    有小伙伴们对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

    没想到吧,就这简简单单三个字母的函数,居然有这么强大的功能,多多掌握一些函数还是非常有益处的,关于这个函数还有无穷的使用技巧,大家先掌握部分,下部分内容跟随项目来学习吧~

    专题推荐:python中的oct函数
    品易云
    上一篇:如何实现python3中的字符串对齐? 下一篇:python以列表为参数的函数有哪些?

    相关文章推荐

    • 如何设置Python字典以值进行排序?• 怎么利用python统计字典中元素出现的次数?• 如何实现python字典循环添加元素?• python数据结构里的集合怎么用?• 如何实现python3中的字符串对齐?

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网