day -8 at payilagam import and modules
Kuhanraja A R
Posted on November 22, 2024
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__)
the value of name will be set to "main".
print("Hello")
print(__file__)
output:
Hello
__main__
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)
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
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)
This will display detailed information about the math module, including its functions like floor(), ceil(), etc.
help('modules')
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)
Posted on November 22, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.