Docsity

Python program 15th lab program for MCA new syllabus cbcs, Study notes of Computer Science

Toggle sidebar
bg1
15. Write a python program to define a module and
import a specific function in that module to another
program .
Program1 :
1. #Python Module example
2.
3. def add(a, b):
4. """This program adds two
5. numbers and return the result"""
6.
7. result = a + b
8. return result
Program 2 :
# importing add() from above program
import example
example.add(4,5.5)
# importing standard module math
Import math
Print(โ€œ The value of pi is :โ€,math.pi)
pf3
pf4
pf5

Partial preview of the text

Download Python program 15th lab program for MCA new syllabus cbcs and more Study notes Computer Science in PDF only on Docsity!

  1. Write a python program to define a module and import a specific function in that module to another program. Program1 :
  2. #Python Module example
  3. def add(a, b):
  4. """This program adds two
  5. numbers and return the result"""
  6. result = a + b
  7. return result Program 2 :

importing add() from above program

import example example.add(4,5.5)

importing standard module math

Import math Print(โ€œ The value of pi is :โ€,math.pi)

importing module by renaming it by using as keyword

Import math as m Print(โ€œ The value of pi is : :โ€,m.pi)

using fromโ€ฆ import statement

From math import pi Print(โ€œThe value of pi is:โ€,pi) #importing multiple attributes from math import pi, e Print(โ€œ The value of pi is:โ€, pi) Print(โ€œ the value of e is:โ€,e)

importing all definitions from a

module using * symbol From math import * Print(โ€œ The value of pi is:โ€,pi)

  1. The Python interpreter imports a module only

    once during a session.
  2. This module shows the effect of

use the dir() function to find out names that are

defined inside a module. dir(example) #All other names other than โ€œadd โ€œ that begin with an underscore are default Python attributes associated #with the module (we did not define them ourself) #name attribute contains the name of the module. Import example example.name #All the names defined in our current namespace can be found out using the dir() function without any #arguments. a = 1 b = "hello" import math dir()