2. Disclaimer: This presentation is prepared
by trainees of baabtra as a part of
mentoring program. This is not official
document of baabtra –Mentoring Partner
Baabtra-Mentoring Partner is the mentoring division of baabte System
Technologies Pvt . Ltd
6. What is module??
●
●
a module is a file consisting of Python
code. A module can define functions,
classes and variables. A module can also
include runnable code.
The file name containing suffix .py
7. def fib(n):
# write Fibonacci series up to n
a, b = 0, 1
while b < n:
print b,
a, b = b, a+b
●
●
Save the file in fibo.py
We can use fibo.py in other program by
importing fibo.py
9. ●
●
●
●
●
A module can contain executable statements as well as
function definitions
They are executed only the first time the module name is
encountered in an import statement
Modules can import other modules.
Each module has its own private symbol table, which is used
as the global symbol table by all functions defined in the
module.
The author of a module can use global variables in the
module without worrying about accidental clashes with a
user’s global variables.
10. ●
There is a variant of the import statement that imports names
from a module directly into the importing module’s symbol table.
For example:
from fibo import fib, fib2
Fib(500)
Output:
1 1 2 3 5 8 13 21 34 55 89 144 233 377
●
There is another variant to import all names that a module
defines:
from fibo import *
●
This imports all names except those beginning with an
underscore (_).
11. ●
●
Each module is only imported once per interpreter session if
you change your modules, you must restart the interpreter or
if it’s just one module you want to test interactively, use
reload()
Eg: reload(modulename).
12. The Module Search Path
When a module named spam is imported, the interpreter first
searches for a built-in module with that name. If not found, it
then searches for a file named spam.py in a list of directories
given by the variable sys.path.
“Compiled” Python files
●
●
If a file called spam.pyc exists in the directory where
spam.py is found
15. If this presentation helped you, please
visit our page facebook.com/baabtra and
like it.
Thanks in advance.
www.baabtra.com | www.massbaab.com |
www.baabte.com
16. Contact Us
Emarald Mall (Big
Bazar Building)
Mavoor Road,
Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25
550
Start up Village
Eranakulam,
Kerala, India.
Email:
info@baabtra.com
NC Complex,
Near Bus
Stand
Mukkam,
Kozhikode,
Kerala, India.
Ph: + 91 –
495 40 25
550