Загрузка...

Modules in Python

Modules in Python: I received one project: Client—project Many persons will involve…to complete the project 1. Requirement analysis 2. Client-meet---requirement: 3. SRS 4. Design #Modules example: Arithmetic Modules #arith.py def add(a,b): return a+b def sub(a,b): return a-b def mul(a,b): return a*b def div(a,b): return a/b Save the above program as “arith.py” Go to same folder (Ex: Caliculator) and write another program to use the function of other module. import arith a=int(input("enter a value:")) b=int(input("enter b value:")) c=arith.add(a,b) d=arith.sub(a,b) e=arith.mul(a,b) f=arith.div(a,b) print("Addition of two nos:",c) print("Sub of two nos:",d) print("Mul of two nos:",e) print("div:",f) """ Output: enter a value:8 enter b value:4 Addition of two nos: 12 Sub of two nos: 4 Mul of two nos: 32 div: 2.0 """ save this program with any name.py, Ex: test.py literals statements functions: collection of statements which performs a specific task is called function modules: collections of functions, classes and variables which are saved into a file, which is nothing but a module Two types of modules: 1. Predefined/built in modules: Sys,io, functools,math,…etc 2. User defined module Which means we are defining our own module, so that we can use wherever it is required, simply by importing the module name, call the function. #If you want to use arith.py module in another directory import sys sys.path.append("F:\Programs\Caliculator") import arith a=int(input("enter any two nos")) b=int(input("enter any two nos")) c=arith.add(a,b) print("addition of two numbers are:",c) """ enter any two nos8 enter any two nos4 addition of two numbers are: 12 """ #If you want to use arith.py module in another directory We can use from module_name(file.py)import functionanmes,func2,… import sys sys.path.append("F:\Programs\Caliculator") #import arith from arith import add,div a=int(input("enter any two nos")) b=int(input("enter any two nos")) #c=arith.add(a,b) c=add(a,b) print("addition of two numbers are:",c) """ enter any two nos8 enter any two nos4 addition of two numbers are: 12 """ Example2: #module1.py n=20 def add(a,b): print(a+b) def mul(a,b): print(a*b) module1 contains one variable i.e n, and two functions (add(),mul()) I want to use the above members (var,fun,class) in another program in same folder. Steps: 1. First we need to ‘import the module’ Ex: import module1 2. We can access members by using module name. Example: modulename.member module1.n module1.add(2,3) module1.mul(4,5) Renaming the module: #renaming a module import module1 as m print(m.n) m.add(2,3) m.mul(2,3) """ 20 5 6 """ From..import We can import the modules, and its members by using from…import keywords. Main use of from..import , directly we can access members of modules. Ex: #from..import from module1 import n,add,mul print(n) add(2,3) mul(2,3) """ 20 5 6 """ We can import all members of a modules by using following way: from module1 import* print(n) add(2,3) mul(2,3) """ 20 5 6 """ If you want to use the other program’s functions, variables, classes in another program: import module import module1,module2,module3,module4,…. import module1 as m Renaming Multiple modules : Import module1 as m, module2 as m1, module3 as m2,.. Without using module name, directily we can call the members of modules: From module1 import add,n,mul #all the members can be accessed by using * From module1 import*

Видео Modules in Python автора Персональный бот
Яндекс.Метрика
Все заметки Новая заметка Страницу в заметки
Страницу в закладки Мои закладки
На информационно-развлекательном портале SALDA.WS применяются cookie-файлы. Нажимая кнопку Принять, вы подтверждаете свое согласие на их использование.
О CookiesНапомнить позжеПринять