Aug 12, 2019

Modules | Python programming

Module is a simple but powerful concept in python. We saw in C programs we used header files. ( studio.h , string.h , conio.h etc) In hear we use a similar thing called module. We can call a module as a collection of functions. There are hundred of per built modules for various tasks. Also we can make our own modules. In following example I make a module called myModule.py . After I import it in our main python program with Command 'import'.
[email protected]:~/programming$ cat myModule.py
def calculate(a, b):
    c = (a + 2*b)-3
    return c
def printOut():
  print("hear we print a string")
 
[email protected]:~/programming$ cat tmp1.py
#!/usr/bin/env python
import myModule as module1
#now we can use myModule.py's functions.
module1.printOut()
print (module1.calculate(2, 3))

[email protected]:~/programming$ python tmp1.py
hear we print a string
5

[email protected]:~/programming$ python3 tmp1.py
hear we print a string
5
First we want to talk about myModule.py. there are no special things there. We have defined some functions in that file but we haven't call those functions. Next the syntax for importing a module is following. import module_name Or import module_name as new_name In our above example we have used second method. So we have given a name when we importing the module. Then we can refer module's functions with that name. Note that the syntax for importing modules is same in both versions of python. Let's assume that we imported our module(myModule.py) with first method. import myModule So now we want to access it's functions with module name like this. myModule.printOut() myModule.calculate() But I suggest you to use second method. Always give a new name when you import a module. Also there is another method for importing a module. That is importing only a required function from a module like following. from sys import argv Hear we imported argv function from the sys module. This method has a benefit when comparing with above one. This will reduce memory allocation because you only import a one function instead of importing all big module. I think now it's clear how to import a module and how to use it. There are lot of important modules like requests, urliib2, time , threading etc. I'll make separate tutorials for explain those. Till then read other tutorials and master them. Thanks for reading
Apr 16
Wordpress nulled theme checker

We all love free stuff. So many people try to install premium themes and plugins on there WordPress....

Aug 12
Introduction to C language

C. Yes the classic C language is the mother language of many programing languages like C++/C# . Do....

Aug 20
Assembly basic tutorial - add two numbers

Welcome guys, today another basic tutorial on assembly coding. Hear I'll explain you how we can add....

Replying to 's comment Cancel reply
CATEGORIES
ABOUT AUTHER
Thilan Danushka Dissanayaka

Thilan Dissanayaka

Hi, I'm Thilan from Srilanka. An undergraduate Engineering student of University of Ruhuna. I love to explorer things about CS, Hacking, Reverse engineering etc.

SOCIAL
RANDOM ARTICLES