Python内置函数是Python解释器自带的一些函数,无需导入任何模块即可直接使用,这些函数可以帮助我们完成一些常见的操作,提高编程效率,以下是一些常用的Python内置函数及其功能:
1、数学运算函数
函数名 | 功能 |
abs(x) | 返回x的绝对值 |
cmp(x, y) | 比较x和y的大小,返回1、0或1 |
divmod(x, y) | 返回x除以y的商和余数 |
float(x) | 将x转换为浮点数 |
hex(x) | 将x转换为十六进制字符串 |
int(x) | 将x转换为整数 |
len(x) | 返回x的长度(元素个数) |
max(x1, x2, …) | 返回x1、x2等中的最大值 |
min(x1, x2, …) | 返回x1、x2等中的最小值 |
pow(x, y) | 返回x的y次幂 |
round(x[, n]) | 对x进行四舍五入,n表示保留的小数位数,默认为0 |
str(x) | 将x转换为字符串 |
sum(x1, x2, …) | 返回x1、x2等的总和 |
oct(x) | 将x转换为八进制字符串 |
2、类型转换函数
函数名 | 功能 |
bool([x]) | 将x转换为布尔值,非零值为True,零值为False;无参数时,返回当前对象的布尔值 |
bytes([int]) | 将int转换为字节对象 |
complex([real[, imag]]) | 创建复数对象,real为实部,imag为虚部;无参数时,返回当前对象的复数形式 |
dict([seq]) | 创建字典对象,seq为键值对序列;无参数时,返回当前对象的字典形式 |
float([x]) | 将x转换为浮点数;无参数时,返回当前对象的浮点数形式 |
int([x]) | 将x转换为整数;无参数时,返回当前对象的整数形式 |
list([seq]) | 创建列表对象,seq为元素序列;无参数时,返回当前对象的列表形式 |
ord(c) | 返回字符c的Unicode编码 |
str([object]) | 将object转换为字符串;无参数时,返回当前对象的字符串形式 |
frozenset([seq]) | 创建不可变集合对象,seq为元素序列;无参数时,返回当前对象的不可变集合形式 |
set([seq]) | 创建集合对象,seq为元素序列;无参数时,返回当前对象的集合形式 |
type([object], /) | 返回object的类型;无参数时,返回当前对象的类型 |
3、序列操作函数
函数名 | 功能 |
list(seq) | 将seq转换为列表对象;无参数时,返回空列表对象 |
len(s) | 返回序列s的长度(元素个数) |
max(s[, key]) | 返回序列s中的最大值;key为可选参数,用于指定比较元素的方式,默认为None,表示按元素本身比较;如果指定了key函数,则按照key函数的结果进行比较 |
min(s[, key]) | 返回序列s中的最小值;key为可选参数,用于指定比较元素的方式,默认为None,表示按元素本身比较;如果指定了key函数,则按照key函数的结果进行比较 |
reversed(s) | 返回一个反向的迭代器,用于遍历序列s的元素,顺序与原序列相反 |
sorted(iterable, key=None, reverse=False) | 返回一个排序后的列表对象,iterable为可迭代对象;key为可选参数,用于指定比较元素的方式,默认为None,表示按元素本身比较;reverse为可选参数,表示是否降序排列,默认为False(升序排列) |
zip(*iterables) | 返回一个zip对象,用于同时遍历多个可迭代对象的元素;iterables为可迭代对象序列,如zip(a, b, c)等价于zip(a, b) + zip(b, c) |
4、文件操作函数
函数名 | 功能 |
open(file, mode=’r’, buffering=1, encoding=None, errors=None, newline=None, closefd=True, opener=None) | 打开文件并返回文件对象;file为文件路径;mode为打开模式,默认为’r’(只读);buffering为缓冲区大小,默认为1(系统自动选择);encoding为文件编码方式,默认为None;errors为错误处理方式,默认为None;newline为换行符处理方式,默认为None;closefd为是否关闭文件描述符,默认为True;opener为自定义的文件打开方式,默认为None(使用系统默认方式) |
| read([size]) > str or bytes or bytearray or NoneType or int or long or float or bool or NoneType or list or dict or set or object or NoneType or any object instance (depending on the file object and mode) or byteslike object or bytearraylike object or strlike object or NoneType or intlike object or longlike object or floatlike object or boollike object or NoneType or listlike object or dictlike object or setlike object or objectlike object or any other object instance (depending on the file object and mode) or byteslike object or bytearraylike object or strlike object or NoneType or intlike object or longlike object or floatlike object or boollike object or NoneType or listlike object or dictlike object or setlike object or objectlike object or any other object instance (depending on the file object and mode) [size] > int: return size bytes (or the read time out if none specified) from the file. If the optional size argument is negative or omitted, read all data until EOF is reached. The file must be opened in binary mode or by using an appropriate text mode to interpret end of line characters. This method may raise an OSError if the device is not ready. Returns a byteslike object if no size was specified and the default is to read the entire file. Returns a str if no size was specified and the default is to read the entire file. Returns a bytearray if no size was specified and the default is to read the entire file. Returns a list if no size was specified and the default is to read the entire file. Returns a dictionary if no size was specified and the default is to read the entire file. Returns a set if no size was specified and the default is to read the entire file. Returns an integer if no size was specified and the default is to read the entire file. Returns a float if no size was specified and the default is to read the entire file. Returns a boolean if no size was specified and the default is to read the entire file. Returns an integer if no size was specified and the default is to read the entire file. Returns a long if no size was specified and the default is to read the entire file. Returns a float if no size was specified and the default is to read the entire file. Returns a boolean if no size was specified and the default is to read the entire file. Returns an integer if no size was specified and the default is to read the entire file. Returns a long if no size was specified and the default is to read the entire file. Returns a float if no size was specified and the default is to read the entire file. Returns a boolean if no size was specified and the default is to read the entire file. Returns an integer if no size was specified and the default is to read the entire file. Returns a long if no size was specified and the default is to read the entire file. Returns a float if no size was specified and the default is to read the entire file. Returns a boolean if no size was specified and the default is to read the entire file. Returns an integer if no size was specified and the default is to read the entire file. Returns a long if no size was specified and the default is to read the entire file. Returns a float if no size was specified and the default is to read the entire file. Returns a boolean if no size was specified andthe default is to readthe entirefile.Returnsanintegerifnosizewasspecifiedandthedefaultistoreadtheentirefile.Returnsalongifnosizewasspecifiedandthedefaultistoreadtheentirefile.Returnsafloatifnosizewasspecifiedandthedefaultistoreadtheentirefile.Returnsabooleanifnosizewasspecifiedandthedefaultistoreadtheentirefile.Returnsanintegerifnosizewasspecifiedandthedefaultistoreadtheentirefile.Returnsalongifno
原创文章,作者:未希,如若转载,请注明出处:https://www.kdun.com/ask/648055.html
本网站发布或转载的文章及图片均来自网络,其原创性以及文中表达的观点和判断不代表本网站。如有问题,请联系客服处理。
发表回复