本文共 3682 字,大约阅读时间需要 12 分钟。
python也算学了一段时间,有些函数很常用但用的时候还是会忘,现在总结记录一下python里面很实用的并经常用到的内置函数(不用导入其他模块)。
help() 查看在线帮助,可以是各种类型的数据结构或者它们的方法函数。
>>> help(list)Help on class list in module builtins:class list(object) | list() -> new empty list | list(iterable) -> new list initialized from iterable's items | | Methods defined here: | | __add__(self, value, /) | Return self+value. | | __contains__(self, key, /) | Return key in self.
print()
1.直接输出任何类型(数值,字符串,布尔,列表,元组,字典······)>>> print(100)100>>> print('hello python')hello python>>> L=[1,2,'p']>>> print(L)[1, 2, 'p']>>> r={'x':5,'y':10}>>> print(r){'x': 5, 'y': 10}
2.格式化输出(类似c中的printf)
>>> s='hello'>>> x=len(s)>>> print('the length of %s is %d' % (s,x))the length of hello is 5
(1)%字符:标记转换说明符的开始
(2)转换标志:-表示左对齐;+表示在转换值之前要加上正负号;“”(空白字符)表示正数之前保留空格;0表示转换值若位数不够则用0填充
(3)最小字段宽度:转换后的字符串至少应该具有该值指定的宽度。如果是*,则宽度会从值元组中读出。
(4) 点(.)后跟精度值:如果转换的是实数,精度值就表示出现在小数点后的位数。如果转换的是字符串,那么该数字就表示最大字段宽度。如果是*,那么精度将从元组中读出
>>> pi = 3.141592653 >>> print('%10.3f' % pi) #字段宽10,精度3 3.142 >>> print("pi = %.*f" % (3,pi)) #用*从后面的元组中读取字段宽度或精度 pi = 3.142 >>> print('%010.3f' % pi) #用0填充空白 000003.142 >>> print('%-10.3f' % pi) #左对齐 3.142 >>> print('%+f' % pi) #显示正负号 +3.141593
3.print默认换行,若不想换行,可在输出语句后加上end=‘sep’,sep为分隔符。
input() 输入函数,默认输入的是字符串,可通过强转函数转换
int() 把括号内的类型强转为整形 float() 把括号内的类型强转为浮点型 str() 把括号内的类型强转为字符型 list() 转为列表类型 tuple() 转为元组类型>>> a=input('请输入:')请输入:10000>>> print('a的类型为',type(a),a)a的类型为10000>>> a'10000'>>> int(a)10000
abs() 取绝对值函数,适用整型和浮点型
>>> abs(-10.1)10.1
len() 求序列长度(字符串,字典,列表,元组)
>>> a=[1,2,3,4]>>> len(a)4>>> s='hello world'>>> len(s)
max() 取可迭代对象中的最大值
min() 取可迭代对象中的最小值>>> max(1,2,3,4)4>>> max('jaljfla')'l'>>> max([1,2,3,4,7])7>>> min([1,2,3,4,7])1
sum() 获得的迭代对象(列表,元组,集合)的和
>>> sum([0,1,2])3>>> sum([0,1,2],3) #加完后再加36
divmod() 取模函数,求商和余数
>>> divmod(54,4)(13, 2)
pow(a,b(,c)) a的b次方,如果c存在,再对c取模
>>> pow(2,5)32>>> pow(2,5,3)2
bin() 转换为二进制数
oct() 转换为8进制数 hex() 转换为16进制数 (转换后为字符型)>>> hex(25)'0x19'
all() 可迭代对象元素都为真时返回True,否则返回False
any() 可迭代对象元素只要有一个为真就返回True,否则返回False>>> all([1,2,3])True>>> all([0,1,2])False>>> any([0,1,2])True
dir() 查看某个类型所有方法
>>> dir(list)['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
type() 查看数据类型
>>> a={1:2}>>> type(a)#字典类型
ord() 以字符作为参数,返回其ASCll或者Unicode数值
chr() 返回数值对应的字符>>>ord('a')97>>> chr(97)'a'
round(a(,b)) 返回四舍五入后a的值,b控制小数点位数
>>> round(100.001656,3)100.002>>> round(100.000056,3)100.0
however,在实际使用中发现round函数并不总是如上所说的四舍五入。如:
>>> round(2.355,2)2.35
因为该函数对于返回的浮点数并不是按照四舍五入的规则来计算,而会收到计算机表示精度的影响(具体的我也不太清楚?)。
sorted() 对所有可迭代对象排序
>>> L=[4,2,3,1] #列表排序>>> sorted(L)[1, 2, 3, 4]>>> k={'b':2,'d':1,'a':4}>>> L=sorted(k.items()) #按字典的key排序>>> L[('a', 4), ('b', 2), ('d', 1)]>>> L=sorted(k.items(),key=lambda x:x[1]) #按字典的value排序>>> L[('d', 1), ('b', 2), ('a', 4)]>>> L=[('b',2),('a',1),('c',3),('d',4)]>>> sorted(L, cmp=lambda x,y:cmp(x[1],y[1])) # 利用cmp函数[('a', 1), ('b', 2), ('c', 3), ('d', 4)]>>> sorted(L, key=lambda x:x[1]) # 利用key[('a', 1), ('b', 2), ('c', 3), ('d', 4)]
zip() 将多个列表拼接成一个元组列表
>>> list_1=[1,2,3,4]>>> list_2=['a','b']>>> z=zip(list_1,list_2) #z本身时zip类型>>> for i in z:... print(i,end='\t')(1, 'a') (2, 'b')
更新中···
转载地址:http://kefqb.baihongyu.com/