分享

Python程序员都应该学习掌握的的25个最基本内置函数

 信息科技云课堂 2024-04-12 发布于山东

Python的标准库中有数十个内置函数,还有有数千个第三方库。任何人都不可能记住所有这些东西。大多数Python开发人员只需要大约30个内置函数,但哪30个取决于你实际使用Python做什么。


Python的内置函数中,大概有25个函数是所有Python程序员都应该学习掌握的的最基本内置函数。另一部分函数也非常有用,这要看你一般用Python来干什么,还有一部分函数可能很多Python程序员从来没用过。

>>> #print:打印输出,学习程序设计用到的第一个函数>>> str1="Hello World">>> print(str1)Hello World>>> print(str1,end="!\n")Hello World!>>> print(str1, sep="\n")Hello World
>>> #input函数接受一个标准输入数据,返回为 string 类型。>>> m=input("提示性字符串")提示性字符串123>>> print(m)123
>>> #len:返回对象(字符、列表、元组等)长度或项目个数。>>>str1="zbxx">>> len(str1) # 字符串长度4
>>> #str:转换为字符串>>> version = 3>>> "Python " + str(version)'Python 3'
>>> #int:转换为数字>>> #int:将字符串转换为整数>>> str1="123">>> int(str1)123
>>> #float:转换为浮点数>>> str1="123.456">>> float(str1)123.456>>> n=123>>> float(n)123.0
>>> #list:将可迭代对象创建为一个列表>>> str1="zbxx">>> list(str1)['z', 'b', 'x', 'x']>>> tuple1=( 'Google', 'zbxx.net', 'Taobao')>>> list(tuple1)['Google', 'zbxx.net', 'Taobao']
>>> #tuple:将可迭代对象创建为一个元组>>> n=[5,1,7,9,3]>>> tuple(n)(5, 1, 7, 9, 3)>>> str1="zbxx">>> tuple(str1)('z', 'b', 'x', 'x')
>>> #dict:创建一个字典>>> m= [('a', 1), ('b', 2), ('c', 3)]>>> dict(m){'a': 1, 'b': 2, 'c': 3}
>>> #set:创建一个集合>>> n=[5,1,7,9,3]>>> set(n){1, 3, 5, 7, 9}
>>> #range:创建一个可迭代对象>>> range(3)range(0, 3)>>> m=[n for n in range(3)]>>> print(m)[0, 1, 2]>>> m=[n for n in range(1,5,2)]>>> print(m)[1, 3]
>>> #sum:对序列进行求和计算>>>sum([1,2,3]) 6
>>> #bool:将给定参数转换为布尔类型>>> bool(1)True>>> bool(0)False>>> bool('zbxx')True>>> bool('')False
>>> #enumerate:将一个可遍历的数据对象组合为一个索引序列>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']>>> list(enumerate(seasons))[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> #zip将可迭代的对象对应的元素打包成一个个元组>>> a=[1,2,3]>>> b=[4,5,6]>>> list(c)[(1, 4), (2, 5), (3, 6)]
>>> #reversed:返回一个反转的迭代器>>> numbers=[2, 1, 3, 4, 7]>>> reversed_numbers = reversed(numbers)>>> list(reversed_numbers)[7, 4, 3, 1, 2]
>>> #sorted:对所有可迭代的对象进行排序操作>>> numbers=[8, 2, 13, 3, 1]>>> sorted(numbers)[1, 2, 3, 8, 13]>>> sorted(numbers, reverse=True)[13, 8, 3, 2, 1]
>>> #max:返回给定参数的最大值>>> numbers=[8, 2, 13, 3, 1]>>> max(numbers)13
>>> #min:返回给定参数的最小值>>> numbers=[8, 2, 13, 3, 1]>>> min(numbers)1
>>> #any:用于判断给定的可迭代参数是否全部为False,>>> #则返回False,如果有一个为True,则返回True。>>>any(['a', 'b', 'c', 'd']) # 列表list,元素都不为空或0True>>> any(['a', 'b', '', 'd']) # 列表list,存在一个为空的元素True>>> any([0, '', False]) # 列表list,元素全为0,'',falseFalse>>> any(('a', 'b', 'c', 'd')) # 元组tuple,元素都不为空或0True>>> any(('a', 'b', '', 'd')) # 元组tuple,存在一个为空的元素True>>> any((0, '', False)) # 元组tuple,元素全为0,'',falseFalse>>> any([]) # 空列表False>>> any(()) # 空元组False
>>> #all:用于判断给定的可迭代参数中的所有元素是否都为TRUE,>>> #如果是返回True,否则返回False。>>> all(['a', 'b', 'c', 'd']) # 列表list,元素都不为空或0True>>> all(['a', 'b', '', 'd']) # 列表list,存在一个为空的元素False>>> all([0, 12, 3]) # 列表list,存在一个为0的元素False>>> all(('a', 'b', 'c', 'd')) # 元组tuple,元素都不为空或0True>>> all(('a', 'b', '', 'd')) # 元组tuple,存在一个为空的元素False>>> all((0, 1, 2, 3)) # 元组tuple,存在一个为0的元素False>>> all([]) # 空列表True>>> all(()) # 空元组True
>>> #dir:不带参数时,返回当前范围内的变量、方法和定义的类型列表;>>> #带参数时,返回参数的属性、方法列表。>>>dir() # 获得当前模块的属性列表['__builtins__', '__doc__', '__name__', '__package__', 'arr', 'myslice']>>> dir([ ]) # 查看列表的方法['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
>>> #vars:返回对象的属性和属性值的字典对象。>>>print(vars()){'__builtins__': <module '__builtin__' (built-in)>, '__name__': '__main__', '__doc__': None, '__package__': None}>>> class Zbxx: a=1>>> print(vars(Zbxx)){'__module__': '__main__', 'a': 1, '__dict__': <attribute '__dict__' of 'Zbxx' objects>, '__weakref__': <attribute '__weakref__' of 'Zbxx' objects>, '__doc__': None}
>>> #type:返回对象的类型>>> numbers=[8, 2, 13, 3, 1]>>> type(numbers)<class 'list'>
>>> #help:用于查看函数或模块用途的详细说明。>>> help('str')

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多