0% found this document useful (0 votes)
2 views9 pages

Python Modules - Notes Lyst5106

Modules descriptive Notes in Python

Uploaded by

Poorvika N J
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views9 pages

Python Modules - Notes Lyst5106

Modules descriptive Notes in Python

Uploaded by

Poorvika N J
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Today’s Agenda

 File, script and module


 Built-in modules
 Documentation string

File, script & module


A file from the hard disk when executed on command line then such
a program or a file is called as a script.

If we have a single file with numerous set of functions then it is


difficult to work with such file. The better way is to group similar
set of functions and make a separate file of it and name accordingly.
These files are called as modules.

Whenever we are in need of certain functions, instead of defining a


function again we can just use the function present in particular
module by bringing to current file. This process is called as
importing and is done by using
the keyword import.

Importing functions from


modules to a file reduces the
length of code and certainly
reduces the complexity.

1 Python fundamentals| Rooman Technologies


Let us now consider an example where we have certain functions in a
module called as mymodule and want to use functions present in it, in
the python script called as [Link] and see how this works.

Instead of calling a module by its name every single time we want to


use a function from that module, we can rename it and this is called
as aliasing. This can be done by using keyword called as as. Let us see
how to do this

2 Python fundamentals| Rooman Technologies


There is definitely a simpler way for this, which is by using a
keyword called as from. Let us see how it makes things easy

In above example * means import all the functions present in


mymodule to the present file. And we can use the functions just by
calling by its name.

If the module contains huge number of functions and we want to use


only a couple of functions from it then instead of using * and
importing all functions we can just mentions the function which we
want to use and import them selectively as shown below

3 Python fundamentals| Rooman Technologies


If you want to import more than one function from the module the
all you have to do is

Note: Knowing that a function is present in the module but calling it


without importing, will definitely throw an error saying <function
name> is not defined.

And also do not mix these different ways of importing functions, it


will give an error. Better follow just one method.

4 Python fundamentals| Rooman Technologies


Built-in modules
Whatever we have seen so far are user defined modules, where we
have created a module with certain functions in it. But there are
some modules which contain numerous functions in it and are already
present in python these are called as built-in modules.

Let us explore one such called as math

help() is a function which will give you complete description about


each function present in that module. Above are only few functions
present in math module.

And if you just want to know the different functions present in the
module without any description the just use dir() function as shown
below

5 Python fundamentals| Rooman Technologies


By looking at the square brackets we can say that it returns the list
of functions name present in a particular module.

To get the help for a specific function, let us see what to do

Let us check if it works as expected

6 Python fundamentals| Rooman Technologies


Let us check some more functions like ceil(), floor()

For example:

Now let’s see what floor() does

For example:

7 Python fundamentals| Rooman Technologies


Documentation string
Earlier we saw how to create a module, but we have seen that every
module has a description so that it’s easy for a programmer to know
what does a function inside particular module do. The description
provided is called as documentation. The definition of that function
is called documentation string which is enclosed within triple quotes.

Let us understand by an example

We can certainly see the documentation of all the functions in


power_of module.

To see description of selected functions, as said earlier we have to


use help(<module_name>.<function_name>) as shown below

8 Python fundamentals| Rooman Technologies


There is also a second way to access the documentation string. Every
function has a variable, the name of this variable is __doc__(double
underscore doc double underscore)/dunder doc. Let us see how to
access it

9 Python fundamentals| Rooman Technologies

You might also like