美文网首页
Python中10个常用的内置函数

Python中10个常用的内置函数

作者: 小张Python | 来源:发表于2021-02-28 00:30 被阅读0次

    大家好,我是小张

    在 3.8 版本中,Python 解释器共有近 69 个内置函数可供使用,有了它们能极大地提高编码效率,

    image-20210221235544315

    数量虽然不少,但在日常搬砖中只用到其中一部分,根据使用频率和用法,列出来几个本人认为不错的内置函数,结合一些例子介绍给大家

    complex()

    返回一个形如 a+bj 的复数,传入参数分为三种情况:

    • 参数为空时,返回0j

    • 参数为字符串时,将字符串表达式解释为复数形式并返回

    • 参数为两个整数(a,b)时,返回 a+bj

    • 参数只有一个整数 a 时,虚部 b 默认为0,函数返回 a+0j

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n107" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">>>> complex('1+2j')
    (1+2j)

    complex()
    0j
    complex(1,2)
    (1+2j)
    complex(2)
    (2+0j)</pre>

    dir()

    • 不提供参数时,返回当前本地范围内的名称列表

    • 提供一个参数时,返回该对象包含的全部属性

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n114" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">>>> class Test:
    def first(self):
    return 1
    def second(self):
    return 2

    dir(Test)# 提供参数时
    ['class', 'delattr', 'dict', 'dir', 'doc', 'eq', 'format', 'ge', 'getattribute', 'gt', 'hash', 'init', 'init_subclass', 'le', 'lt', 'module', 'ne', 'new', 'reduce', 'reduce_ex', 'repr', 'setattr', 'sizeof', 'str', 'subclasshook', 'weakref', 'first', 'second']
    dir()# 未提供参数时
    ['Test', 'annotations', 'builtins', 'doc', 'loader', 'name', 'package', 'spec']</pre>

    divmod(a,b)

    • a -- 代表被除数,整数或浮点数;

    • b -- 代表除数,整数或浮点数;

    根据 除法运算 计算 a,b 之间的商和余数,函数返回一个元组(p,q) ,p 代表商 a//b ,q 代表余数 a%b

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n122" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">>>> divmod(21,4)
    (5, 1)

    divmod(20.1,3)
    (6.0, 2.1000000000000014)</pre>

    enumerate(iterable,start=0)

    • iterable -- 一个可迭代对象,列表、元组序列等

    • start -- 计数索引值,默认初始为0

    该函数返回枚举对象是个迭代器,利用 next() 方法依次返回元素值,每个元素以元组形式存在,包含一个计数元素(起始为 start )和 iterable 中对应的元素值;

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n130" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">>>> stu = ['xiao','zhang','li','qian']

    enumerate(stu)
    <enumerate object at 0x0000025331773F48>
    list(enumerate(stu))# 默认start = 0
    [(0, 'xiao'), (1, 'zhang'), (2, 'li'), (3, 'qian')]
    list(enumerate(stu,start = 1))# 默认start = 1
    [(1, 'xiao'), (2, 'zhang'), (3, 'li'), (4, 'qian')]</pre>

    eval(expression,globals,locals)

    • expression -- 字符串表达式

    • globals -- 变量作用,全局命名空间,如果提供必须为字典形式;可选参数

    • locals -- 变量作用域,局部命名空间,如果提供可为任何映对象;可选参数

    将字符串表达式解析,返回执行结果

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n140" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">>>> eval("2+2")
    4

    eval("2==2")
    True</pre>

    filter(function,iterable)

    • function -- 提供筛选函数名,返回 true 或 false

    • iterable -- 列表、元组等可迭代对象

    函数返回 iterable 中满足 function 为 True 的元素;假设有一个列表,我们只需要其中的一部分元素,这时就可以提前写一个函数再借助 filter 来进行过滤

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n148" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">>>> num_list = list(range(20))

    num_list
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
    def mod_three(num):
    if(num%3==0):
    return True
    else:
    return False

    filter(mod_three,num_list)
    <filter object at 0x00000253315A6608>
    list(filter(mod_three,num_list))
    [0, 3, 6, 9, 12, 15, 18]</pre>

    isinstance(object,classinfo)

    • object --表示一个类型的对象,若不是此类型, 函数恒返回 False;

    • calssinfo -- 为一个类型元组或单个类型;

    判断对象 object 的类型是否为 classinfo 或 classinfo 中其中一个类型,返回 True 或 False;

    调试Python程序时,障碍之一就是初始变量没有定义类型,所以在代码编写或者调试时,isinstance() 函数常被用来判断变量类型,帮助我们了解程序整个逻辑防止出错

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n157" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">>>> isinstance(num,str)
    False

    isinstance(num,int)


    #### isinstance(object,classinfo)

    * object --表示一个类型的对象,若不是此类型, 函数恒返回 False;

    * calssinfo -- 为一个类型元组或单个类型;

    判断对象 object 的类型是否为 classinfo 或 classinfo 中其中一个类型,返回 True 或 False;

    调试Python程序时,障碍之一就是初始变量没有定义类型,所以在代码编写或者调试时,isinstance() 函数常被用来判断变量类型,帮助我们了解程序整个逻辑防止出错

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n157" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">>>> isinstance(num,str)
    False
    >>> isinstance(num,int)
    True</pre>

    map(function,iterable,...)

    • function -- 执行函数;

    • iterable -- 可迭代对象;

    将 iterable 中的每一个元素放入函数 function 中依次执行,得到与 iteable 元素数量相同的迭代器;

    map() 中可迭代对象可以为多个,其中函数 function 中的参数为所有可迭代对象中并行元素,最终得到的迭代器的数量是以提供元素数量最少可迭代对象为基准,当数量最少的可迭代对象所有元素输入完毕后即执行停止

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n166" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">>>> def fun_a(a,b):
    return a+2*b

    num_list1 = [1,2,3,4]
    num_list2 = [2,3,4,5]
    list(map(fun_a,num_list1,num_list2))

    [5, 8, 11, 14]</pre>

    input()

    函数用于人机交互,读取从输入端输入的一行内容,转化为字符串

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n169" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">>>> s = input('input:')
    input:小张Pyhon

    s
    '小张Pyhon'</pre>

    zip(*iteables)

    • *iterables -- 两个或两个以上可迭代对象

    将所有 iterable 对象中元素按参数顺序以逐元素聚合在一起,最终得到一个迭代器,迭代器中每个元素是个n元素元组,n 表示函数中输入 iterable 参数数量

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n175" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">>>> num_list1 = [1,2,3,4]

    num_list2 = [2,3,1,2]
    list(zip(num_list1,num_list2))
    [(1, 2), (2, 3), (3, 1), (4, 2)]</pre>

    zip() 函数功能等价于

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n177" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">def zip(*iterables):

    zip('ABCD', 'xy') --> Ax By

    sentinel = object()
    iterators = [iter(it) for it in iterables]
    while iterators:
    result = []
    for it in iterators:
    elem = next(it, sentinel)
    if elem is sentinel:
    return
    result.append(elem)
    yield tuple(result)</pre>

    除了上面列举的 10 个之外,还有其它 59 个内置函数,想了解的小伙伴可点击下方原文阅读,对于Python开发者来说掌握这些内置函数非常有用,一方面能提高编码效率,另一方面也能提高代码简洁性;在某一方面功能虽然没有第三方库函数强大,但也不能被小觑。

    好了以上就是本篇文章的全部内容了,最后感谢大家阅读,我们下期见~

    相关文章

      网友评论

          本文标题:Python中10个常用的内置函数

          本文链接:https://www.haomeiwen.com/subject/cjmwfltx.html