编程札记

P

Python3 数字Number Ceil 方法

Python3 数字Number Ceil 方法 #!/usr/bin/python3 import math # This will import math module print ("math.ceil(-45.17) : ", math.ceil(-45.17)) print ("math.ceil(100.12) : ", math.ceil(100.12)) print ("math.ceil(100.72) : ", math.ceil(100.72)) print ("math.ceil(math.pi) : ", math.ceil(math.pi))

阅读全文 ↗
P

Python3 Pass 语句

Python3 Pass 语句 #!/usr/bin/python3 for letter in 'Python': if letter == 'h': pass print ('This is pass block') print ('Current Letter :', letter) print ("Good bye!")

阅读全文 ↗
P

Python3 Continue 语句

Python3 Continue 语句 #!/usr/bin/python3 for letter in 'Python': # First Example if letter == 'h': continue print ('Current Letter :', letter) var = 10 # Second Example while var > 0: var = var -1 if var == 5: continue print ('Current variable value :', var) print ("Good bye!")

阅读全文 ↗
P

Python3 Break 语句

Python3 Break 语句 #!/usr/bin/python3 for letter in 'Python': # First Example if letter == 'h': break print ('Current Letter :', letter) var = 10 # Second Example while var > 0: print ('Current variable value :', var) var = var -1 if var == 5: break print ("Good bye!")

阅读全文 ↗
P

Python3 算术运算符

Python3 算术运算符 #!/usr/bin/python3 a = 21 b = 10 c = 0 c = a + b print ("Line 1 - Value of c is ", c) c = a - b print ("Line 2 - Value of c is ", c ) c = a * b print ("Line 3 - Value of c is ", c) c = a / b print ("Line 4 - Value of c is ", c ) c = a % b print ("Line 5 - Value of c is ", c) a = 2 b = 3 c = a**b print ("Line 6 - Value of c is ", c) a = 10 b = 5 c = a//b print ("Line 7 - Value of c is ", c)

阅读全文 ↗
P

Python3 字典 cmp 方法

Python3 字典 cmp 方法 #!/usr/bin/python3 dict1 = {'Name': 'Zara', 'Age': 7}; dict2 = {'Name': 'Mahnaz', 'Age': 27}; dict3 = {'Name': 'Abid', 'Age': 27}; dict4 = {'Name': 'Zara', 'Age': 7}; print ("Return Value : %d" % cmp (dict1, dict2)) print ("Return Value : %d" % cmp (dict2, dict3)) print ("Return Value : %d" % cmp (dict1, dict4))

阅读全文 ↗
P

Python3 删除元组

Python3 删除元组 #!/usr/bin/python3 tup = ('physics', 'chemistry', 1997, 2000); print (tup) del tup; print ( "After deleting tup : ") print (tup)

阅读全文 ↗
P

Python3 使用Dom解析 xml

Python3 使用Dom解析 xml #!/usr/bin/python3 from xml.dom.minidom import parse import xml.dom.minidom # Open XML document using minidom parser DOMTree = xml.dom.minidom.parse("movies.xml") collection = DOMTree.documentElement if collection.hasAttribute("shelf"): print ("Root element : %s" % collection.getAttribute("shelf")) # Get all the movies in the collection movies = collection.getElementsByTagName("movie") # Print detail of each movie. for movie in movies: print ("*****Movie*****") if movie.hasAttribute("title"): print ("Title: %s" % movie.getAttribute("title")) type = movie.getElementsByTagName('type')[0] print ("Type: %s" % type.childNodes[0].data) format = movie.getElementsByTagName('format')[0] print ("Format: %s" % format.childNodes[0].data) rating = movie.getElementsByTagName('rating')[0] print ("Rating: %s" % rating.childNodes[0].data) description = movie.getElementsByTagName('description')[0] print ("Description: %s" % description.childNodes[0].data)

阅读全文 ↗
P

Python3 xml Parsestring 方法

Python3 xml Parsestring 方法 #!/usr/bin/python3 import xml.sax class MovieHandler( xml.sax.ContentHandler ): def __init__(self): self.CurrentData = "" self.type = "" self.format = "" self.year = "" self.rating = "" self.stars = "" self.description = "" # Call when an element starts def startElement(self, tag, attributes): self.CurrentData = tag if tag == "movie": print ("*****Movie*****") title = attributes["title"] print ("Title:", title) # Call when an elements ends def endElement(self, tag): if self.CurrentData == "type": print ("Type:", self.type) elif self.CurrentData == "format": print ("Format:", self.format) elif self.CurrentData == "year": print ("Year:", self.year) elif self.CurrentData == "rating": print ("Rating:", self.rating) elif self.CurrentData == "stars": print ("Stars:", self.stars) elif self.CurrentData == "description": print ("Description:", self.description) self.CurrentData = "" # Call when a character is read def characters(self, content): if self.CurrentData ==

阅读全文 ↗
P

Python3 cgi 环境

Python3 cgi 环境 #!/usr/bin/python import os print ("Content-type: text/html\r\n\r\n"); print ("<font size=+1>Environment</font><\br>"); for param in os.environ.keys(): print ("<b>%20s</b>: %s<\br>" % (param, os.environ[param]))

阅读全文 ↗