Topics

Python Modules and Functions

Welcome to another tutorial, here you learn how to use modules and other related functions in Python. In the previous tutorial, you learned about modules and other mathematical functions. Aside from the module, you learned previously, there are other mathematical modules available in the python library that can be used directly inside any python program. Few of the modules are very important to programmers who want to make real-life applications.

 

Functions lie inside the Module

Just as already known, you can consider a module as a piece of python code saved within the python package and can be reused from the module files or other python code files.

Before we proceed, try and remember what we did after importing the math module; as we tested some of the functions defined inside the math module, such as the math.floor(8.4), math.sin(3.14159) and so on. Examples of calling a function include math.floor(8.4), pow(5,2) etc.

Check out the example below to see how to use the import statement to import external modules such as the math module.

# Using Math library in Python
import math

x = 10

# log() function is in math module
y = math.log(x)

print (y)

# abs function
z = -7
print (abs(z))

# pow function
print (pow(x,2))

 

From the above example, the code we have written above is just a way to call and use these functions. The logic or code that performs this complex mathematical operation resides inside the module file and is called function definition. 

Function definition sets the rules of how a function will interpret the input provided. Also, it is the function definition itself that decides the type and number of input(s) and output for the functions. A parameter is an official term for these inputs.

Parameter: This is the structure that defines the type of the input accepted as the function is been defined.

Argument: This is the actual value that is inserted as an input while calling or using the function.

Take for example, if we create a module file (INSERT NAME.py) (.py means that is a python code file). So inside the module file, a function has been defined which can solve the quadratic equation ax2 + bx +c =0. Let name the function qSolve(a,b,c)

Now, what you need to do next is to use this function, by providing the values of a, b, and c, and the function will print whether the two solutions are real or not and what they are. Below is how you can import the module and use the function.

Step 1: Import the module

>>> import PythonTutorial

Step 2: call the function with appropriate arguments.

>>> qSolver(2, -6, 3)

Note that this function does not need to be tried on the IDLE, as it will not work, because you have not created any module name (INSERT NAME.py here above example PythonTutorial.py) on your computer. However, this example was just to explain to you how modules and functions work and how you can create custom modules with functions defined by you. The steps highlighted above must be followed to call every function from the modules.

 

How to use a function defined inside a module

Below are the things you should know before making use of a function defined inside a module:

  • The module contained inside a function must be known
  • Confirm if the module file is on your computer; if it is there then you can use it, but if it is not, then you have to download it. Take note that you can check the module by importing it in the IDLE, and an error means it’s not there.
  • Identify the name of the function to be used. You can check out python documentation for the names.
  • Also, check python documentation to know the number of arguments it requires, what they mean, and their order.
  • Finally, call the function; <module-name> is module's name, <function-name> is function's name; and <agrument-1><argument-2>... represent arguments of functions.
>>> <module-name>.<function-name>(<argument-1>, <argument-2>, ...)

 

Note: The number of arguments depends on the function used. Also, some functions, might not accept any argument, and so the number of arguments depends on the function that is been called, as such you need to check the documentation to see the function definition before you use them. 

Check out this example:

>>> variable-name = <module-name>.<function-name>(<argument-1>, <argument-2>, ...)

From the example, you can see that it only makes sense when the function returns any value (output). In addition, functions can return only one output value or none at all.

From what we have learned to suffer, let's make use of function lower() which is defined in the string module.

Before we continue, let’s understand what a String is.

 

What is a String?

A string is a term used to refer to a word such as ‘(INSERT YOUR WORD)', or a sentence like ‘(this website is a good one)’. Strings must be enclosed in single or double quotes ('…'/ "…"). Remember, that in one of our previous tutorials we explained how strings can be stored in a variable. Therefore, a variable that stores a string inside them is known as a String variable.

Furthermore, the String module is not the same as the String explained above, rather, the string module is a module with a name string with functions defined to execute various operations on string variables.

However, the string module is made up of functions that deal with string operations. Check out the example below, as will use lower(), which converts every character of a string to lowercase.

In summary:

  • You should know now that string is the library/module which consists of the function lower().
  • You must import the string module to check if it's available. However, it is a basic module, so it is very much likely to be available.
  • Also, you should check the documentation and find the function lower() on the module's page, as it will give you the required information to use it.
  • The lower() function has just one argument which is a string. 
  • Finally, call the function,
>>> string_variable = "Let's StartLearning Python"
>>> string.lower(string_variable)

but, if you want to go direct so as not to waste memory on saving a variable, you can do it as shown below:

>>> string.lower("Let's StartLearning Python")

Now, calling a function just as we did above, will print only the result when we hit enter. This is shown below:

>>> import string
>>>string.lower("Let's StartLearning Python")
tet's startlearning python

 

From the example above, it clears as the returned value is having all uppercase letters (i.e. Capital letters) converted to lowercase, that is L to lS to s, L to l,  and P to p. Know that, if you used a variable, using a function does not modify the value of the original variable. Just as seen in the above example. You can save the modified value in some other variable, or even in the same variable itself, just like this:

>>> string_variable = string.lower(string_variable)

In the above example, the statement is perfectly valid, because python will compile everything from right to left, that is to say, it will use string.lower() to get the modified string and then later assign the output value to the string variable by using the equal to '=' operator.