Using modules in Python

Modules are useful to split your program/code in different files. In that way it becomes easier to use or adapt different parts separately.
Just the definition of a module is a file that consist Python code. In this blog I am going to explain you the basic things about using modules while programming in Python. The simplest way to make a module in Python is making a .py file. Actually I already explained the use of modules in my blog about using functions in Python from textline 15. In that blog I used the module math, that is a in-built module in Python. The only way you can use that module in the Python script that you are writing is using: import math. After importing that module, you are able to use the whole content of that module. You can see the whole content with explanationof the module math at this site. See this blog from textline 15 for the exact explanation about how to use import module.

You can also import only one function or variable from a module. In that case you need to use: import variable/function from module. In the folowing example I used the variable d out of the module with the name mastery08a to import in Mastery08. In this video you can see what I typed and how it worked.

Another usefull thing to do with your module if the name is too long is renaming it. You can rename you module in the script where you are typing in at that moment by using: import modulename as shortermodulename. In the folowing example I renamed mastery08a to mas8a:

If you want information about using modules in your Python file, you can see my blog about writing them and this site consist more easy and good ordered information about this subject. Hopefully you learned a lot again!

Plaats een reactie

Ontwerp een vergelijkbare site met WordPress.com
Aan de slag