Sie sind auf Seite 1von 8

Name: Pratiksha D.

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.

TOOLS USED: Python 3.4.3, Terminal


THEORY:

 Explain What is Function:


A function is a set of statements that take inputs, do some specific
computation and produces output. The idea is to put some commonly or
repeatedly done task together and make a function, so that instead of
writing the same code again and again for different inputs, we can call the
function.
Python provides built-in functions like print(), etc. but we can also create
your own functions. These functions are called user-defined functions.
Anonymous functions:
In Python, anonymous function means that a function is without a name.
As we already know that def keyword is used to define the normal
functions and the lambda keyword is used to create anonymous functions.

 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.

 How modules are created in Python


To create a module just save the code you want in a file with the file
extension .py:

Example

Save this code in a file named mymodule.py

def greeting(name):
  print("Hello, " + name)

Use a Module

Now we can use the module we just created, by using


the import statement:

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

The module can contain functions, as already described, but also


variables of all types (arrays, dictionaries, objects etc):

Example

Save this code in the file mymodule.py

person1 = {
  "name": "John",
  "age": 36,
  "country": "Norway"
}

Example

Import the module named mymodule, and access the person1


dictionary:

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

You can create an alias when you import a module, by using


the as keyword:

Example

Create an alias for mymodule called mx:


import mymodule as mx

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 and use the platform module:

import platform

x = platform.system()
print(x)

Using the dir() Function

There is a built-in function to list all the function names (or variable
names) in a module. The dir() function:

Example

List all the defined names belonging to the platform module:

import platform

x = dir(platform)
print(x)

Note: The dir() function can be used on all modules, also the ones you
create yourself.

Import From Module


You can choose to import only parts from a module, by using
the from keyword.

Example

The module named mymodule has one function and one dictionary:

def greeting(name):
  print("Hello, " + name)

person1 = {
  "name": "John",
  "age": 36,
  "country": "Norway"
}

Example

Import only the person1 dictionary from the module:

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

#importing employee module in code


from mod import *
name=input("Enter Employee name: ")
basic = int(input("Enter Employee basic salary: "))
gross = basic + da(basic) + HRA(basic)
pf(gross)
tax(gross)
net = gross - pf(gross) - tax(gross)
print("The Gross salary is",gross)
print("The Net Salary is",net)

OUTPUT:
CONCLUSION:
Hence we have successfully implemented and studied functions and modules in
python.

Das könnte Ihnen auch gefallen