DEV Community

Kuhanraja A R
Kuhanraja A R

Posted on

day -8 at payilagam import and modules

MODULE:

A Python module is a file containing Python definitions and statements. A module can define functions, classes, and variables. A module can also include runnable code.

python module ends with .py

name: gives module name
file: gives file path
doc : tells documentation string

identifiers starting and ending with double underscores (like name, doc, etc.) are called "dunder" (double underscore) methods or attributes.

print("Hello")
print(__name__) 
Enter fullscreen mode Exit fullscreen mode

the value of name will be set to "main".


print("Hello")
print(__file__)

output:
Hello
__main__
Enter fullscreen mode Exit fullscreen mode

the script which is saved as first file and you run it directly with python first file, the output will look something like this:

Hello
/home/raja/Desktop/first.py

''' User module documentation string'''
print(doc)

User module documentation string

calculator.py


def add(no1,no2):
    print(no1+no2)

def subtract(no1,no2):
    print(no1-no2)

def multiply(no1,no2):
    print(no1*no2)

def divide(no1,no2):
    print(no1/no2)
Enter fullscreen mode Exit fullscreen mode

The import statement in Python is used to bring code from one module (a Python file) into another.

user.py

import calculator

calculator.add(100,123)
calculator.multiply(10,3)

This will call the add function from your calculator module, which adds 100 and 123.
This will call the multiply function, which multiplies 10 and 3.

223
30

user.py

from calculator import add, divide

add(100,200)
divide(200,40)

300
5.0
Enter fullscreen mode Exit fullscreen mode

help()

In Python, the help() function is a built-in function used to display documentation about Python objects, modules, functions, classes, or methods.

import math
help(math)
Enter fullscreen mode Exit fullscreen mode

This will display detailed information about the math module, including its functions like floor(), ceil(), etc.

help('modules')
Enter fullscreen mode Exit fullscreen mode

This will displays the list of modules available in python.

task:

money.py
def deposit:
    def deposit(amount):
    print("Enter the deposit amount:",amount)
def withdraw(amount):
    print("Enter the withdraw amount:",amount)

consumer.py
import money
money.deposit(1000)
money.withdraw(500)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)