Note: The print print statement in python3.3.5 now has a new wording:
1. Python specifies that random numbers are generated
Import Random # correctly test print notation >>> rand = random.randint (1,500)print (rand)# Wrong print syntax:print randsyntaxerror:invalid syntax
2. #python对文件操作
1. Generate random NumbersImportRandom#This is a note that introduces the moduleRnd = Random.randint (1,500)#generate a random number between 1-5002. Read the file F= Open ("C:\\1.txt","R") Lines= F.readlines ()#Read all content forLineinchLinesPrint Line3. write file F= Open ("C:\\1.txt","r+")#Readable writable modeF.write ("123")#Write String4. Regular expressions, reading Tomcat logs and printing datesImportre regx="\d\d\d\d-\d\d-\d+"F= Open ("C:\stdout.log","R") I=0 forStrinchf.readlines ():ifRe.search (REGX,STR): Response.Write (str+"<br>") ifI>10: Break#because it is a test, only 10 rows are analyzedI=i+1f.close ();5. Connect to the databaseImportPgdb Conn=Pgdb.connect (Host='localhost', databse='Qingfeng', user='Qingfeng', password='123') cur=conn.cursor () Cur.execute ("select * FROM Dream") PrintCur.rowcount6. Sax Processes xml:Importstring fromXml.saxImportSaxlib, SaxextsclassQuotationhandler (saxlib. Handlerbase):"""Crude sax Extractor for QUOTATIONS.DTD document""" def __init__(self): Self.in_quote=0 Self.thisquote="' defstartdocument (self):Print '---Begin Document---' defstartelement (self, Name, attrs):ifName = ='Quotation': Print 'Quotation:'Self.in_quote= 1Else: Self.thisquote= Self.thisquote +'{' defendElement (self, name):ifName = ='Quotation': PrintString.Join (String.Split (self.thisquote[:230)) +'...', Print '('+str (Len (self.thisquote)) +'bytes) \ n'Self.thisquote="'Self.in_quote=0Else: Self.thisquote= Self.thisquote +'}' defcharacters (self, ch, start, length):ifSelf.in_quote:self.thisquote= Self.thisquote + ch[start:start+Length]if __name__=='__main__': Parser=saxexts. Xmlparserfactory.make_parser () handler=Quotationhandler () Parser.setdocumenthandler (handler) parser.parsefile (open ("Sample.xml") ) Parser.close ()7The GUI module standard for Python is Tkinter, also has Qt and MFC modules, which are interested in everyone's own search underImportTkinter Root=tkinter.tk () my=label (Root,"Welcome to Python 's world") My.pack () Root.mainloop ()
Python important block of function code