0
What is module and what is function ?
7 Answers
+ 1
in my example, if you put calc.py in the same folder as your script it would just import with
import calc
0
modules are lots of functions stored in a sharable manner to ease in your coding. for instance if you say
import random
you are importing the random module to use the built in functions it contains.
Functions are chunks of code that are recallable. for instance if you have
def sayOne():
print("One")
and you called it 3 times
sayOne()
sayOne()
sayOne()
the output would be
One
One
One
0
a module is an external file that can hold functions predefined data etc.
so if you write many functions and decide to put them in an external file , that is a module.
0
So what about random and randint ? how we can define our own function to create our own modules..is it possible ?
0
simple. say you had these functions
def add(x,y):
return x+y
def sub(x,y):
return x-y
def div(x,y):
return x/y
save them to a file with a name like calc.py
Now if you want to use your add function in your new project you would just import calc and use it
import calc
add(4,3)
0
This is why you need to avoid naming files with module names. for example I named a file random.py .. after that when I import random it imports my script instead and then random functions no longer worked
0
so how python read the address of calc.py file where it is locate..is it mandatory to show the path of .py file in script