Monday, September 19, 2016

Python Modules and Packages





Working directory

PyLearning
├── Animals
├── foo
├── README.md
└── test


foo/
├── bar.py
├── fibo.py
├── fibo.pyc
├── __init__.py
└── __init__.pyc


test/
├── Backwards.py
├── Backwards.pyc
├── callFibo.py
├── callFibo.pyc
├── Card.py
├── Foo.py
├── Foo.pyc
├── __init__.py
├── __init__.pyc
├── mystuff.py
├── mystuff.pyc
└── support.py



fibo.py
def fib(n):    # write Fibonacci series up to n
    a, b = 0, 1
    while b < n:
        print b,
        a, b = b, a+b

def fib2(n):   # return Fibonacci series up to n
    result = []
    a, b = 0, 1
    while b < n:
        result.append(b)
        a, b = b, a+b
    print result

def fib3(n):    # write Fibonacci series up to n
    a, b = 0, 1
    while b < n:
        print b,
        a, b = b, a+b




callFibo.py

from foo.fibo import fib, fib2, fib3

print('Call to fib3()')
fib3(100)
print('\n')

print('Call to fib2()')
fib2(100)
print('\n')

print('Call to fib()')
fib(1000)
print('\n')   

print('Call to instance of fib()')
fib=fib
fib(500)

Output

Call to fib3()
1 1 2 3 5 8 13 21 34 55 89 

Call to fib2()
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]


Call to fib()
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 

Call to instance of fib()
1 1 2 3 5 8 13 21 34 55 89 144 233 377



















No comments:

Post a Comment