您好,欢迎访问三七文档
当前位置:首页 > IT计算机/网络 > AI人工智能 > python函数中文手册
内置函数一,文档说明原始文档来自于pythonv2.7.2中文译文和用法尚不完全,您可以自由修改和完善,您可以在文档结尾鸣谢添上您的名字,我们将会感谢您做的贡献!二,函数列表1,取绝对值abs(x)Returntheabsolutevalueofanumber.Theargumentmaybeaplainorlongintegerorafloatingpointnumber.Iftheargumentisacomplexnumber,itsmagnitudeisreturned.如果你不知道绝对值什么意思,那就要补一下小学数学了!基本用法2,all(iterable)ReturnTrueifallelementsoftheiterablearetrue(oriftheiterableisempty).Equivalentto:3.any(iterable)ReturnTrueifanyelementoftheiterableistrue.Iftheiterableisempty,returnFalse.Equivalentto:4.basestring()Thisabstracttypeisthesuperclassforstrandunicode.Itcannotbecalledorinstantiated,butitcanbeusedtotestwhetheranobjectisaninstanceofstrorunicode.isinstance(obj,basestring)isequivalenttoisinstance(obj,(str,unicode)).是字符串和字符编码的超类,是抽象类型。不能被调用或者实例化。可以用来判断实例是否为字符串或者字符编码。方法:5.二进制转换bin(x)Convertanintegernumbertoabinarystring.TheresultisavalidPythonexpression.IfxisnotaPythonintobject,ithastodefinean__index__()methodthatreturnsaninteger.转换成二进制表达方法:6.布尔类型bool([x])ConvertavaluetoaBoolean,usingthestandardtruthtestingprocedure.Ifxisfalseoromitted,thisreturnsFalse;otherwiseitreturnsTrue.boolisalsoaclass,whichisasubclassofint.Classboolcannotbesubclassedfurther.ItsonlyinstancesareFalseandTrue布尔类型的转化用法:7.二进制数组的转化bytearray([source[,encoding[,errors]]])Returnanewarrayofbytes.Thebytearraytypeisamutablesequenceofintegersintherange0=x256.Ithasmostoftheusualmethodsofmutablesequences,describedinMutableSequenceTypes,aswellasmostmethodsthatthestrtypehas,seeStringMethods.Theoptionalsourceparametercanbeusedtoinitializethearrayinafewdifferentways:Ifitisastring,youmustalsogivetheencoding(andoptionally,errors)parameters;bytearray()thenconvertsthestringtobytesusingstr.encode().Ifitisaninteger,thearraywillhavethatsizeandwillbeinitializedwithnullbytes.Ifitisanobjectconformingtothebufferinterface,aread-onlybufferoftheobjectwillbeusedtoinitializethebytesarray.Ifitisaniterable,itmustbeaniterableofintegersintherange0=x256,whichareusedastheinitialcontentsofthearray.Withoutanargument,anarrayofsize0iscreated.8.callable(object)ReturnTrueiftheobjectargumentappearscallable,Falseifnot.Ifthisreturnstrue,itisstillpossiblethatacallfails,butifitisfalse,callingobjectwillneversucceed.Notethatclassesarecallable(callingaclassreturnsanewinstance);classinstancesarecallableiftheyhavea__call__()method.9.数字转化成字符chr(i)ReturnastringofonecharacterwhoseASCIIcodeistheintegeri.Forexample,chr(97)returnsthestring'a'.Thisistheinverseoford().Theargumentmustbeintherange[0..255],inclusive;ValueErrorwillberaisedifiisoutsidethatrange.Seealsounichr().用法:10.classmethod(function)Returnaclassmethodforfunction.Aclassmethodreceivestheclassasimplicitfirstargument,justlikeaninstancemethodreceivestheinstance.Todeclareaclassmethod,usethisidiom:11.两两比较cmp(x,y)Comparethetwoobjectsxandyandreturnanintegeraccordingtotheoutcome.Thereturnvalueisnegativeifxy,zeroifx==yandstrictlypositiveifxy.X小于X输出负(-1),X等于Y输出零(0),X大于Y输出正(1)用法:12.compile(source,filename,mode[,flags[,dont_inherit]])CompilethesourceintoacodeorASTobject.Codeobjectscanbeexecutedbyanexecstatementorevaluatedbyacalltoeval().sourcecaneitherbeastringoranASTobject.RefertotheastmoduledocumentationforinformationonhowtoworkwithASTobjects.13.complex([real[,imag]])Createacomplexnumberwiththevaluereal+imag*jorconvertastringornumbertoacomplexnumber.Ifthefirstparameterisastring,itwillbeinterpretedasacomplexnumberandthefunctionmustbecalledwithoutasecondparameter.Thesecondparametercanneverbeastring.Eachargumentmaybeanynumerictype(includingcomplex).Ifimagisomitted,itdefaultstozeroandthefunctionservesasanumericconversionfunctionlikeint(),long()andfloat().Ifbothargumentsareomitted,returns0j.14.delattr(object,name)Thisisarelativeofsetattr().Theargumentsareanobjectandastring.Thestringmustbethenameofoneoftheobject’sattributes.Thefunctiondeletesthenamedattribute,providedtheobjectallowsit.Forexample,delattr(x,'foobar')isequivalenttodelx.foobar.15.字典dict([arg])Createanewdatadictionary,optionallywithitemstakenfromarg.ThedictionarytypeisdescribedinMappingTypes—dict.Forothercontainersseethebuiltinlist,set,andtupleclasses,andthecollectionsmodule.16.很重要的函数,属性输出dir([object])Withoutarguments,returnthelistofnamesinthecurrentlocalscope.Withanargument,attempttoreturnalistofvalidattributesforthatobject.方法17.divmod(a,b)Taketwo(noncomplex)numbersasargumentsandreturnapairofnumbersconsistingoftheirquotientandremainderwhenusinglongdivision.Withmixedoperandtypes,therulesforbinaryarithmeticoperatorsapply.Forplainandlongintegers,theresultisthesameas(a//b,a%b).Forfloatingpointnumberstheresultis(q,a%b),whereqisusuallymath.floor(a/b)butmaybe1lessthanthat.Inanycaseq*b+a%bisveryclosetoa,ifa%bisnon-zeroithasthesamesignasb,and0=abs(a%b)abs(b).18.enumerate(sequence[,start=0])Returnanenumerateobject.sequencemustbeasequence,aniterator,orsomeotherobjectwhichsupportsiteration.Thenext()methodoftheiteratorreturnedbyenumerate()returnsatuplecontainingacount(fromstartwhichdefaultsto0)andthecorrespondingvalueobtainedfromiteratingoveriterable.enumerate()isusefulforobtaininganindexedseries:(0,seq[0]),(1,seq[1]),
本文标题:python函数中文手册
链接地址:https://www.777doc.com/doc-4209979 .html