Beruflich Dokumente
Kultur Dokumente
Laldas
SE Computer A
Roll No.53
Experiment:5B
AIM: A Python program to create an employee module and uses its function to
calculate the gross and net salaries of an employee.
Define Modules:
A module allows you to logically organize your Python code. Grouping related
code into a module makes the code easier to understand and use. A module is a
Python object with arbitrarily named attributes that you can bind and reference.
Simply, a module is a file consisting of Python code. A module can define
functions, classes and variables. A module can also include runnable code.
Example
The Python code for a module named aname normally resides in a file
named aname.py. Here's an example of a simple module, support.py
def print_func( par ):
print "Hello : ", par
return
The import Statement
You can use any Python source file as a module by executing an import
statement in some other Python source file. The import has the following syntax
−
import module1[, module2[,... moduleN]
When the interpreter encounters an import statement, it imports the module if the
module is present in the search path. A search path is a list of directories that
the interpreter searches before importing a module.
Example
def greeting(name):
print("Hello, " + name)
Use a Module
Example
Import the module named mymodule, and call the greeting function:
import mymodule
mymodule.greeting("Jonathan")
Note: When using a function from a module, use the
syntax: module_name.function_name.
Variables in Module
Example
person1 = {
"name": "John",
"age": 36,
"country": "Norway"
}
Example
import mymodule
a = mymodule.person1["age"]
print(a)
Naming a Module
You can name the module file whatever you like, but it must have the
file extension .py
Re-naming a Module
Example
a = mx.person1["age"]
print(a)
Built-in Modules
There are several built-in modules in Python, which you can import
whenever you like.
Example
import platform
x = platform.system()
print(x)
There is a built-in function to list all the function names (or variable
names) in a module. The dir() function:
Example
import platform
x = dir(platform)
print(x)
Note: The dir() function can be used on all modules, also the ones you
create yourself.
Example
def greeting(name):
print("Hello, " + name)
person1 = {
"name": "John",
"age": 36,
"country": "Norway"
}
Example
from mymodule import person1
print (person1["age"])
A Python program to create an employee module and uses its function to
calculate the gross and net salaries of an employee.
CODE:
#employee module.py
def da(basic):
da = basic * 0.8
return da
def HRA(basic):
HRA = basic * 0.3
return HRA
def pf(gross):
pf = gross * 0.15
return pf
def tax(gross):
tax = gross * 0.25
return tax
OUTPUT:
CONCLUSION:
Hence we have successfully implemented and studied functions and modules in
python.