Creating Your Own Module

Creating Your Own Module

Programs for Creating and Using a Custom Module

Step 1: Create a Custom Module (mymodule.py)

Create a file named mymodule.py with the following functions.

# mymodule.py

def add(a, b):
    """Returns the sum of two numbers."""
    return a + b

def subtract(a, b):
    """Returns the difference between two numbers."""
    return a - b

def multiply(a, b):
    """Returns the product of two numbers."""
    return a * b

def divide(a, b):
    """Returns the quotient of two numbers, if b is not zero."""
    if b != 0:
        return a / b
    else:
        return "Cannot divide by zero"

def factorial(n):
    """Returns the factorial of a number."""
    if n == 0:
        return 1
    else:
        return n * factorial(n - 1)

def greet(name):
    """Returns a greeting message."""
    return f"Hello, {name}!"

Save this code as mymodule.py. We’ll now import and use it in different programs.


Program 1: Import and Use the add and subtract Functions

import mymodule

# Using the add function
result_add = mymodule.add(5, 3)
print("Addition Result:", result_add)

# Using the subtract function
result_subtract = mymodule.subtract(10, 4)
print("Subtraction Result:", result_subtract)

Expected Output:

Addition Result: 8
Subtraction Result: 6

Program 2: Import and Use the multiply and divide Functions

import mymodule

# Using the multiply function
result_multiply = mymodule.multiply(7, 6)
print("Multiplication Result:", result_multiply)

# Using the divide function
result_divide = mymodule.divide(42, 7)
print("Division Result:", result_divide)

Expected Output:

Multiplication Result: 42
Division Result: 6.0

Program 3: Use the factorial Function

import mymodule

# Using the factorial function
num = 5
result_factorial = mymodule.factorial(num)
print(f"Factorial of {num}:", result_factorial)

Expected Output:

Factorial of 5: 120

Program 4: Use the greet Function

import mymodule

# Using the greet function
name = "Alice"
greeting = mymodule.greet(name)
print(greeting)

Expected Output:

Hello, Alice!

Program 5: Use All Functions Together in a Comprehensive Program

import mymodule

# Calling all functions
print("Addition:", mymodule.add(10, 5))
print("Subtraction:", mymodule.subtract(10, 5))
print("Multiplication:", mymodule.multiply(10, 5))
print("Division:", mymodule.divide(10, 5))
print("Factorial of 4:", mymodule.factorial(4))
print("Greeting:", mymodule.greet("Charlie"))

Expected Output:

Addition: 15
Subtraction: 5
Multiplication: 50
Division: 2.0
Factorial of 4: 24
Greeting: Hello, Charlie!

Program 6: Import Specific Functions Directly

from mymodule import add, greet

# Directly calling add and greet without prefix
print("Addition:", add(3, 9))
print(greet("Bob"))

Expected Output:

Addition: 12
Hello, Bob!

Program 7: Use Aliases for Functions

import mymodule as mm

# Calling functions with module alias
print("Multiplication (Alias):", mm.multiply(4, 7))
print("Division (Alias):", mm.divide(35, 5))

Expected Output:

Multiplication (Alias): 28
Division (Alias): 7.0

Program 8: Use factorial with Larger Input

import mymodule

# Calculating factorial of a larger number
print("Factorial of 7:", mymodule.factorial(7))

Expected Output:

Factorial of 7: 5040

Program 9: Error Handling with divide

import mymodule

# Attempt division by zero
result = mymodule.divide(5, 0)
print("Division Result:", result)

Expected Output:

Division Result: Cannot divide by zero

Program 10: Conditional Import and Use

try:
    import mymodule
    # Only execute if the module is present
    print("Factorial of 3:", mymodule.factorial(3))
except ImportError:
    print("Module 'mymodule' not found.")

Expected Output (if module is present):

Factorial of 3: 6

These programs demonstrate using a custom module, with expected outputs provided for each example.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top