Python中字典的内建函数用法是什么? python内建函数
python\u3001\u4ec0\u4e48\u662f\u5185\u5efa\u51fd\u6570\uff1fpython\u5185\u5efa\u51fd\u6570\u603b\u7ed3
1. abs(x)
abs()\u51fd\u6570\u8fd4\u56de\u6570\u5b57\uff08\u53ef\u4e3a\u666e\u901a\u578b\u3001\u957f\u6574\u578b\u6216\u6d6e\u70b9\u578b\uff09\u7684\u7edd\u5bf9\u503c\u3002\u5982\u679c\u7ed9\u51fa\u590d\u6570\uff0c\u8fd4\u56de\u503c\u5c31\u662f\u8be5\u590d\u6570\u7684\u6a21\u3002\u4f8b\u5982\uff1a
>>>print abs(-2,4)
2.4
>>>print abs(4+2j)
4.472135955
2. apply(function,args[,keywords])
apply()\u51fd\u6570\u5c06args\u53c2\u6570\u5e94\u7528\u5230function\u4e0a\u3002function\u53c2\u6570\u5fc5\u987b\u662f\u53ef\u8c03\u7528\u5bf9\u8c61\uff08\u51fd\u6570\u3001\u65b9\u6cd5\u6216\u5176\u4ed6\u53ef\u8c03\u7528\u5bf9\u8c61\uff09\u3002args\u53c2\u6570\u5fc5\u987b\u4ee5
\u5e8f\u5217\u5f62\u5f0f\u7ed9\u51fa\u3002\u5217\u8868\u5728\u5e94\u7528\u4e4b\u524d\u88ab\u8f6c\u6362\u4e3a\u5143\u7ec4\u3002function\u5bf9\u8c61\u5728\u88ab\u8c03\u7528\u65f6\uff0c\u5c06args\u5217\u8868\u7684\u5185\u5bb9\u5206\u522b\u4f5c\u4e3a\u72ec\u7acb\u7684\u53c2\u6570\u770b\u5f85\u3002\u4f8b\u5982\uff1a
apply(add,(1,3,4))
\u7b49\u4ef7\u4e8e
add(1,3,4)
\u5728\u4ee5\u5217\u8868\u6216\u5143\u7ec4\u5b9a\u4e49\u4e86\u4e00\u5217\u53c2\u6570\uff0c\u4e14\u9700\u8981\u5c06\u6b64\u5217\u8868\u53c2\u6570\u5206\u522b\u4f5c\u4e3a\u4e2a\u4e2a\u72ec\u7acb\u53c2\u6570\u4f7f\u7528\u7684\u60c5\u51b5\u4e0b\uff0c\u5fc5\u987b\u4f7f\u7528apply()\u51fd\u6570\u3002\u5728\u8981\u628a\u53d8\u957f\u53c2\u6570\u5217\u5e94\u7528\u5230\u5df2\u51fd\u6570\u4e0a\u65f6\uff0capply()\u51fd\u6570\u975e\u5e38\u6709\u7528\u3002
\u53ef\u9009\u9879keywords\u53c2\u6570\u5e94\u662f\u4e2a\u5b57\u5178\uff0c\u5b57\u5178\u7684\u5173\u952e\u5b57\u662f\u5b57\u7b26\u4e32\u3002\u8fd9\u4e9b\u5b57\u7b26\u4e32\u5728apply()\u51fd\u6570\u7684\u53c2\u6570\u5217\u672b\u5c3e\u5904\u7ed9\u51fa\uff0c\u5b83\u4eec\u5c06\u88ab\u7528\u4f5c\u5173\u952e\u5b57\u53c2\u6570\u3002
3. buffer(object[,offset[,size]])
\u5982\u679cobject\u5bf9\u8c61\u652f\u6301\u7f13\u5b58\u8c03\u7528\u63a5\u53e3buffer()\u51fd\u6570\u5c31\u4e3aobject\u5bf9\u8c61\u521b\u5efa\u4e00\u4e2a\u65b0\u7f13\u5b58\u3002\u8fd9\u6837\u7684\u5bf9\u8c61\u5305\u62ec\u5b57\u7b26\u4e32\u3001\u6570\u7ec4\u548c\u7f13\u5b58\u3002\u8be5\u65b0\u7f13\u5b58\u901a\u8fc7\u4f7f\u7528\u4ece
offset\u53c2\u6570\u503c\u5f00\u59cb\u77e5\u9053\u8be5\u5bf9\u8c61\u672b\u5c3e\u7684\u5b58\u50a8\u7247\u6bb5\u6216\u4eceoffset\u53c2\u6570\u503c\u5f00\u59cb\u76f4\u5230size\u53c2\u6570\u7ed9\u51fa\u7684\u5c3a\u5bf8\u4e3a\u957f\u5ea6\u7684\u5b58\u50a8\u7247\u6bb5\u6765\u5f15\u7528object\u5bf9\u8c61\u3002\u5982\u679c\u6ca1
\u7ed9\u51fa\u4efb\u4f55\u9009\u9879\u53c2\u6570\uff0c\u7f13\u5b58\u533a\u57df\u5c31\u8986\u76d6\u6574\u4e2a\u5e8f\u5217\uff0c\u6700\u7ec8\u5f97\u5230\u7684\u7f13\u5b58\u5bf9\u8c61\u662fobject\u5bf9\u8c61\u6570\u636e\u7684\u53ea\u8bfb\u62f7\u8d1d\u3002
\u7f13\u5b58\u5bf9\u8c61\u7528\u4e8e\u7ed9\u67d0\u4e2a\u5bf9\u8c61\u7c7b\u578b\u521b\u5efa\u4e00\u4e2a\u66f4\u53cb\u597d\u7684\u63a5\u53e3\u3002\u6bd4\u5982\uff0c\u5b57\u7b26\u4e32\u5bf9\u8c61\u7c7b\u578b\u901a\u7528\u7f13\u5b58\u5bf9\u8c61\u800c\u53d8\u5f97\u53ef\u7528\uff0c\u5141\u8bb8\u9010\u4e2a\u5b57\u8282\u5730\u8bbf\u95ee\u5b57\u7b26\u4e32\u4e2d\u7684\u4fe1\u606f\u3002
4. callable(object)
callable()\u51fd\u6570\u5728object\u5bf9\u8c61\u662f\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u60c5\u51b5\u4e0b\uff0c\u8fd4\u56de\u771f(true)\uff1b\u5426\u5219\u5047(false)\uff0c\u53ef\u8c03\u7528\u5bf9\u8c61\u5305\u62ec\u51fd\u6570\u3001\u65b9\u6cd5\u3001\u4ee3\u7801\u5bf9\u8c61\u3001\u7c7b\uff08\u5728\u8c03\u7528\u65f6\u8fd4\u56de\u65b0\u7684\u5b9e\u4f8b\uff09\u548c\u5df2\u7ecf\u5b9a\u4e49\u2018\u8c03\u7528\u2019\u65b9\u6cd5\u7684\u7c7b\u5b9e\u4f8b
5. chr(i)
chr()\u51fd\u6570\u8fd4\u56de\u4e0eASCII\u7801i\u76f8\u5339\u914d\u7684\u4e00\u4e2a\u5355\u4e00\u5b57\u7b26\u4e32\uff0c\u5982\u4e0b\u4f8b\u6240\u793a\uff1a
>>>print chr(72)+chr(101)+chr(108)+chr(111)
hello
chr()\u51fd\u6570\u662ford()\u51fd\u6570\u7684\u53cd\u51fd\u6570\uff0c\u5176\u4e2dord()\u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u56deASCII\u6574\u6570\u7801\uff0c\u53c2\u6570i\u7684\u53d6\u503c\u5e94\u57280~255\u8303\u56f4\u5185\u3002\u5982\u679c\u53c2\u6570i\u7684\u53d6\u503c\u5728\u6b64\u8303\u56f4\u4e4b\u5916\uff0c\u5c06\u5f15\u53d1ValueError\u5f02\u5e38\u3002
6. cmp(x,y)
cmp()\u51fd\u6570\u6bd4\u8f83x\u548cy\u8fd9\u4e24\u4e2a\u5bf9\u8c61\uff0c\u4e14\u6839\u636e\u6bd4\u8f83\u7ed3\u679c\u8fd4\u56de\u4e00\u4e2a\u6574\u6570\u3002\u5982\u679cxy\uff0c\u5219\u8fd4\u56de\u6b63\u6570\u3002\u8bf7\u6ce8\u610f\uff0c\u6b64\u51fd\u6570\u7279\u522b\u7528\u6765\u6bd4\u8f83\u6570\u503c\u5927\u5c0f\uff0c\u800c\u4e0d\u662f\u4efb\u4f55\u5f15\u7528\u5173\u7cfb\uff0c\u56e0\u800c\u6709\u4e0b\u9762\u7684\u7ed3\u679c\uff1a
>>>a=99
>>>b=int('99')
>>>cmp(a,b)
0
7. coerce(x,y)
coerce()\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u5143\u7ec4\uff0c\u8be5\u5143\u7ec4\u7531\u4e24\u4e2a\u6570\u503c\u578b\u53c2\u6570\u7ec4\u6210\u3002\u6b64\u51fd\u6570\u5c06\u4e24\u4e2a\u6570\u503c\u578b\u53c2\u6570\u8f6c\u6362\u4e3a\u540c\u4e00\u7c7b\u578b\u6570\u5b57\uff0c\u5176\u8f6c\u6362\u89c4\u5219\u4e0e\u7b97\u672f\u8f6c\u6362\u89c4\u5219\u4e00\u6837\u3002\u4e00\u4e0b\u662f\u4e24\u4e2a\u4f8b\u5b50\uff1a
>>>a=1
>>>b=1.2
>>>coerce(a,b)
(1.0,1.2)
>>>a=1+2j
>>>b=4.3e10
>>>coerce(a,b)
((1+2j),(43000000000+0j))
8 compile(string,filename,kind)
compile()\u51fd\u6570\u5c06string\u7f16\u8bd1\u4e3a\u4ee3\u7801\u5bf9\u8c61\uff0c\u7f16\u8bd1\u751f\u6210\u7684\u4ee3\u7801\u5bf9\u8c61\u63a5\u4e0b\u6765\u88abexec\u8bed\u53e5\u6267\u884c\uff0c\u63a5\u7740\u80fd\u5229\u7528eval()\u51fd\u6570\u5bf9\u5176\u8fdb\u884c\u6c42\u503c\u3002
filename\u53c2\u6570\u5e94\u662f\u4ee3\u7801\u4ece\u5176\u4e2d\u8bfb\u51fa\u7684\u6587\u4ef6\u540d\u3002\u5982\u679c\u5185\u90e8\u751f\u6210\u6587\u4ef6\u540d\uff0cfilename\u53c2\u6570\u503c\u5e94\u662f\u76f8\u5e94\u7684\u6807\u8bc6\u7b26\u3002kind\u53c2\u6570\u6307\u5b9astring\u53c2\u6570\u4e2d\u6240\u542b\u4ee3\u7801\u7684\u7c7b\u522b\u3002
\u4e3e\u4f8b\u5982\u4e0b\uff1a
>>>a=compile(\u2018print \u201cHello World\u201d\u2019,\u2019\u2019,\u2019single\u2019)
>>>exec(a)
Hello World
>>>eval(a)
Hello World
9. complex(real,[image])
Complex()\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u590d\u6570\uff0c\u5176\u5b9e\u90e8\u4e3areal\u53c2\u6570\u503c\u3002\u5982\u679c\u7ed9\u51faimage\u53c2\u6570\u7684\u503c\uff0c\u5219\u865a\u90e8\u5c31\u4e3aimage\uff1b\u5982\u679c\u9ed8\u8ba4image\u53c2\u6570\uff0c\u5219\u865a\u90e8\u4e3a0j\u3002
10. delattr(object,name)
delattr()\u51fd\u6570\u5728object\u5bf9\u8c61\u8bb8\u53ef\u65f6\uff0c\u5220\u9664object\u5bf9\u8c61\u7684name\u5c5e\u6027\uff0c\u6b64\u51fd\u6570\u7b49\u4ef7\u4e8e\u5982\u4e0b\u8bed\u53e5\uff1a
del object.attr
\u800cdelattr()\u51fd\u6570\u5141\u8bb8\u5229\u7528\u7f16\u7a0b\u65b9\u6cd5\u5b9a\u4e49\u6765\u5b9a\u4e49object\u548cname\u53c2\u6570\uff0c\u5e76\u4e0d\u662f\u5728\u4ee3\u7801\u4e2d\u663e\u793a\u6307\u5b9a\u3002
\u5176\u5b9e\u5b89\u88c5python\u5305\u4f1a\u81ea\u5e26\u4e00\u4e2a\u6709\u95ee\u53f7\u6807\u793a\u201c\uff1f\u201d\u7684"Python Manuals"\u53ef\u4ee5\u4ed4\u7ec6\u9605\u8bfb\u4e00\u4e0b\uff0c\u4e5f\u53ef\u4f5c\u5e2e\u52a9\u6587\u6863\u7684\u3002
\u4ecb\u7ecdPython\u7684\u5185\u5efa\u51fd\u6570\u548c\u5f02\u5e38.\u8bb8\u591a\u5185\u5efa\u51fd\u6570\u7684\u7ec6\u8282\u53ca\u7279\u6027\u53ef\u4ee5\u5728\u8fd9\u91cc\u627e\u5230.
\u5185\u5efa\u51fd\u6570
\u672c\u8282\u4ecb\u7ecd\u7684\u51fd\u6570\u5728\u89e3\u91ca\u5668\u4e2d\u603b\u662f\u53ef\u7528\u7684,\u4ed6\u4eec\u88ab\u5305\u542b\u5728 __builtin__ \u6a21\u5757\u91cc.\u53e6\u5916\u6bcf\u4e2a\u6a21\u5757\u7684 __builtins__ \u5c5e\u6027\u7ecf\u5e38\u6307\u7684\u662f\u8fd9\u4e2a\u6a21\u5757(\u9664\u4e86\u5f53\u5728restricted execution\u73af\u5883\u4e0b\u8fd0\u884c\u65f6).
_(\u4e0b\u5212\u7ebf)
\u9ed8\u8ba4\u60c5\u51b5\u4e0b,\u53d8\u91cf _ \u7528\u4e8e\u5728\u4ea4\u4e92\u6a21\u5f0f\u4e0b\u8868\u793a\u6700\u8fd1\u4e00\u4e2a\u8868\u8fbe\u5f0f\u7684\u8fd0\u884c\u7ed3\u679c.
\u53c2\u9605 sys.displayhook (118)
__import__(name [, globals [, locals [, fromlist]]])
import\u8bed\u53e5\u901a\u8fc7\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\u6765\u5bfc\u5165\u6a21\u5757. name\u662f\u5305\u542b\u6a21\u5757\u540d\u5b57\u7684\u5b57\u7b26\u4e32, globals\u662f\u4e00\u4e2a\u53ef\u9009\u7684\u5b9a\u4e49\u5168\u5c40\u540d\u79f0\u7a7a\u95f4\u7684\u5b57\u5178, locals\u662f\u5b9a\u4e49\u5c40\u90e8\u540d\u79f0\u7a7a\u95f4\u7684\u5b57\u5178, fromlist\u662ffrom\u8bed\u53e5\u76ee\u6807\u7684\u5217\u8868.\u4f8b\u5982, import spam\u8bed\u53e5\u4f1a\u8c03\u7528__import__('spam', globals(), locals(), []) ,\u800cfrom spam import foo \u8bed\u53e5\u4f1a\u8c03\u7528 __import__('spam', globals(), locals(), ['foo']). \u5982\u679c\u6a21\u5757\u540d\u5728\u5305\u540d\u5b57\u4e4b\u540e(\u5982foo.bar)\u800c\u4e14fromlist\u4e3a\u7a7a\u65f6,\u5c31\u8fd4\u56de\u5bf9\u5e94\u7684\u6a21\u5757\u5bf9\u8c61.\u5982\u679cfromlist\u4e0d\u4e3a\u7a7a,\u5219\u53ea\u4f1a\u8fd4\u56de\u6700\u9876\u7ea7\u7684\u5305.
\u8fd9\u4e2a\u51fd\u6570\u662f\u4e00\u4e2a\u4f4e\u7b49\u7ea7\u7684\u6a21\u5757\u8f7d\u5165\u63a5\u53e3.\u5b83\u5e76\u4e0d\u6267\u884cimport\u8bed\u53e5\u7684\u6240\u6709\u6b65\u9aa4(\u901a\u5e38\u60c5\u51b5\u4e0b\u5c40\u90e8\u540d\u79f0\u7a7a\u95f4\u5e76\u4e0d\u4f1a\u968f\u6a21\u5757\u4e2d\u5305\u542b\u5bf9\u8c61\u7684\u540d\u79f0\u5f15\u7528\u7684\u6539\u53d8\u800c\u6539\u53d8.\u8fd9\u4e2a\u51fd\u6570\u53ef\u4ee5\u7531\u7528\u6237\u6765\u91cd\u65b0\u5b9a\u4e49,\u8fd9\u6837\u4e3aimport\u8bed\u53e5\u52a0\u5165\u65b0\u7684\u884c\u4e3a.\u9ed8\u8ba4\u7684\u6267\u884c\u5e76\u4e0d\u4f1a\u68c0\u67e5locals\u53c2\u6570,\u800cglobals\u53ea\u7528\u4e8e\u51b3\u5b9a\u5305\u7684\u5185\u5bb9(\u8fd9\u4e9b\u53c2\u6570\u53ef\u4ee5\u4f7f __import__()\u80fd\u591f\u5b8c\u6574\u5730\u8bbf\u95ee\u5168\u5c40\u548c\u5c40\u90e8\u540d\u79f0\u7a7a\u95f4)
abs(x)
\u8fd4\u56dex\u7684\u7edd\u5bf9\u503c
apply(func [, args [, keywords]])
\u5bf9\u53ef\u8c03\u7528\u5bf9\u8c61func\u6267\u884c\u51fd\u6570\u8c03\u7528. args\u662f\u4e00\u4e2a\u5305\u542b\u56fa\u5b9a\u4f4d\u7f6e\u53c2\u6570\u7684\u5143\u7ec4, keywords\u662f\u5305\u542b\u5173\u952e\u53c2\u6570\u7684\u5b57\u5178. apply()\u51fd\u6570\u4e5f\u53ef\u4ee5\u5199\u505afunc(*args ,**keywords ).
buffer(sequence [, offset [, size]])
\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u7f13\u51b2\u5668\u5bf9\u8c61.\u7f13\u51b2\u5668\u901a\u5e38\u662f\u4e00\u4e2a\u5e8f\u5217(\u5982\u5b57\u7b26\u4e32)\u7684\u5b57\u8282\u5bfc\u5411\u5e8f\u5217.\u7f13\u51b2\u5668\u548c\u5b57\u7b26\u4e32\u5728\u8bb8\u591a\u5730\u65b9\u662f\u76f8\u540c\u7684,\u4f46\u662f\u5b83\u4e0d\u652f\u6301\u5b57\u7b26\u4e32\u65b9\u6cd5,\u800c\u4e14\u4e5f\u4e0d\u80fd\u4f7f\u7528string\u6a21\u5757\u7684\u51fd\u6570.
callable(object)
\u5f53object\u4e3a\u53ef\u8c03\u7528\u5bf9\u8c61\u65f6\u8fd4\u56deTrue,\u5426\u5219\u8fd4\u56deFalse
chr(i)
\u5c06\u4e00\u4e2a0\u5230255\u7684\u6574\u6570\u8f6c\u6362\u4e3a\u4e00\u4e2a\u5b57\u7b26.
cmp(x,y)
\u6bd4\u8f83x\u548cy. x y\u8fd4\u56de\u6574\u6570.\u5b83\u53ef\u4ee5\u6bd4\u8f83\u4efb\u610f\u4e24\u4e2a\u5bf9\u8c61\u5e76\u8fd4\u56de\u7ed3\u679c,\u5373\u4f7f\u6709\u65f6\u5019\u5bf9\u8c61\u7684\u6bd4\u8f83\u8c6a\u65e0\u610f\u4e49(\u4f8b\u5982\u6bd4\u8f83\u6587\u4ef6\u5bf9\u8c61).\u5728\u67d0\u4e9b\u73af\u5883\u4e0b,\u8fd9\u6837\u7684\u6bd4\u8f83\u4f1a\u5f15\u53d1\u5f02\u5e38.
coerce(x,y)
\u5c06x\u548cy\u503c\u8f6c\u6362\u4e3a\u540c\u4e00\u4e2a\u6570\u503c\u7c7b\u578b\u5e76\u4f5c\u4e3a\u4e00\u4e2a\u5143\u7ec4\u8fd4\u56de.(\u7b2c\u4e09\u7ae0,\u7c7b\u578b\u548c\u5bf9\u8c61)
compile(string, filename, kind)
\u4f7f\u7528exec()\u6216eval()\u5c06\u5b57\u7b26\u4e32\u7f16\u8bd1\u4e3a\u4ee3\u7801\u5bf9\u8c61. filename is a string containing the name of the file in which the string was defined. kind\u4e3a'exec'\u65f6\u4ee3\u8868\u4e00\u4e2a\u8868\u8fbe\u5f0f\u7684\u5e8f\u5217, 'eval'\u4ee3\u8868\u4e00\u4e2a\u8868\u8fbe\u5f0f, 'single'\u4ee3\u8868\u4e00\u4e2a\u8fd0\u884c\u8bed\u53e5.
complex(real [, imag])
\u521b\u5efa\u4e00\u4e2a\u590d\u6570
delattr(object, attr)
\u5220\u9664\u5bf9\u8c61\u7684\u4e00\u4e2a\u5c5e\u6027, attr\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32.\u4e0e del object.attr\u76f8\u540c
dir([object])
\u8fd4\u56de\u5305\u542b\u5c5e\u6027\u540d\u79f0\u7684\u5217\u8868.\u5b83\u4eec\u6765\u81ea\u5bf9\u8c61\u7684 __dict__, __methods__,\u4ee5\u53ca __members__ \u5c5e\u6027.\u5982\u679c\u6ca1\u6709\u4f20\u9012\u7ed9\u5b83\u53c2\u6570,\u5219\u4f1a\u8fd4\u56de\u5f53\u524d\u7684local symbol table
divmod(a,b)
\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u5546\u548c\u4f59\u6570\u7684\u5143\u7ec4.\u5bf9\u4e8e\u6574\u6570,\u5c06\u8fd4\u56de(a / b , a % b ),\u5bf9\u4e8e\u6d6e\u70b9\u6570,\u5c06\u8fd4\u56de(math.floor(a / b ), a % b )
eval(expr [, globals [, locals]])
\u8ba1\u7b97\u4e00\u4e2a\u8868\u8fbe\u5f0f\u7684\u503c. expr\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\u6216\u7531compile()\u521b\u5efa\u7684\u4e00\u4e2a\u4ee3\u7801\u5bf9\u8c61. globals\u548clocals\u4e3a\u64cd\u4f5c\u5b9a\u4e49\u7684\u5168\u5c40\u548c\u5c40\u90e8\u540d\u79f0\u7a7a\u95f4,\u5f53\u7701\u7565\u65f6,\u8868\u8fbe\u5f0f\u5c06\u5728\u8c03\u7528\u65f6\u7684\u540d\u79f0\u7a7a\u95f4\u8ba1\u7b97.
execfile(filename [, globals [, locals]])
\u8fd0\u884c\u6587\u4ef6filename\u4e2d\u7684\u8bed\u53e5. globals\u548clocals\u5b9a\u4e49\u4e86\u6587\u4ef6\u8fd0\u884c\u7684\u5168\u5c40\u548c\u5c40\u90e8\u540d\u79f0\u7a7a\u95f4,\u5f53\u7701\u7565\u65f6,\u6587\u4ef6\u5c06\u5728\u8c03\u7528\u65f6\u7684\u540d\u79f0\u7a7a\u95f4\u8fd0\u884c.\u8fd9\u4e2a\u51fd\u6570\u4e0d\u80fd\u5728\u4e00\u4e2a\u51fd\u6570\u4e3b\u4f53\u91cc\u4f7f\u7528,\u56e0\u4e3a\u5b83\u4e0e\u5185\u5d4c\u8303\u56f4\u4e0d\u76f8\u5bb9.
filter(function, list)
\u4f7f\u7528func()\u51fd\u6570\u6765\u8fc7\u6ee4s\u4e2d\u7684\u5143\u7d20.\u4f7ffunc\u8fd4\u56de\u503c\u4e3afalse\u7684\u5143\u7d20\u88ab\u4e22\u5f03,\u5176\u5b83\u7684\u5b58\u5165filter\u51fd\u6570\u8fd4\u56de\u7684\u5217\u8868\u4e2d.\u5982\u679cfunction\u662fNone,\u5219list\u4e2d\u503c\u4e3aFalse\u7684\u5143\u7d20\u5c31\u88ab\u5220\u9664.
float(x)
\u5c06x\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570
getattr(object, name [, default])
\u8fd4\u56de\u4e00\u4e2a\u5bf9\u8c61\u7684\u5c5e\u6027. name\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32. default\u662f\u4e00\u4e2a\u53ef\u9009\u7684\u503c,\u4ee3\u8868\u5f53\u6ca1\u6709\u8fd9\u4e2a\u5c5e\u6027\u65f6\u8fd4\u56de\u7684\u503c. \u4e0e object.name \u7ed3\u679c\u76f8\u540c
globals()
\u8fd4\u56de\u4e00\u4e2a\u4e0e\u5168\u5c40\u540d\u79f0\u7a7a\u95f4\u5bf9\u5e94\u7684\u5b57\u5178
hasattr(object, name)
\u8fd4\u56deobject\u662f\u5426\u6709name\u5c5e\u6027,\u5e03\u5c14\u503c
hash(object)
\u8fd4\u56de\u4e00\u4e2a\u5bf9\u8c61\u7684\u6574\u6570\u54c8\u5e0c\u503c(\u5982\u679c\u53ef\u80fd).\u4e24\u4e2a\u76f8\u7b49\u5bf9\u8c61\u7684\u54c8\u5e0c\u503c\u662f\u76f8\u540c\u7684.\u6a21\u5757\u6ca1\u6709\u5b9a\u4e49\u4e00\u4e2a\u54c8\u5e0c\u503c.
hex(x)
\u5c06\u4e00\u4e2a\u6574\u6570\u6216\u957f\u6574\u6570\u8f6c\u6362\u4e3a\u4e00\u4e2a\u5341\u516d\u8fdb\u5236\u7684\u5b57\u7b26\u4e32
id(object)
\u8fd4\u56de\u4e00\u4e2a\u5bf9\u8c61\u7684\u6574\u6570id
input([prompt])
\u76f8\u5f53\u4e8eeval(raw_input(prompt ))
int(x [, base])
\u5c06\u4e00\u4e2a\u6570\u5b57\u6216\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570. \u53ef\u9009\u53c2\u6570base\u4ee3\u8868\u4ece\u5b57\u7b26\u4e32\u8f6c\u6362\u65f6\u7684\u57fa\u7840/\u6839\u636e
intern(string)
Checks to see whether string is contained in an internal table of strings. If found, a copy of the internal string is returned. If not, string is added to the internal table and returned. This function is primarily used to get better performance in operations involving dictionary lookups. Interned strings are never garbage-collected. Not applicable to Unicode strings.
isinstance(object, classobj)
\u68c0\u67e5object\u662f\u5426\u662fclassobj\u7684\u4e8b\u4f8b\u6216\u5b50\u7c7b.\u4e5f\u53ef\u7528\u4e8e\u68c0\u67e5\u7c7b\u578b
issubclass(class1, class2)
\u68c0\u67e5class1\u662f\u5426\u662fclass2\u7684\u5b50\u7c7b(\u6d3e\u751f\u7c7b)
\u6ce8\u610f: issubclass(A , A )\u8fd4\u56deTrue
len(s)
\u8fd4\u56de\u5e8f\u5217s\u4e2d\u5305\u542b\u7684\u6761\u76ee\u6570\u76ee
list(s)
\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u5e8f\u5217s\u4e2d\u6761\u76ee\u7684\u65b0\u5217\u8868
locals()
\u8fd4\u56de\u4e00\u4e2a\u4e0e\u8c03\u7528\u65f6\u5c40\u90e8\u540d\u79f0\u7a7a\u95f4\u76f8\u5bf9\u5e94\u7684\u5b57\u5178
long(x [, base])
\u5c06\u4e00\u4e2a\u6570\u5b57\u6216\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u957f\u6574\u6570,\u53ef\u9009\u53c2\u6570base\u4ee3\u8868\u4ece\u5b57\u7b26\u4e32\u8f6c\u6362\u65f6\u7684\u57fa\u7840/\u6839\u636e
map(function, list, ...)
\u5c06\u5e8f\u5217list\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u4f20\u9012\u7ed9function\u51fd\u6570\u505a\u53c2\u6570,\u51fd\u6570\u7684\u8fd4\u56de\u503c\u7ec4\u6210\u5217\u8868\u5e76\u8fd4\u56de.\u5982\u679c\u63d0\u4f9b\u7ed9\u591a\u4e2a\u5217\u8868,\u5219\u51fd\u6570\u5e94\u5305\u542b\u6709\u591a\u4e2a\u53c2\u6570,\u6bcf\u4e2a\u53c2\u6570\u4ece\u4e0d\u540c\u7684\u5217\u8868\u83b7\u5f97.\u5982\u679c\u51fd\u6570\u4e3aNone,\u5219\u9ed8\u8ba4\u4e3a identity function(?\u8eab\u4efd\u51fd\u6570).\u5982\u679cNone\u6620\u5c04\u5230\u591a\u4e2a\u5217\u8868,\u5219\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u5143\u7ec4\u7684\u5217\u8868,\u5143\u7ec4\u7684\u6bcf\u4e2a\u5143\u7d20\u5206\u522b\u6765\u81ea\u5404\u4e2a\u5217\u8868.\u5982\u679c\u6709\u5fc5\u8981,\u77ed\u7684\u5217\u8868\u5c06\u4f7f\u7528None\u6765\u6269\u5145\u5230\u4e0e\u6700\u957f\u5217\u8868\u957f\u5ea6\u76f8\u7b49. map\u53ef\u4ee5\u4f7f\u7528list comprehensions \u6765\u4ee3\u66ff.\u4f8b\u5982map(function , alist ),\u53ef\u4ee5\u4f7f\u7528[function (x) for x in alist ]\u6765\u4ee3\u66ff
\u53c2\u9605 zip (105).
max(s [, args, ...])
\u5355\u4e2a\u53c2\u6570\u65f6,\u8fd4\u56de\u5e8f\u5217s\u4e2d\u7684\u6700\u5927\u503c.\u591a\u4e2a\u53c2\u6570\u65f6,\u8fd4\u56de\u503c\u6700\u5927\u7684\u53c2\u6570
min(s [, args, ...])
\u5355\u4e2a\u53c2\u6570\u65f6,\u8fd4\u56de\u5e8f\u5217s\u4e2d\u7684\u6700\u5c0f\u503c.\u591a\u4e2a\u53c2\u6570\u65f6,\u8fd4\u56de\u503c\u6700\u5c0f\u7684\u53c2\u6570
oct(x)
\u5c06\u4e00\u4e2a\u6574\u6570\u6216\u8005\u957f\u6574\u6570\u8f6c\u6362\u4e3a\u516b\u8fdb\u5236\u5b57\u7b26\u4e32
open(filename [, mode [, bufsize]])
\u6253\u5f00\u6587\u4ef6filename\u5e76\u8fd4\u56de\u4e00\u4e2a\u6587\u4ef6\u5bf9\u8c61(\u7b2c\u5341\u7ae0,\u8fd0\u884c\u73af\u5883). mode\u4ee3\u8868\u6587\u4ef6\u6253\u5f00\u7684\u6a21\u5f0f. 'r' \u8868\u793a\u8bfb, 'w' \u8868\u793a\u5199, 'a' \u8868\u793a\u5728\u6587\u4ef6\u672b\u5c3e\u6dfb\u52a0\u5185\u5bb9. \u8fd8\u6709\u4e00\u79cd\u66f4\u65b0\u6a21\u5f0f,\u4f60\u53ea\u8981\u5728\u8bfb\u5199\u6a21\u5f0f\u540e\u589e\u52a0\u4e00\u4e2a'+'\u5c31\u53ef\u4ee5\u4f7f\u7528\u8fd9\u79cd\u6a21\u5f0f,\u5982'r+' \u6216 'w+'.\u5f53\u4e00\u4e2a\u6587\u4ef6\u4ee5\u66f4\u65b0\u6a21\u5f0f\u6253\u5f00,\u4f60\u5c31\u53ef\u4ee5\u5bf9\u8fd9\u4e2a\u6587\u4ef6\u8fdb\u884c\u8bfb\u5199\u64cd\u4f5c.\u53ea\u8981\u5728\u4efb\u4f55\u8bfb\u53d6\u64cd\u4f5c\u4e4b\u524d\u5237\u65b0\u6240\u6709\u7684\u8f93\u51fa\u7f13\u51b2\u5c31\u4e0d\u4f1a\u6709\u95ee\u9898.\u5982\u679c\u4e00\u4e2a\u6587\u4ef6\u4ee5 'w+' \u6a21\u5f0f\u6253\u5f00,\u5b83\u7684\u957f\u5ea6\u5c31\u5ea6\u622a\u4e3a 0.\u5f53mode\u7701\u7565\u65f6,\u5c06\u4f1a\u4f7f\u7528'w'\u6a21\u5f0f.bufsize\u53c2\u6570\u6307\u5b9a\u4e86\u7f13\u51b2\u884c\u4e3a, 0\u4ee3\u8868\u65e0\u7f13\u51b2,1\u4ee3\u8868\u884c\u7f13\u51b2,\u5176\u4ed6\u6b63\u6570\u4ee3\u8868\u4e00\u4e2a\u5927\u7ea6\u7684\u5b57\u8282\u7f13\u51b2\u5668\u5927\u5c0f,\u8d1f\u6570\u4ee3\u8868\u4f7f\u7528\u7cfb\u7edf\u9ed8\u8ba4\u7f13\u51b2\u5668\u5927\u5c0f(\u4e5f\u662f\u9ed8\u8ba4\u884c\u4e3a)
ord(c)
\u8fd4\u56de\u5355\u4e2a\u5b57\u7b26c\u7684\u6574\u6570\u987a\u5e8f\u503c.\u666e\u901a\u5b57\u7b26\u8fd4\u56de[0,255]\u4e2d\u7684\u4e00\u4e2a\u503c,Unicode\u5b57\u7b26\u8fd4\u56de [0,65535]\u4e2d\u7684\u4e00\u4e2a\u503c
pow(x, y [, z])
\u8fd4\u56dex ** y ,\u5982\u679cz\u5b58\u5728\u8fd4\u56de(x ** y ) % z
range([start,] stop [, step])
\u8fd4\u56de\u4e00\u4e2a\u4ecestart\u5230stop\u7684\u6574\u6570\u5e8f\u5217, step\u4ee3\u8868\u6b65\u8fdb,\u9ed8\u8ba4\u503c\u4e3a1. start\u9ed8\u8ba4\u503c\u4e3a0.\u8d1f\u6570\u7684step\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u9012\u51cf\u7684\u6574\u6570\u5e8f\u5217
\u53c2\u9605xrange (105)
raw_input([prompt])
\u4ece\u6807\u51c6\u8f93\u5165(sys.stdin)\u4e2d\u8bfb\u53d6\u4e00\u884c,\u5e76\u628a\u5b83\u4f5c\u4e3a\u5b57\u7b26\u4e32\u8fd4\u56de.\u5982\u679c\u63d0\u4f9b\u4e86prompt,\u5b83\u5c06\u9996\u5148\u6253\u5370\u5230\u6807\u51c6\u8f93\u51fa(sys.stdout).\u5f53\u8bfb\u53d6\u5230\u4e00\u4e2aEOF\u65f6,\u5c31\u4f1a\u5f15\u53d1\u4e00\u4e2aEOFError\u5f02\u5e38.\u5982\u679creadline\u6a21\u5757\u88ab\u5bfc\u5165,\u90a3\u4e48\u8fd9\u4e2a\u51fd\u6570\u4f1a\u4f7f\u7528\u5b83\u6765\u63d0\u4f9b\u66f4\u9ad8\u7ea7\u7684\u529f\u80fd
reduce(func, seq [, initializer])
\u51fd\u6570\u4ece\u4e00\u4e2a\u5e8f\u5217\u6536\u96c6\u4fe1\u606f,\u7136\u540e\u53ea\u8fd4\u56de\u4e00\u4e2a\u503c(\u4f8b\u5982\u6c42\u548c,\u6700\u5927\u503c,\u7b49).\u5b83\u9996\u5148\u4ee5\u5e8f\u5217\u7684\u524d\u4e24\u4e2a\u5143\u7d20\u8c03\u7528\u51fd\u6570,\u518d\u5c06\u8fd4\u56de\u503c\u548c\u7b2c\u4e09\u4e2a\u53c2\u6570\u4f5c\u4e3a\u53c2\u6570\u8c03\u7528\u51fd\u6570,\u4f9d\u6b21\u6267\u884c\u4e0b\u53bb,\u8fd4\u56de\u6700\u7ec8\u7684\u503c. func\u51fd\u6570\u6709\u4e14\u53ea\u6709\u4e24\u4e2a\u53c2\u6570.\u5728seq\u4e3a\u7a7a\u65f6,\u5c06\u4f7f\u7528\u521d\u59cb\u503cinitializer.
reload(module)
\u91cd\u65b0\u5bfc\u5165\u4e00\u4e2a\u5df2\u7ecf\u5bfc\u5165\u7684\u6a21\u5757. module\u5fc5\u987b\u662f\u4e00\u4e2a\u5df2\u7ecf\u5b58\u5728\u7684\u6a21\u5757\u5bf9\u8c61.\u4e00\u822c\u60c5\u51b5\u4e0b\u5e76\u4e0d\u9f13\u52b1\u4f7f\u7528\u8fd9\u4e2a\u51fd\u6570,\u9664\u4e86\u5728\u8c03\u8bd5\u7684\u65f6\u5019.
\u5f53\u4e00\u4e2a\u6a21\u5757\u91cd\u5bfc\u5165\u65f6,\u5b9a\u4e49\u5b83\u7684\u5168\u5c40\u540d\u79f0\u7a7a\u95f4\u7684\u5b57\u5178\u4f9d\u7136\u5b58\u5728.Thus, definitions in the old module that aren\u2019t part of the newly reloaded module are retained.\u6a21\u5757\u53ef\u4ee5\u5229\u7528\u8fd9\u4e2a\u6765\u68c0\u67e5\u4ed6\u4eec\u662f\u5426\u5df2\u7ecf\u88ab\u5bfc\u5165.
\u91cd\u5bfc\u5165\u4e00\u4e2a\u4f7f\u7528C\u7f16\u5199\u7684\u6a21\u5757\u901a\u5e38\u662f\u4e0d\u5408\u6cd5\u7684
If any other modules have imported this module by using the from statement, they\u2019ll continue to use the definitions in the previously imported module. This problem can be avoided by either reissuing the from statement after a module has been reloaded or using fully qualified names such as module.name .
\u5982\u679c\u6709\u4f7f\u7528\u4ee5\u524d\u6a21\u5757\u4e2d\u7c7b\u521b\u5efa\u7684\u5b9e\u4f8b,\u5b83\u4eec\u5c06\u7ee7\u7eed\u4f7f\u7528\u4ee5\u524d\u7684\u6a21\u5757
repr(object)
\u8fd4\u56de\u4e00\u4e2a\u5bf9\u8c61\u7684\u6807\u51c6\u5b57\u7b26\u4e32\u8868\u793a.\u4e0e\u5411\u540e\u7684\u5f15\u53f7 `object` \u76f8\u540c.\u5927\u591a\u6570\u60c5\u51b5\u4e0b,\u8fd4\u56de\u7684\u5b57\u7b26\u4e32\u53ef\u4ee5\u4f7f\u7528eval()\u6765\u518d\u6b21\u521b\u5efa\u8fd9\u4e2a\u5bf9\u8c61.
round(x [, n])
Returns the result of rounding the floating-point number x to the closest multiple of 10 to the power minus n . If n is omitted, it defaults to 0. If two multiples are equally close, rounding is done away from 0 (\u4f8b\u5982, 0.5 is rounded to 1.0 and -0.5 is rounded to -1.0).
setattr(object, name, value)
\u8bbe\u7f6e\u4e00\u4e2a\u5bf9\u8c61\u7684\u5c5e\u6027. name\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32. \u76f8\u5f53\u4e8eobject.name = value .
slice([start,] stop [, step])
\u8fd4\u56de\u4e00\u4e2a\u4ee3\u8868\u6307\u5b9a\u6570\u5217\u4e2d\u4e00\u4e2a\u6574\u6570\u7684\u5207\u7247\u5bf9\u8c61.\u5207\u7247\u5bf9\u8c61\u4e5f\u53ef\u4ee5\u6709\u6269\u5c55\u5207\u7247\u64cd\u4f5c\u8bed\u53e5\u6765\u4ea7\u751f.(\u7b2c\u4e09\u7ae0,\u5e8f\u5217\u548c\u6620\u5c04\u65b9\u6cd5)
str(object)
\u8fd4\u56de\u8868\u793a\u5bf9\u8c61\u7684\u53ef\u6253\u5370\u5f62\u5f0f\u7684\u5b57\u7b26\u4e32.\u4e0eprint\u8bed\u53e5\u4ea7\u751f\u7684\u5b57\u7b26\u4e32\u76f8\u540c.
tuple(s)
\u4ece\u5e8f\u5217s\u4e2d\u521b\u5efa\u4e00\u4e2a\u5143\u7ec4.\u5982\u679cs\u5df2\u7ecf\u662f\u4e00\u4e2a\u5143\u7ec4,\u5219\u8fd4\u56des
type(object)
\u8fd4\u56deobject\u7684\u7c7b\u578b,\u5b83\u662f\u4e00\u4e2atypes\u6a21\u5757\u4e2d\u5b9a\u4e49type\u7c7b\u578b
\u53c2\u9605isinstance (102)
unichr(i)
\u5c06\u4e00\u4e2a0\u523065535\u7684\u6574\u6570\u8f6c\u6362\u4e3a\u4e00\u4e2aUnicode\u5b57\u7b26
unicode(string [, encoding [, errors]])
\u5c06string\u8f6c\u6362\u4e3aUnicode\u5b57\u7b26\u4e32. encoding\u6307\u5b9a\u4e86string\u7684\u6570\u636e\u7f16\u7801,\u5b83\u88ab\u7701\u7565\u65f6,\u5c06\u4f7f\u7528sys.getdefaultencoding(). errors\u6307\u5b9a\u7f16\u7801\u9519\u8bef\u5904\u7406\u65b9\u5f0f.('strict', 'ignore', \u6216 'replace' .\u53c2\u9605\u7b2c\u4e09\u7ae0\u548c\u7b2c\u4e5d\u7ae0\u4e2d\u6709\u5173Unicode\u5185\u5bb9)
vars([object])
\u8fd4\u56deobject\u7684 symbol table (\u901a\u5e38\u5728object\u7684__dict__\u5c5e\u6027).\u5982\u679c\u6ca1\u6709\u7ed9\u5b83\u63d0\u4f9b\u53c2\u6570,\u5219\u8fd4\u56de\u5bf9\u5e94\u5f53\u524d\u5c40\u90e8\u540d\u79f0\u7a7a\u95f4\u7684\u5b57\u5178.
xrange([start,] stop [, step])
\u548crange\u51fd\u6570\u76f8\u4f3c,\u4f46\u8fd4\u56de\u7684\u662f\u4e00\u4e2aXRangeType\u5bf9\u8c61.\u5b83\u751f\u6210\u4e00\u4e2a\u8fed\u4ee3\u5668,\u5c31\u662f\u53ea\u6709\u7528\u90a3\u4e2a\u6570\u65f6\u624d\u4e34\u65f6\u901a\u8fc7\u8ba1\u7b97\u63d0\u4f9b\u503c,\u800c\u4e0d\u662f\u5168\u90e8\u50a8\u5b58\u5b83\u4eec.\u8fd9\u6837\u5728\u5904\u7406\u5927\u7684\u6570\u5217\u65f6\u80fd\u8282\u7701\u5927\u91cf\u7684\u5185\u5b58.
zip(s1 [, s2 [,..]])
\u7528\u6765\u5c06\u51e0\u4e2a\u5e8f\u5217\u7ec4\u5408\u6210\u4e00\u4e2a\u5305\u542b\u5143\u7ec4\u7684\u5e8f\u5217,\u5e8f\u5217\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20t[i ] = (s1[i ], s2[i ], ..., sn[i ]).\u7ed3\u679c\u4e0e\u6700\u77ed\u5e8f\u5217\u7684\u957f\u5ea6\u76f8\u7b49.
点击上方 "Python人工智能技术" 关注,星标或者置顶
22点24分准时推送,第一时间送达
后台回复“大礼包”,送你特别福利
编辑:乐乐 | 来自:pypypypy
上一篇:
正文
大家好,我是Pythn人工智能技术。
内置函数就是Python给你提供的,拿来直接用的函数,比如print.,input等。
截止到python版本3.6.2 ,python一共提供了68个内置函数,具体如下
abs() dict() help() min() setattr()
all() dir() hex() next() slice()
any() divmod() id() object() sorted()
ascii() enumerate() input() oct() staticmethod()
bin() eval() int() open() str()
bool() exec() isinstance() ord() sum()
bytearray() filter() issubclass() pow() super()
bytes() float() iter() print() tuple()
callable() format() len() property() type()
chr() frozenset() list() range() vars()
classmethod() getattr() locals() repr() zip()
compile() globals() map() reversed() __import__()
complex() hasattr() max() round()
delattr() hash() memoryview() set()
本文将这68个内置函数综合整理为12大类,正在学习Python基础的读者一定不要错过,建议收藏学习!
和数字相关 1. 数据类型
bool : 布尔型(True,False)
int : 整型(整数)
float : 浮点型(小数)
complex : 复数
bin() 将给的参数转换成二进制
otc() 将给的参数转换成八进制
hex() 将给的参数转换成十六进制
print(hex(10)) # 十六进制:0xa
print(oct(10)) # 八进制:0o12
3. 数学运算
abs() 返回绝对值
divmode() 返回商和余数
round() 四舍五入
pow(a, b) 求a的b次幂, 如果有三个参数. 则求完次幂后对第三个数取余
sum() 求和
min() 求最小值
max() 求最大值
print(divmod(20,3)) # 求商和余数:(6,2)
print(round(4.50)) # 五舍六入:4
print(round(4.51)) #5
print(pow(10,2,3)) # 如果给了第三个参数. 表示最后取余:1
print(sum([1,2,3,4,5,6,7,8,9,10])) # 求和:55
print(min(5,3,9,12,7,2)) #求最小值:2
print(max(7,3,15,9,4,13)) #求最大值:15
和数据结构相关 1. 序列
(1)列表和元组
list() 将一个可迭代对象转换成列表
tuple() 将一个可迭代对象转换成元组
print(tuple([1,2,3,4,5,6])) #(1, 2, 3, 4, 5, 6)
(2)相关内置函数
reversed() 将一个序列翻转, 返回翻转序列的迭代器
slice() 列表的切片
it = reversed(lst) # 不会改变原列表. 返回一个迭代器, 设计上的一个规则
print(list(it)) #['啊', '好', '你']
lst = [1, 2, 3, 4, 5, 6, 7]
print(lst[1:3:1]) #[2,3]
s = slice(1, 3, 1) # 切片用的
print(lst[s]) #[2,3]
(3)字符串
str() 将数据转化成字符串
format() 与具体数据相关, 用于计算各种小数, 精算等.
s = "hello world!"
print(format(s, "^20")) #剧中
print(format(s, "<20")) #左对齐
print(format(s, ">20")) #右对齐
# hello world!
# hello world!
# hello world!
print(format(3, 'b' )) # 二进制:11
print(format(97, 'c' )) # 转换成unicode字符:a
print(format(11, 'd' )) # ⼗进制:11
print(format(11, 'o' )) # 八进制:13
print(format(11, 'x' )) # 十六进制(⼩写字母):b
print(format(11, 'X' )) # 十六进制(大写字母):B
print(format(11, 'n' )) # 和d⼀样:11
print(format(11)) # 和d⼀样:11
print(format(123456789, 'e' )) # 科学计数法. 默认保留6位小数:1.234568e+08
print(format(123456789, '0.2e' )) # 科学计数法. 保留2位小数(小写):1.23e+08
print(format(123456789, '0.2E' )) # 科学计数法. 保留2位小数(大写):1.23E+08
print(format(1.23456789, 'f' )) # 小数点计数法. 保留6位小数:1.234568
print(format(1.23456789, '0.2f' )) # 小数点计数法. 保留2位小数:1.23
print(format(1.23456789, '0.10f')) # 小数点计数法. 保留10位小数:1.2345678900
print(format(1.23456789e+3, 'F')) # 小数点计数法. 很大的时候输出INF:1234.567890
bytes() 把字符串转化成bytes类型
print(bs) #b'\xe4\xbb\x8a\xe5\xa4\xa9\xe5\x90\x83\xe9\xa5\xad\xe4\xba\x86\xe5\x90\x97'
bytearray() 返回一个新字节数组. 这个数字的元素是可变的, 并且每个元素的值得范围是[0,256)
ret = bytearray("alex" ,encoding ='utf-8')
print(ret[0]) #97
print(ret) #bytearray(b'alex')
ret[0] = 65 #把65的位置A赋值给ret[0]
print(str(ret)) #bytearray(b'Alex')
ord() 输入字符找带字符编码的位置
chr() 输入位置数字找出对应的字符
ascii() 是ascii码中的返回该值 不是就返回u
print(ord('中')) # '中'字在编码表中的位置:20013
print(chr(65)) # 已知码位,求字符是什么:A
print(chr(19999)) #丢
for i in range(65536): #打印出0到65535的字符
print(chr(i), end=" ")
print(ascii("@")) #'@'
repr() 返回一个对象的string形式
吃了%s顿饭" % 3
print(s)#今天# 吃了3顿 饭
print(repr(s)) # 原样输出,过滤掉转义字符
不管百分号%
#'今天
吃了3顿饭'
2. 数据集合
字典:dict 创建一个字典
集合:set 创建一个集合
frozenset() 创建一个冻结的集合,冻结的集合不能进行添加和删除操作。
3. 相关内置函数
len() 返回一个对象中的元素的个数
sorted() 对可迭代对象进行排序操作 (lamda)
语法:sorted(Iterable, key=函数(排序规则), reverse=False)
Iterable: 可迭代对象
key: 排序规则(排序函数), 在sorted内部会将可迭代对象中的每一个元素传递给这个函数的参数. 根据函数运算的结果进行排序
reverse: 是否是倒叙. True: 倒叙, False: 正序
lst.sort() # sort是list里面的一个方法
print(lst) #[1, 5, 5, 6, 7, 9, 12, 13, 18]
ll = sorted(lst) # 内置函数. 返回给你一个新列表 新列表是被排序的
print(ll) #[1, 5, 5, 6, 7, 9, 12, 13, 18]
l2 = sorted(lst,reverse=True) #倒序
print(l2) #[18, 13, 12, 9, 7, 6, 5, 5, 1]
#根据字符串长度给列表排序
lst = ['one', 'two', 'three', 'four', 'five', 'six']
def f(s):
return len(s)
l1 = sorted(lst, key=f, )
print(l1) #['one', 'two', 'six', 'four', 'five', 'three']
enumerate() 获取集合的枚举对象
for index, el in enumerate(lst,1): # 把索引和元素一起获取,索引默认从0开始. 可以更改
print(index)
print(el)
# 1
# one
# 2
# two
# 3
# three
# 4
# four
# 5
# five
all() 可迭代对象中全部是True, 结果才是True
any() 可迭代对象中有一个是True, 结果就是True
print(any([0,0,0,False,1,'good'])) #True
zip() 函数用于将可迭代的对象作为参数, 将对象中对应的元素打包成一个元组, 然后返回由这些元组组成的列表. 如果各个迭代器的元素个数不一致, 则返回列表长度与最短的对象相同
lst2 = ['醉乡民谣', '驴得水', '放牛班的春天', '美丽人生', '辩护人', '被嫌弃的松子的一生']
lst3 = ['美国', '中国', '法国', '意大利', '韩国', '日本']
print(zip(lst1, lst1, lst3)) #
for el in zip(lst1, lst2, lst3):
print(el)
# (1, '醉乡民谣', '美国')
# (2, '驴得水', '中国')
# (3, '放牛班的春天', '法国')
# (4, '美丽人生', '意大利')
# (5, '辩护人', '韩国')
# (6, '被嫌弃的松子的一生', '日本')
fiter() 过滤 (lamda)
语法:fiter(function. Iterable)
function: 用来筛选的函数. 在filter中会自动的把iterable中的元素传递给function. 然后根据function返回的True或者False来判断是否保留留此项数据 , Iterable: 可迭代对象
搜索公众号顶级架构师后台回复“面试”,送你一份惊喜礼包。
def func(i): # 判断奇数
return i % 2 == 1
lst = [1,2,3,4,5,6,7,8,9]
l1 = filter(func, lst) #l1是迭代器
print(l1) #
print(list(l1)) #[1, 3, 5, 7, 9]
map() 会根据提供的函数对指定序列列做映射(lamda)
语法 : map(function, iterable)
可以对可迭代对象中的每一个元素进行映射. 分别去执行 function
def f(i): return i
lst = [1,2,3,4,5,6,7,]
it = map(f, lst) # 把可迭代对象中的每一个元素传递给前面的函数进行处理. 处理的结果会返回成迭代器print(list(it)) #[1, 2, 3, 4, 5, 6, 7]
和作用域相关
locals() 返回当前作用域中的名字
globals() 返回全局作用域中的名字
a = 10
print(locals()) # 当前作用域中的内容
print(globals()) # 全局作用域中的内容
print("今天内容很多")
func()
# {'a': 10}
# {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__':
# <_frozen_importlib_external.SourceFileLoader object at 0x0000026F8D566080>,
# '__spec__': None, '__annotations__': {}, '__builtins__':
# (built-in)>, '__file__': 'D:/pycharm/练习/week03/new14.py', '__cached__': None,
# 'func': }
# 今天内容很多
和迭代器生成器相关
range() 生成数据
next() 迭代器向下执行一次, 内部实际使⽤用了__ next__()⽅方法返回迭代器的下一个项目
iter() 获取迭代器, 内部实际使用的是__ iter__()⽅方法来获取迭代器
print(i)
# 15
# 10
# 5
# 0
lst = [1,2,3,4,5]
it = iter(lst) # __iter__()获得迭代器
print(it.__next__()) #1
print(next(it)) #2 __next__()
print(next(it)) #3
print(next(it)) #4
字符串类型代码的执行
eval() 执行字符串类型的代码. 并返回最终结果
exec() 执行字符串类型的代码
compile() 将字符串类型的代码编码. 代码对象能够通过exec语句来执行或者eval()进行求值
print(eval(s1)) # 17 可以动态的执行代码. 代码必须有返回值
s2 = "for i in range(5): print(i)"
a = exec(s2) # exec 执行代码不返回任何内容
# 0
# 1
# 2
# 3
# 4
print(a) #None
# 动态执行代码
exec("""
def func():
print(" 我是周杰伦")
""" )
func() #我是周杰伦
code1 = "for i in range(3): print(i)"
com = compile(code1, "", mode="exec") # compile并不会执行你的代码.只是编译
exec(com) # 执行编译的结果
# 0
# 1
# 2
code2 = "5+6+7"
com2 = compile(code2, "", mode="eval")
print(eval(com2)) # 18
code3 = "name = input('请输入你的名字:')" #输入:hello
com3 = compile(code3, "", mode="single")
exec(com3)
print(name) #hello
输入输出
print() : 打印输出
input() : 获取用户输出的内容
#hello*world@
内存相关
hash() : 获取到对象的哈希值(int, str, bool, tuple). hash算法:(1) 目的是唯一性 (2) dict 查找效率非常高, hash表.用空间换的时间 比较耗费内存
s = 'alex'print(hash(s)) #-168324845050430382lst = [1, 2, 3, 4, 5]print(hash(lst)) #报错,列表是不可哈希的 id() : 获取到对象的内存地址s = 'alex'print(id(s)) #2278345368944
文件操作相关
open() : 用于打开一个文件, 创建一个文件句柄
f.read()
f.close()
模块相关
__ import__() : 用于动态加载类和函数
import os
name = input("请输入你要导入的模块:")
__import__(name) # 可以动态导入模块
帮 助
help() : 函数用于查看函数或模块用途的详细说明
调用相关
callable() : 用于检查一个对象是否是可调用的. 如果返回True, object有可能调用失败, 但如果返回False. 那调用绝对不会成功
print(callable(a)) #False 变量a不能被调用
def f():
print("hello")
print(callable(f)) # True 函数是可以被调用的
查看内置属性
dir() : 查看对象的内置属性, 访问的是对象中的__dir__()方法
你还有什么想要补充的吗?
免责声明:本文内容来源于网络,文章版权归原作者所有,意在传播相关技术知识&行业趋势,供大家学习交流,若涉及作品版权问题,请联系删除或授权事宜。
技术君个人微信
添加技术君个人微信即送一份惊喜大礼包
→ 技术资料共享
→ 技术交流社群
--END--
往日热文:
Python程序员深度学习的“四大名著”:
这四本书着实很不错!我们都知道现在机器学习、深度学习的资料太多了,面对海量资源,往往陷入到“无从下手”的困惑出境。而且并非所有的书籍都是优质资源,浪费大量的时间是得不偿失的。给大家推荐这几本好书并做简单介绍。
获得方式:
2.后台回复关键词:名著
字典内置函数&方法
Python字典包含了以下内置函数:
1 cmp(dict1, dict2)
比较两个字典元素。
2 len(dict)
计算字典元素个数,即键的总数。
3 str(dict)
输出字典可打印的字符串表示。
4 type(variable)
返回输入的变量类型,如果变量是字典就返回字典类型。
绛旓細Python瑙i噴鍣ㄥ唴缃簡璁稿鍑芥暟锛岃繖鎰忓懗鐫鎴戜滑鏃犻渶瀹氫箟锛屽缁堝彲浠ュ畠浠備笅闈㈡寜鐓у嚱鏁扮殑瀛楁瘝椤哄簭锛岃璁轰竴浜涘父鐢鐨勫唴寤哄嚱鏁銆俥val()eval()鍑芥暟瑙f瀽浼犵粰瀹冪殑琛ㄨ揪寮忥紝骞跺湪绋嬪簭涓繍琛孭ython琛ㄨ揪寮(浠g爜)銆備妇涓緥瀛愶細>>> x = 1 >>> eval("x + 1") # 娉ㄦ剰锛"x + 1"鏄瓧绗︿覆 2 >>> eval("4 < 9...
绛旓細words = line.split(" ")璇诲叆涓琛屽瓧绗ine 浠ョ┖鏍尖 鈥濆垎闅旇瘝 杩斿洖涓鍫嗗崟璇嶅垪琛╨ist
绛旓細Python瑙i噴鍣ㄥ唴缃簡璁稿鍑芥暟锛岃繖鎰忓懗鐫鎴戜滑鏃犻渶瀹氫箟锛屽缁堝彲浠ュ畠浠傛帴涓嬫潵鍜屽ぇ瀹朵竴璧疯璁轰竴涓父鐢鐨勫唴寤哄嚱鏁-input()鍜宨sinstance()銆俰nput()input()鍑芥暟璇诲彇鐢ㄦ埛杈撳叆锛屽苟杞崲鎴愬瓧绗︿覆锛>>> a = input() # 灏唅nput()杩斿洖鐨勫艰祴鍊肩粰a Python >>> a # 鏌ョ湅a鐨勫硷紙涓哄瓧绗︿覆'Python'锛'Pytho...
绛旓細锛歝allable()callable()鍑芥暟鍒ゆ柇鍙傛暟鏄惁鍙皟鐢紝濡傛灉鍙皟鐢紝鍒欒繑鍥濼rue锛屽惁鍒欎細杩斿洖False銆備妇涓緥瀛愶細鑷畾涔夌殑绫荤殑瀵硅薄锛屼竴鑸儏鍐典笅鏄笉鑳借皟鐢ㄧ殑锛氫絾鍙互閫氳繃瀹氫箟__call__()鏂规硶鏉ヤ娇瀵硅薄鍙樺緱鍙互璋冪敤锛氬叧浜Python鐨鍩虹闂鍙互鐪嬩笅杩欎釜缃戦〉鐨勮棰戞暀绋嬶紝Python鍩虹甯歌闂锛屽笇鏈涙垜鐨勫洖绛旇兘甯埌浣犮
绛旓細globals锛氳繖涓弬鏁版帶鍒剁殑鏄竴涓叏灞鐨勫懡鍚嶇┖闂达紝涔熷氨鏄垜浠湪璁$畻琛ㄨ揪寮忕殑鏃跺欏彲浠ヤ娇鐢ㄥ叏灞鐨勫懡鍚嶇┖闂翠腑鐨鍑芥暟锛屽鏋滆繖涓弬鏁拌鎻愪緵浜嗐傚苟涓旀病鏈夋彁渚涜嚜瀹氫箟鐨刡uiltins(python鐨勫唴寤妯″潡)锛岄偅涔堜細灏嗗綋鍓嶇幆澧冧腑鐨刡uiltins鎷疯礉鍒版彁渚涚殑globals閲岋紝鐒跺悗杩涜璁$畻銆傚鏋済lobals娌℃湁琚彁渚涳紝鍒浣跨敤python鐨鍏ㄥ眬鍛藉悕绌洪棿...
绛旓細writer = open(鈥/tmp.log', 'a', encoding='utf-8') # 浠ヨ拷鍔犳ā寮忔墦寮tmp.log鏂囦欢锛岀紪鐮佷负utf-8 writer.write('寮濮嬪鐞...') # 鍐欏叆鍐呭 writer.write('%(tile)s %(author)s %(size)d %(summary)s' % book)writer.write('澶勭悊瀹屾瘯')writer.flush() # 鍒锋柊缂撳瓨 writer.close(...
绛旓細python鐨勫唴寤哄嚱鏁鍗虫槸python鑷甫鐨勫嚱鏁帮紝杩欑鍑芥暟涓嶉渶瑕佸畾涔夛紝骞朵笖涓嶅悓鐨勫唴寤哄嚱鏁板叿鏈変笉鍚岀殑鍔熻兘锛屽彲浠ョ洿鎺浣跨敤銆備互涓嬫槸閮ㄥ垎鍐呭缓鍑芥暟鐢ㄦ硶鍙婅鏄 1銆乤bs()锛岃繑鍥炴暟瀛楃殑缁濆鍊笺2銆乤ll()锛屽鏋滈泦鍚堜腑鎵鏈夊厓绱犳槸true鎴栭泦鍚堜负绌洪泦鍚堬紝杩斿洖True銆3銆乤ny()锛屽鏋滈泦鍚堜腑鏈変竴椤瑰厓绱犳槸true锛岃繑鍥濼rue锛涚┖闆嗗悎涓...
绛旓細python涓鍙互浣跨敤dir(__builtins__)鏂规硶鏌ョ湅鎵鏈鍐呭缓鍑芥暟銆
绛旓細Python3甯哥敤鍐呯疆鍑芥暟鎬荤粨 2018-11-01 python 鏈甯哥敤鐨勫唴寤哄嚱鏁銆 2018-08-06 Python瀛楀吀涓鍑犱釜甯哥敤鍑芥暟鎬荤粨 1 2020-08-11 python鐨鍐呯疆鍑芥暟鏈夊摢浜,閮芥槸浠涔堟剰鎬? 1 2016-12-16 python 鍒嗙被姹囨诲嚱鏁版湁鍝簺 1 2020-10-14 python 楂橀樁鍑芥暟鏈夊摢浜 ...
绛旓細濡傛灉浣浣跨敤杩囧叾浠栬瑷锛屼綘鍙兘浼氬弽瀵硅锛歠ib 涓嶆槸涓涓嚱鏁帮紝鑰屾槸涓涓柟娉曪紝鍥犱负瀹冨苟涓嶈繑鍥炰换浣曞笺備簨瀹炰笂锛屾病鏈 return 璇彞鐨勫嚱鏁纭疄浼氳繑鍥炰竴涓硷紝铏界劧鏄竴涓浉褰撲护浜哄帉鐑︾殑鍊硷紙鎸 None 锛夈傝繖涓艰绉颁负 None 锛堣繖鏄竴涓鍐呭缓鍚嶇О锛夈傚鏋 None 鍊兼槸鍞竴琚功鍐欑殑鍊硷紝閭d箞鍦ㄥ啓鐨勬椂鍊欓氬父浼氳...