Modules and related Functions in Python
In one of the previous tutorial, we learnt about the math module. We also checked some of its functions like the
log, etc., and also learned how they are used. Now just like the math module, there are several other modules available in python library which can be used directly inside any python program. Some of them are so important that programmers literally can’t make any real-life app without these modules.
In this lesson, we will revisit Modules and the Functions within them and will try to have a better understanding of them.
Functions lie inside the Module
Consider modules as, just another piece of python code, saved somewhere within the python package and you can reuse the code from the module files in any other (or your) python code file. Now try to recall what we did after importing the
math module. We tested some of the functions defined inside the
math module, like
sin(3.14159) are all examples of calling a function.
To see how we can use the
import statement to import external modules like math module, checkout the live example,
Now if, what we have written above is just a way to call and use these functions. Where is the actual logic(code) to perform these complex mathematical operations stored? i.e. calculating power, floor, or sine for the provided value(s). All this (code or logic), reside inside the module file and is called Function definition. Function definition sets the rules that how a function will interpret the input provided, perform some operation on it and further what is to be returned as the output or result.
In fact, it is the function definition itself that decides the type and number of input(s) and output (output is always one, but inputs can be multiple) for the functions. These inputs have an official term for them, they are known as Parameters.
Parameter: Structure that defines the type of the input accepted, while defining the function.
Argument: The actual value that is inserted as an input while calling/using the function.
Suppose we have created a module file onlineexamguide.py (
.py implies that it’s a python code file; Yes! you can create a module file yourself too, it’s just python code after all). Inside the module file, we have defined a function that can solve a quadratic equation
ax2 + bx + c = 0. Let the name of the function be
qSolver(a, b, c).
All you have to do to use this function is, provide the value of
c, i.e., coefficient values of the equation, and the function will print whether the two solutions are real or not and what are they. So, lets see how can we import the module and use the function.
Step 1: First import the module.
>>> import onlineexamguide
Step 2: Next call the function with appropriate arguments, i.e., for
c=3. Hence we will call,
>>> qSolver(2, -6, 3)
You don’t have to try this function on IDLE since this won’t work. Why? Well because we hadn’t actually created any module named onlineexamguide.py in your local system(laptop/computer). This was just to explain to you how module and functions actually work and how we can create custom modules with functions defined by us. You have to follow the same steps in order to call every function from the modules.
There are already many modules available for python. Some might have already been installed in your system when you installed python, while others can be easily downloaded from the internet. It all depends on what you need.
How to use a function defined inside a module
Following are the things you should know while using a function defined inside a module:
- You must know which module contains that function.
- Check if it’s already in your system. If it is, then all good, otherwise go ahead and download it from the internet.
(NOTE: You can check by importing that module in IDLE. Error means it’s not there.)
- Next, identify the name of the function that you want to use. You can refer to python’s documentation for that purpose.
- Refer documentation to check the number of arguments it requires, what they mean and their order.
- Next, call the function. <module-name> is module’s name, <function-name> is function’s name and <agrument-1>, <argument-2>… are the arguments of functions.
Note that the number of arguments depend on the function we are using. In some cases, function might not accept any argument. It entirely depends on which function you are calling, hence you must check the documentation to see the function definition before using any function. Above called function may or may not return any value (function’s output). In some cases, we are often require to store the output for future use. We do so by storing it in a variable, which we have already learnt in the last tutorial.
>>> variable-name = <module-name>.<function-name>
(<argument-1>, <argument-2>, ...)
This only make sense when the function returns any value (output). Also, functions can only return one output value or none at all.
Let’s use what we have understood till now, to use a function
lower() which is defined in the
Additional Topic (What is String?)
String is just a term used to refer to a word like
"onlineexamguide" or sentences like
"onlineexamguide is a good website".
They must be enclosed in single or double quotes (‘…’/ “…”). In the previous chapter, we have already explained how they can be stored in a variable. Variable which stores a string inside them are called string variables.
string module is different from the above explained String. This one is just a module with name string with functions defined to perform various operations on string variables.
string module consists of some functions which deal with operations on strings. In the example below, we will use
lower(), which is used to convert every character of a string to lowercase.
- We already know that
stringis the library/module which consists of the function
- Import the
stringmodule to check if it’s available. Since it’s a basic module, it’s very likely that it will be there.
- Check the documentation and find the function
lower()in the module’s page. It will give you the information required to use it.
- We already know that
- It only has one argument which is a string. So create a string variable with value as “Let’s onlineexamguide”.
- Next call the function,
>>> string_variable = "Let's onlineexamguide" >>> string.lower(string_variable)
Or, if you want to be direct and don’t waste memory on saving a variable, just do it like,
>>> string.lower("Let's onlineexamguide")
Calling a function like we did above will only print the result when we hit enter. Just like this,
As you can see, the returned value is having all uppercase letters (Capital letters) converted to lowercase, i.e. L to l, S to s and T to t. In case you used a variable, keep in mind that using a function like this doesn’t modify the value of the original variable. Thus even after using the function, the value of
string_variable will remain as it was before. So if you want, you can save the modified value in some other variable, or even in the same variable itself, like:
>>> string_variable = string.lower(string_variable)
This statement is perfectly valid since python will compile everything from right to left, i.e., it will first use
string.lower() to get the modified string and then later assign the output value to the
string_variable variable using the
= (equal to) operator.