Life is short, you need Python

公用函数与模块

1 #! /usr/bin/env python
2  
3 def fib(n):
4 a,b = 0,1
5 while(b < n):
6 print b
7 a,b = b,a+b
8
9 def fib2(n):
10 a,b = 0,1
11 result = []
12 while(b<n):
13 result.append(b)
14 a,b = b,a+b
15 return result

save the file as fibo.py, then enter python enviroment as below:

E:\PyTest>python
Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import fibo
>>> fibo.fib(20)
1
1
2
3
5
8
13
>>> from fibo import *
>>> fib(20)
1
1
2
3
5
8
13

 

Pay attention to the difference between import modulename and from modulename import module

posted @ 2010-09-27 15:01  runfox545  阅读(329)  评论(0编辑  收藏  举报
白月黑羽 Python教程 白月黑羽Python