摘要: Methods of File ObjectsThe rest of the examples in this section will assume that a file object calledfhas already been created.To read a file’s contents, callf.read(size), which reads some quantity of data and returns it as a string or bytes object.sizeis an optional numeric argument. Whensizeis omi 阅读全文
posted @ 2012-12-28 01:47 fff8965 阅读(197) 评论(0) 推荐(0) 编辑
摘要: string的ljust()、rjust()、center()用来对齐>>> for x in range(1, 11):... print(repr(x).rjust(2), repr(x*x).rjust(3), end=' ')... # Note use of 'end' on previous line... print(repr(x*x*x).rjust(4))... 1 1 1 2 4 8 3 9 27 4 16 64 5 25 125 6 36 216 7 49 343 8 64 512 9 81 7... 阅读全文
posted @ 2012-12-28 01:43 fff8965 阅读(195) 评论(0) 推荐(0) 编辑
摘要: dir函数用来查看上下文dir()会列出当前上下文的所有变量、模块、函数等等,把得到的值再传给dir可以像下查看>>> import fibo, sys>>> dir(fibo)['__name__', 'fib', 'fib2']>>> dir(sys) ['__displayhook__', '__doc__', '__egginsert', '__excepthook__', '__loader__', &# 阅读全文
posted @ 2012-12-28 01:33 fff8965 阅读(201) 评论(0) 推荐(0) 编辑
摘要: >>> tel = {'jack': 4098, 'sape': 4139}>>> tel['guido'] = 4127>>> tel{'sape': 4139, 'guido': 4127, 'jack': 4098}>>> tel['jack']4098>>> del tel['sape']>>> tel['irv'] = 4127> 阅读全文
posted @ 2012-12-28 00:56 fff8965 阅读(319) 评论(0) 推荐(0) 编辑
摘要: Set包含不重复元素,用{}或者set()创建,有于或非等运算符>>> basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}>>> print(basket) # show that duplicates have been removed{'orange', 'banana', 'pear', 'apple'}>&g 阅读全文
posted @ 2012-12-28 00:51 fff8965 阅读(161) 评论(0) 推荐(0) 编辑
摘要: 定义函数>>> def fib(n): # write Fibonacci series up to n... """Print a Fibonacci series up to n."""... a, b = 0, 1... while a < n:... print(a, end=' ')... a, b = b, a+b... print()...>>> # Now call the function we just defined:... fib(2000)0 1 1 阅读全文
posted @ 2012-12-28 00:17 fff8965 阅读(279) 评论(0) 推荐(0) 编辑