A good way to play around with python modules is using functions. In a previous mastery about the programing environment we already reviewed how to create a python file, and a module is just like that, but with the condition is that it has to be able to run by itself if given a parameter, just like a function.

First we shall create our own module by opening a new file and writing the following function:

Def smores (x,y):

C = x + y

Return c

Then save the file with the name “Coco.py” in the folder yo store all of your programs.

Now we can make use of it, as you can probably tell the function above makes an addition of two numbers, if you need to perform this operation in a program, instead of writing it again you can just import it with something called “dot notation”.

Get to the first line of your code and type the command “Import”, then write the name of your file, in this case Coco, do not write the “.py”. After that go to the part where you need to implement the function and type “Coco” followed by a dot and then the name of the function, in this case “smores”, it should look like this: “Coco.smores()”, place the values you want added inside the parenthesis and there you go! As simple as that.

As a plus from creating your own modules with useful functions inside of them you can also use some that already come within python or just download some from the internet.

The import process will be the same for all of the modules, you just have to be careful and be sure you save them all in the same place for easy Access.

CC BY 4.0 Creating, Importing and Using Python modules by juansalvadorfernandez is licensed under a Creative Commons Attribution 4.0 International License.