python内置函数

Python013

python内置函数,第1张

python内置函数是什么?一起来看下吧:

python内置函数有:

abs:求数值的绝对值

>>>abs(-2) 2

pmod:返回两个数值的商和余数

>>>pmod(5,2) (2,1) >>pmod(5.5,2) (2.0,1.5)

bool:根据传入的参数的逻辑值创建一个布尔值

>>>bool() #未传入参数 False >>>bool(0) #数值0、空序列等值为False False >>>bool(1) True

all:判断可迭代对象的每个元素是否都为True值

>>>all([1,2]) #列表中每个元素逻辑值均为True,返回True True >>> all(()) #空元组 True >>> all({}) #空字典 True

help:返回对象的帮助信息

>>> help(str)  Help on class str in module builtins: class str(object) |  str(object='') -> str |  str(bytes_or_buffer[, encoding[, errors]]) -> str |   |  Create a new string object from the given object. If encoding or |  errors is specified, then the object must expose a data buffer |  that will be decoded using the given encoding and error handler. |  Otherwise, returns the result of object.__str__() (if defined) |  or repr(object). |  encoding defaults to sys.getdefaultencoding(). |  errors defaults to 'strict'. |   |  Methods defined here: |   |  __add__(self, value, /)           Return self+value.

_import_:动态导入模块

index = __import__('index') index.sayHello()

locals:返回当前作用域内的局部变量和其值组成的字典

>>> def f():     print('before define a ')     print(locals()) #作用域内无变量     a = 1     print('after define a')     print(locals()) #作用域内有一个a变量,值为1 >>> f>>> f() before define a  {}  after define a {'a': 1}

input:读取用户输入值

>>> s = input('please input your name:') please input your name:Ain >>> s 'Ain'

open:使用指定的模式和编码打开文件,返回文件读写对象

# t为文本读写,b为二进制读写 >>> a = open('test.txt','rt') >>> a.read() 'some text' >>> a.close()

eval:执行动态表达式求值

>>> eval('1+2+3+4') 10

除了上述举例的函数之外,内置函数按分类还可分为:

1、数学运算(7个)

2、类型转换(24个)

3、序列操作(8个)

4、对象操作(7个)

5、反射操作(8个)

6、变量操作(2个)

7、交互操作(2个)

8、文件操作(1个)

9、编译操作(4个)

10、装饰器(3个)

abs()divmod()input()open()staticmethod()

all()enumerate()int()ord()str()

any()eval()isinstance()pow()sum()

basestring()execfile()issubclass()print()super()

bin()file()iter()property()tuple()

bool()filter()len()range()type()

bytearray()float()list()raw_input()unichr()

callable()format()locals()reduce()unicode()

chr()frozenset()long()reload()vars()

classmethod()getattr()map()repr()xrange()

cmp()globals()max()reverse()zip()

compile()hasattr()memoryview()round()__import__()

complex()hash()min()set()

delattr()help()next()setattr()

dict()hex()object()slice()

dir()id()oct()sorted()

一、主体不同

1、类:是面向对象程序设计实现信息封装的基础。

2、函数:是指一段在一起的、可以做某一件事儿的程序。也叫做子程序、(OOP中)方法。

二、特点不同

1、类:是一种用户定义的引用数据类型,也称类类型。每个类包含数据说明和一组操作数据或传递消息的函数。类的实例称为对象。

2、函数:分为全局函数、全局静态函数;在类中还可以定义构造函数、析构函数、拷贝构造函数、成员函数、友元函数、运算符重载函数、内联函数等。

三、规则不同

1、类:实质是一种引用数据类型,类似于byte、short、int(char)、long、float、double等基本数据类型,不同的是它是一种复杂的数据类型。

2、函数:函数必须声明后才可以被调用。调用格式为:函数名(实参)调用时函数名后的小括号中的实参必须和声明函数时的函数括号中的形参个数相同。

参考资料来源:百度百科-函数

参考资料来源:百度百科-类