摘要:
>>> def powersum(power, *args):... '''Return the sum of each argument raised to specified power.'''... total = 0... for i in args:... total += pow(i, 阅读全文
摘要:
#!/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)的数指定了一个 阅读全文
摘要:
os模块 这个模块包含普遍的操作系统功能。如果你希望你的程序能够与平台无关的话,这个模块是尤为重要的。即它允许一个程序在编写后不需要任何改动,也不会发生任何问题,就可以在Linux和Windows下运行。一个例子就是使用os.sep可以取代操作系统特定的路径分割符。 下面列出了一些在os模块中比较有 阅读全文
摘要:
#!/usr/bin/python# Filename: cat.pyimport sysdef readfile(filename): '''Print a file to the standard output.''' f = file(filename) while True: line = 阅读全文
摘要:
#!/usr/bin/python# Filename: finally.pyimport timetry: f = file('poem.txt') while True: # our usual file-reading idiom line = f.readline() if len(line 阅读全文
摘要:
#!/usr/bin/python# Filename: raising.pyclass ShortInputException(Exception): '''A user-defined exception class.''' def __init__(self, length, atleast) 阅读全文
摘要:
#!/usr/bin/python# Filename: try_except.pyimport systry: s = raw_input('Enter something --> ')except EOFError: print '\nWhy did you do an EOF on me?' 阅读全文