【黑马程序员】Python编程学习之常用标准库_第1页
【黑马程序员】Python编程学习之常用标准库_第2页
【黑马程序员】Python编程学习之常用标准库_第3页
【黑马程序员】Python编程学习之常用标准库_第4页
【黑马程序员】Python编程学习之常用标准库_第5页
已阅读5页,还剩17页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

+Q1679806262+Q1679806262黑马程序]Pywwwjtheima,com卜传喈播程学下高端IT教育品牌Python/course/273.html71912sxkqqhttps://pan.baidu.eom/s/12-dymJ8FjWzh6b5NKJuP3Qgqlystr='str[0]strstr[3]strstr[0:3]str.find()str='str[0]strstr[3]strstr[0:3]str.find()在则返回字符串首字str.index()在则返回字符串首字str.count()len(str)0。str.replace('串中指定字符数据的str.split("",切割数据信息;如果切割str.capitalize():str.startswith(串中下标为0的串中下标为3的串中从下标为0strstrstrstrII数):进行指定字符str字符串数据):返回boolen-1strstr否以以指指定d黑吗程序员否以以指指定d黑吗程序员传回booien下高端IT教育品牌str——str.lower()str.upper()str.ljust(的字符串长度):返回一个字符串,将strstr充操作。str.rjust(度):返strstr充操作。str.center(长度):strstrstr.lstrip()空格。str.rstrip()空格。str.rfind()findstr.rindex()符的下标。indexstrstr.partition(标。串):从strstr.rpartition(串):下空字符。partitionstr.splitlines():。的。str.isdigit()字。boolenstrstr.isalpha()boolenstrstr.isalnum()boolenstr空格格。。^黑马程boo员wwwJtheima,comstr.isupper()boolen传智播客旗不高端IT教育品牌-str-str.islower()boolenstr写。strmystr.join(str)mystr作;返回拼接后的字符串数据信息。strprint("%0.1f",float(result*1.0))的数据print("%d*%d=%d"%(j,i,i*j),end=''):结束最后的2、文件流文件流对应的基础操作:f=open('','')iffboolen开。w果该文件不存在,则创建一个新的文件。r默认模式。arb:wbab:传智播客旗下传智播客旗下高端文件教育品牌传智播客旗下传智播客旗下高端文件教育品牌+Q1679806262+Q1679806262黑马程序员r+ww.itheim文件omw+a+rb+:wb+ab+f.close()f.wirte()f.read()readf.readline()f.readlines()素为一行数据的信息。f.tell()seek(offset,from)from0:表示文件的开头。1:表示文件的当前位置。2:表示文件的末尾。offset测试f.seek(5,0)5传传智播移3s下高端IT教育品牌传传智播移3s下高端IT教育品牌+Q1679806262+Q1679806262黑河黑河程序员wwwJ1、列表:strList=['']str=''newStr=''strList.appendstrList[0]=newStrstrinstrLsitboolenforiteminstrListitemdelstrList[0]strList.pop()strList.remove('')2、字典:Keyinfor['age']=18delinfor.clear3、字典的常见操作dic={1:'xiaohao',2:'xiaodong'}dic.keys()dic.valuses()dic.items()组,分别存放着键和值。dic.has_key(1)boolen4、元组:tuple=(1,2,3,4,5)+Q1679806262+Q16798062621黑马程序员1e[-2:theima.8m传智播客旗下高端1丁教育品牌tuple[1:]cmp(tuple1,tuple2)max(tuple)min(tuple)tuple(seq)5、运算符操作列表、字典、11en((1,2,3,4))2、(1,2,3,4)+(1,2,3,4,5)3['Hi']*4boolen4、1in(1,2,3,4):判断元boolen4、异常捕获定义:某些错误操作导致程序无法正常运行异常捕获:try:except(NameError,IOError),result:print(result)#else:print("else")finally:print("finally")range1、help(range):range(stop)->listofintegersrange(start,stop[,step])->listofintegers2、参数分析:start:start0range53,4]5e(0黑马程序员W.itStOpr结束,但传智播客旗下高磷op教育电相e05[0,1,2,1range051range05range(0,5,1)。3、示教:a=range(5)list(a)map1、help(map):map(...)map(function,sequence[,sequence,...])->list2、参数分析:function:sequence:,functionlist3、语法:functionfunctionlist4、示教函数需要一个参数map(lambdax:x*x,[1,2,3])[1,4,9]函数需要两个参数map(lambdax,y:x+y,[1,2,3],[4,5,6])[5,7,9]None,map(None,[1,3,5,7,9],[2,4,6,8,10])

#2),⑶4),(5,6),(7,8),(9,10)]百客旗下wwwJtheima,com।高端IT教育品牌#Nonemap(None,[1,3,5,7,9],[2,4,6])[(1,2),(3,4),(5,6),(7,None),(9,None)]filter1、help(filter):filter(...)filter(functionorNone,sequence)->list,tuple,orstringReturnthoseitemsofsequenceforwhichfunction(item)istrue.IffunctionisNone,returntheitemsthataretrue.Ifsequenceisatupleorstring,returnthesametype,elsereturnalist.2、参数分析:function:TrueFalsesequence:strtuplelist3、语法filtersequencefunctionTruesequence类型相同4、示例filter(lambdax:x%2,[1,2,3,4])[1,3]filter(None,"she")'she'reduce1、help(reduce):

ce(…)ce(…)u传智播客旗下Lnce新niyM育弼乳Applyafunctionoftwoargumentscumulativelytotheitemsofasequence,fromlefttoright,soastoreducethesequencetoasinglevalue.Forexample,reduce(lambdax,y:x+y,[1,2,3,4,5])calculates((((1+2)+3)+4)+5).Ifinitialispresent,itisplacedbeforetheitemsofthesequenceinthecalculation,andservesasadefaultwhenthesequenceisempty.2、参数分析:function:sequence:strtuplelistinitial:3、语法reducesequencefunctionfunctionfunctioninitialsequenceinitialfunctionsequencefunctionfunctionNone4、示例reduce(lambdax,y:x+y,[1,2,3,4])10reduce(lambdax,y:x+y,[1,2,3,4],5)15reduce(lambdax,y:x+y,['aa','bb','cc'],'dd')传智传智播客旗下python高端IT教育品牌传智传智播客旗下python高端IT教育品牌+Q1679806262+Q1679806262(fX'ddaabbcc'wwwJ9sorted1、help(sorted):sorted(...)sorted(iterable,cmp=None,key=None,reverse=False)-->newsortedlist2、参数分析:cmpx<y返回-1x>y返回1x==y返回03、示例defcmp_ignore_case(s1,s2):u1=s1.upper()u2=s2.upper()ifu1<u2:return-1ifu1>u2:return1return1print()raw_input()2、用户自定义函数:如果系统中,3ipythondir()幺簿黑马程序员^^^名.ahitheim获.combuilinsoswidows'nt'Linux'posixTrueFalseTrueFalsewidows'nt'Linux'posixTrueFalseTrueFalseTrueFalseos.system()shellos.chdir()os.path.getsize()os.path.abspath()ospath.join(path,name)ospath.basename(path)ospath.dirname(path):sys:Python传智传智播客旗下python高端IT教育品牌st')st')智播客旗下高端IT教育品牌+Q1679806262+Q1679806262传mport黑马程序员sys.stdinsys.argvfunctools:1、概念:functoolspython2.52、操作:1、importfunctools:2、dir(functools):查看partial1、概念:2、示例:importfunctoolsdefshowarg(*args,**kw):print(args)print(kw)p1=functools.partial(showarg,1,2,3)p1()p1(4,5,6)p1(a='python',b='itcast')p2=functools.partial(showarg,a=3,b='linux')p2()p2(1,2)IGp2(a='Python',b='^wwwJtheima,com।wraps1、概念:Pythonfunctoolswrapsimportfunctoolsdefnote(func):"notefunction"@functools.wraps(func)defwrapper():"wrapperfunction"print('notesomething')returnfunc()returnwrapper@notedeftest():"testfunction"print('Iamtest')test()print(test.__doc__)jsonJSONjsonlogging黑马程序员wwwJfork/multiprocessingimportos1、pid=os.fork()os.fork()fork()的idfock()Unix/Linux/Mac0,而父进程中windowsimportosforkUnix/Linux/Macwindowspid=os.fork()ifpid==0:print('else:print('1')2')os.getpid():os.getppid()PIDPID2、进程创建的第二种方式:frommultiprocessingimportProcessp=Process(target=run_proc,args=('test',))

targetProcessargsp.start()传传可播客旗下高端IT教育品牌传传可播客旗下高端IT教育品牌+Q1679806262+Q1679806262)黑马程序员火程间的同cpumultiprocessing.cpu_count()cpupipe=multiprocessing.Pipe()pipe[0]pipe[1]pipe[0].send()pipe[0].recv()3、frommultiprocessingimportProcess,Queueq=Queue()q.put()q.get(True)q.empty()threadingthreadimportthreadthread.start_new_thread(defName,())参数为新线程要执行的方法,第二个参数为元组传递执行方法对python(,)''thread.exit_thread()exit.thread()thread.join()pythonThreadjoin()线程可播等待另一个线程执行结束后再继续运行。这个方法还timeout传智播客旗下传智播客旗下高端1丁教育品牌传智播客旗下传智播客旗下高端1丁教育品牌+Q1679806262+Q1679806262黑马程序员ad.setDaemon(True)threadingthread装,可以更方便我们的使用。importthreadingmyThread=threading.Thread(target=defName,args=('Alice',))targetargsmyThread.start()num=len(threading.enumerate)threading.Theaddefrun(self):classMyThread(threading.Thread)进行基类构造函数的创建操作def__init__(self,name,time):threading.Thread.__init__(self,name='')myThread=MyThread()myThread.start()threadingLock()mutex=threading.Lock()ifmutex.acquire([blocking])trueacquire。blockingblockingTrueTrueblockingFalseea程序员ea程序员wwwJ传智播客旗的高端IT教育品牌ea程序员ea程序员wwwJ传智播客旗的高端IT教育品牌+Q1679806262+Q16798062625pythonRLockLockcountercounteracquire

5pythonRLockLockcountercounteracquire

acquireacquirereleasemutex=threading.RLock()mutex.acquire()mutex.release()6pythonConditionConditionConditionwaitingcon=threading.Condition()Lock/RLockRLockcon.acquire()con.wait()Conditionblockedwaitingcon.notify()Conditionwaitingacquirecon.notifyAll():Condition对象通知waiting池中所有的线程acquirecon.release()threading.EventFalsewait()set()TrueEventisSet()Eventevent=threading.Event()Enventdef__init__(self,threadName,event):self.threadEvent=event:重构对应threading.Threadinit的方法。传智传智播客旗下中端待的教育品牌传智传智播客旗下中端待的教育品牌+Q1679806262+Q1679806262©t黑adE程序受他set():启动超位哂QueuefromQueueimportQueuequeue=Queue()queue.qsize()queue.put()queue.set()queue.empty()copytimedatetimeTOC\o"1-5"\h\zcalendar——hashlib-randomimportrandomrandom.random()01传传智播获旗下翥之间教育品牌传传智播获旗下翥之间教育品牌+Q1679806262+Q1679806262伤呼嘿画程序员57ondOmirandint(10.50):r0到TOC\o"1-5"\h

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论