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 . After I import it in our main python program with Command 'import'.
[email protected]:~/programming$ cat
def calculate(a, b):
    c = (a + 2*b)-3
    return c
def printOut():
  print("hear we print a string")
[email protected]:~/programming$ cat
#!/usr/bin/env python
import myModule as module1
#now we can use's functions.
print (module1.calculate(2, 3))

[email protected]:~/programming$ python
hear we print a string

[email protected]:~/programming$ python3
hear we print a string
First we want to talk about 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( 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
Oct 30
best way to learn programming in c

C is a classical programming language. Many of other programming languages are based on C. If you....

Oct 17
Command line arguments in C

Hi guys, In this document we are going to see how we can use command line argumentsin C programs .....

Jun 19
Debugging Binaries with GDB

GDB is shipped with the GNU toolset. It is a debugging tool used in Linux environments. The term....

Replying to 's comment Cancel reply
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.