Open In Colab

Python Modules

  • A module is a file containing a set of functions you want to include in your application.
  • A module is the same as a code library.

Create Your Own Module

  • Save the code in a file with the file extention .py
  • Use the module you just created by using the import statement.
  • import module name (as alt_name)
  • from module_name import function/variable name (as alt_name)
In [1]:
from google.colab import drive
drive.mount('/content/drive')
Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount("/content/drive", force_remount=True).
In [2]:
import os, sys
os.chdir('/content/drive/My Drive')
In [3]:
os.listdir('/content/drive/My Drive')
Out[3]:
['Colab Notebooks',
 'Data',
 'breccia.zip',
 'w_2020',
 'breccia',
 'breccia_jpg',
 'breccia_crop',
 'fibonacci.py',
 '__pycache__']
In [4]:
import fibonacci
In [5]:
fibonacci.fib(7)
0
1
1
2
3
5
8
In [6]:
fibonacci.fi(7)
Out[6]:
[0, 1, 1, 2, 3, 5, 8]
In [7]:
from fibonacci import fib
fib(11)
0
1
1
2
3
5
8
13
21
34
55
In [8]:
from fibonacci import fi
fi(17)
Out[8]:
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987]