python3有多少个内置函数(2023年最新分享)

有没有人在啊,想请说下,python3有多少个内置函数(2023年最新分享)
最新回答
我咋那么萌捏

2024-10-12 09:56:04

导读:本篇文章首席CTO笔记来给大家介绍有关python3有多少个内置函数的相关内容,希望对大家有所帮助,一起来看看吧。

python3--内置函数

python的常用内置函数

1.abs()函数返回数字的绝对值

abs(-40)=40

2.dict()函数用于创建一个字典

dict()

{}???#创建一个空字典类似于u={},字典的存取方式一般为key-value

例如u={"username":"tom",?"age":18}

3.help()函数用于查看函数或模块用途的详细说明

help('math')查看math模块的用处

a=[1,2,3,4]

help(a)查看列表list帮助信息

4.dir()获得当前模块的属性列表

dir(help)

['__call__','__class__','__delattr__','__dict__','__dir__','__doc__','__eq__','__format__','__ge__','__getattribute__','__gt__','__hash__','__init__','__le__','__lt__','__module__','__ne__','__new__','__reduce__','__reduce_ex__','__repr__','__setattr__','__sizeof__','__str__','__subclasshook__','__weakref__']

5.min()方法返回给定参数的最小值/参数可以为序列

a=?min(10,20,30,40)

a

10

6.next()返回迭代器的下一个项目

it=iter([1,2,3,4,5])

next(it)

1

next(it)

2

7.id()函数用于获取对象的内存地址

a=12

id(a)

1550569552

8.enumerate()函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在for循环当中。

a=["tom","marry","leblan"]

list(enumerate(a))

[(0,'tom'),(1,'marry'),(2,'leblan')]

9.oct()函数将一个整数转换成8进制字符串

oct(15)

'0o17'

oct(10)

'0o12'

10.bin()返回一个整数int或者长整数longint的二进制表示

bin(10)

'0b1010'

bin(15)

'0b1111'

11.eval()函数用来执行一个字符串表达式,并返回表达式的值

eval('2+2')

4

12.int()函数用于将一个字符串会数字转换为整型

int(3)

3

int(3.6)

3

int(3.9)

3

int(4.0)

4

13.open()函数用于打开一个文件,创建一个file对象,相关的方法才可以调用它进行读写

f=open('test.txt')

14.str()函数将对象转化为适于人阅读的形式

str(3)

'3'

15.bool()函数用于将给定参数转换为布尔类型,如果没有参数,返回False

bool()

False

bool(1)

True

bool(10)

True

bool(10.0)

True

16.isinstance()函数来判断一个对象是否是一个已知的类型

a=5

isinstance(a,int)

True

isinstance(a,str)

False

17.sum()方法对系列进行求和计算

sum([1,2,3],5)

11

sum([1,2,3])

6

18.super()函数用于调用下一个父类(超类)并返回该父类实例的方法。super是用来解决多重继承问题的,直接用类名调用父类方法

class?User(object):

???def__init__(self):

classPersons(User):

??????super(Persons,self).__init__()

19.float()函数用于将整数和字符串转换成浮点数

float(1)

1.0

float(10)

10.0

20.iter()函数用来生成迭代器

a=[1,2,3,4,5,6]

iter(a)

foriiniter(a):

...????print(i)

...

1

2

3

4

5

6

21.tuple函数将列表转换为元组

a=[1,2,3,4,5,6]

tuple(a)

(1,2,3,4,5,6)

22.len()方法返回对象(字符、列表、元组等)长度或项目个数

s="playbasketball"

len(s)

14

a=[1,2,3,4,5,6]

len(a)

6

23.property()函数的作用是在新式类中返回属性值

classUser(object):

???def__init__(self,name):

???????self.name=name

??defget_name(self):

???????returnself.get_name

??@property

???defname(self):

???????returnself_name

24.type()函数返回对象的类型

25.list()方法用于将元组转换为列表

b=(1,2,3,4,5,6)

list(b)

[1,2,3,4,5,6]

26.range()函数可创建一个整数列表,一般用在for循环中

range(10)

range(0,10)

range(10,20)

range(10,20)

27.getattr()函数用于返回一个对象属性值

classw(object):

...??????s=5

...

a=w()

getattr(a,'s')

5

28.complex()函数用于创建一个复数或者转化一个字符串或数为复数。如果第一个参数为字符串,则不需要指定第二个参数

complex(1,2)

(1+2j)

complex(1)

(1+0j)

complex("1")

(1+0j)

29.max()方法返回给定参数的最大值,参数可以为序列

b=(1,2,3,4,5,6)

max(b)

6

30.round()方法返回浮点数x的四舍五入值

round(10.56)

11

round(10.45)

10

round(10.45,1)

10.4

round(10.56,1)

10.6

round(10.565,2)

10.56

31.delattr函数用于删除属性

classNum(object):

...??a=1

...??b=2

...??c=3.

..print1=Num()

print('a=',print1.a)

a=1

print('b=',print1.b)

b=2

print('c=',print1.c)

c=3

delattr(Num,'b')

print('b=',print1.b)

Traceback(mostrecentcalllast):?File"",line1,inAttributeError:'Num'objecthasnoattribute'b'

32.hash()用于获取取一个对象(字符串或者数值等)的哈希值

hash(2)

2

hash("tom")

-1675102375494872622

33.set()函数创建一个无序不重复元素集,可进行关系测试,删除重复数据,还可以计算交集、差集、并集等。

a=set("tom")

b=set("marrt")

a,b

({'t','m','o'},{'m','t','a','r'})

ab#交集

{'t','m'}

a|b#并集

{'t','m','r','o','a'}

a-b#差集

{'o'}

python有多少内置函数

Python内置函数有很多,为大家推荐5个神仙级的内置函数:

(1)Lambda函数

用于创建匿名函数,即没有名称的函数。它只是一个表达式,函数体比def简单很多。当我们需要创建一个函数来执行单个操作并且可以在一行中编写时,就可以用到匿名函数了。

Lamdba的主体是一个表达式,而不是一个代码块。仅仅能在lambda表达式中封装有限的逻辑进去。

利用Lamdba函数,往往可以将代码简化许多。

(2)Map函数

会将一个函数映射到一个输入列表的所有元素上,比如我们先创建了一个函数来返回一个大写的输入单词,然后将此函数应有到列表colors中的所有元素。

我们还可以使用匿名函数lamdba来配合map函数,这样可以更加精简。

(3)Reduce函数

当需要对一个列表进行一些计算并返回结果时,reduce()是个非常有用的函数。举个例子,当需要计算一个整数列表所有元素的乘积时,即可使用reduce函数实现。

它与函数的最大的区别就是,reduce()里的映射函数(function)接收两个参数,而map接收一个参数。

(4)enumerate函数

用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在for循环当中。

它的两个参数,一个是序列、迭代器或其他支持迭代对象;另一个是下标起始位置,默认情况从0开始,也可以自定义计数器的起始编号。

(5)Zip函数

用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表

当我们使用zip()函数时,如果各个迭代器的元素个数不一致,则返回列表长度与最短的对象相同。

python3有多少内置函数

我刚刚数了下Python3.x一共有153个内置函数

具体如下:

['ArithmeticError','AssertionError','AttributeError','BaseException','BlockingIOError','BrokenPipeError','BufferError','BytesWarning','ChildProcessError','ConnectionAbortedError','ConnectionError','ConnectionRefusedError','ConnectionResetError','DeprecationWarning','EOFError','Ellipsis','EnvironmentError','Exception','False','FileExistsError','FileNotFoundError','FloatingPointError','FutureWarning','GeneratorExit','IOError','ImportError','ImportWarning','IndentationError','IndexError','InterruptedError','IsADirectoryError','KeyError','KeyboardInterrupt','LookupError','MemoryError','ModuleNotFoundError','NameError','None','NotADirectoryError','NotImplemented','NotImplementedError','OSError','OverflowError','PendingDeprecationWarning','PermissionError','ProcessLookupError','RecursionError','ReferenceError','ResourceWarning','RuntimeError','RuntimeWarning','StopAsyncIteration','StopIteration','SyntaxError','SyntaxWarning','SystemError','SystemExit','TabError','TimeoutError','True','TypeError','UnboundLocalError','UnicodeDecodeError','UnicodeEncodeError','UnicodeError','UnicodeTranslateError','UnicodeWarning','UserWarning','ValueError','Warning','WindowsError','ZeroDivisionError','_','__build_class__','__debug__','__doc__','__import__','__loader__','__name__','__package__','__spec__','abs','all','any','ascii','bin','bool','bytearray','bytes','callable','chr','classmethod','compile','complex','copyright','credits','delattr','dict','dir','divmod','enumerate','eval','exec','exit','filter','float','format','frozenset','getattr','globals','hasattr','hash','help','hex','id','input','int','isinstance','issubclass','iter','len','license','list','locals','map','max','memoryview','min','next','object','oct','open','ord','pow','print','property','quit','range','repr','reversed','round','set','setattr','slice','sorted','staticmethod','str','sum','super','tuple','type','vars','zip']

结语:以上就是首席CTO笔记为大家整理的关于python3有多少个内置函数的相关内容解答汇总了,希望对您有所帮助!如果解决了您的问题欢迎分享给更多关注此问题的朋友喔~