Creating python module

Creating python module

Instructor-svgAl-Mamun Sarkar
Mar 25 , 2020

Creating a python module. The following code shows how to create a module in the Python programming language.

Create a folder named fibo. Inside fibo folder create a python file named fibo.py and also create blank __init__.py file. 

Code inside fibo/fibo.py file:

def fib(n):
    series = []
    a, b = 0, 1
    while b < n:
        series.append(b)
        a, b = b, a+b
    return series

 

Create a python file named test.py inside fibo folder for accessing code inside fibo.py files under the same directory.

Code inside fibo/test.py file

from fibo import *

series = fib(100)
for item in series:
    print(item)

Output:

1
1
2
3
5
8
13
21
34
55
89

 

Create a python file named hello.py outside fibo directory.

Code inside hello.py file

from fibo.fibo import fib

series = fib(100)
for item in series:
    print(item)

Output:

1
1
2
3
5
8
13
21
34
55
89

 

  • Share On:
  • fb
  • twitter
  • pinterest
  • instagram