上一页 1 ··· 5 6 7 8 9 10 11 12 13 ··· 15 下一页
摘要: >>> def powersum(power, *args):... '''Return the sum of each argument raised to specified power.'''... total = 0... for i in args:... total += pow(i, 阅读全文
posted @ 2017-10-20 09:48 真勇士王小山 阅读(122) 评论(0) 推荐(0) 编辑
摘要: #!/usr/bin/python# Filename: list_comprehension.pylistone = [2, 3, 4]listtwo = [2*i for i in listone if i > 2]print listtwo 这里我们为满足条件(if i > 2)的数指定了一个 阅读全文
posted @ 2017-10-20 09:45 真勇士王小山 阅读(98) 评论(0) 推荐(0) 编辑
摘要: os模块 这个模块包含普遍的操作系统功能。如果你希望你的程序能够与平台无关的话,这个模块是尤为重要的。即它允许一个程序在编写后不需要任何改动,也不会发生任何问题,就可以在Linux和Windows下运行。一个例子就是使用os.sep可以取代操作系统特定的路径分割符。 下面列出了一些在os模块中比较有 阅读全文
posted @ 2017-10-20 00:52 真勇士王小山 阅读(99) 评论(0) 推荐(0) 编辑
摘要: #!/usr/bin/python# Filename: cat.pyimport sysdef readfile(filename): '''Print a file to the standard output.''' f = file(filename) while True: line = 阅读全文
posted @ 2017-10-20 00:49 真勇士王小山 阅读(268) 评论(0) 推荐(0) 编辑
摘要: #!/usr/bin/python# Filename: finally.pyimport timetry: f = file('poem.txt') while True: # our usual file-reading idiom line = f.readline() if len(line 阅读全文
posted @ 2017-10-20 00:41 真勇士王小山 阅读(139) 评论(0) 推荐(0) 编辑
摘要: #!/usr/bin/python# Filename: raising.pyclass ShortInputException(Exception): '''A user-defined exception class.''' def __init__(self, length, atleast) 阅读全文
posted @ 2017-10-20 00:35 真勇士王小山 阅读(157) 评论(0) 推荐(0) 编辑
摘要: #!/usr/bin/python# Filename: try_except.pyimport systry: s = raw_input('Enter something --> ')except EOFError: print '\nWhy did you do an EOF on me?' 阅读全文
posted @ 2017-10-20 00:30 真勇士王小山 阅读(110) 评论(0) 推荐(0) 编辑
摘要: #!/usr/bin/python# Filename: pickling.pyimport cPickle as p#import pickle as pshoplistfile = 'shoplist.data'# the name of the file where we will store 阅读全文
posted @ 2017-10-19 00:57 真勇士王小山 阅读(158) 评论(0) 推荐(0) 编辑
摘要: 首先,我们通过指明我们希望打开的文件和模式来创建一个file类的实例。模式可以为读模式('r')、写模式('w')或追加模式('a')。事实上还有多得多的模式可以使用,你可以使用help(file)来了解它们的详情。 我们首先用写模式打开文件,然后使用file类的write方法来写文件,最后我们用c 阅读全文
posted @ 2017-10-19 00:29 真勇士王小山 阅读(118) 评论(0) 推荐(0) 编辑
摘要: #!/usr/bin/python# Filename: inherit.pyclass SchoolMember: '''Represents any school member.''' def __init__(self, name, age): self.name = name self.ag 阅读全文
posted @ 2017-10-19 00:21 真勇士王小山 阅读(96) 评论(0) 推荐(0) 编辑
上一页 1 ··· 5 6 7 8 9 10 11 12 13 ··· 15 下一页