You can mail me Contact Me!

Program to define a module and import a specific function in that module to another program

Program to define a module and import a specific function in that module to another program

Let's create a simple calculator module with basic operations:

Algorithm:

  1. Create calculator.py module with functions:
    • Define add(x,y) function
    • Define subtract(x,y) function
    • Define multiply(x,y) function
    • Define divide(x,y) function with zero division check
  2. In main program:
    • Import only multiply function from calculator module
    • Get two numbers from user
    • Call multiply function with user inputs
    • Display result

Hello, dear reader! 👋

Thanks for visiting my blog! I’m a student just like you, sharing what I learn to help others with Python programming. I hope my posts are useful for your studies! 😊

If you find this post helpful, please leave a comment—even just a few emojis will make my day! 🐍✨ Your feedback keeps me motivated to create more content for everyone. 🚀

Happy programming!
Abhin Krishna, S01, EB Department, MEC


Pseudocode:

// calculator.py module
FUNCTION add(x, y)
    RETURN x + y
END FUNCTION

FUNCTION subtract(x, y)
    RETURN x - y
END FUNCTION

FUNCTION multiply(x, y)
    RETURN x * y
END FUNCTION

FUNCTION divide(x, y)
    IF y != 0 THEN
        RETURN x / y
    ELSE
        RETURN "Error: Division by zero"
    END IF
END FUNCTION

// Main program
FROM calculator IMPORT multiply

OUTPUT "Enter first number: "
INPUT num1

OUTPUT "Enter second number: "
INPUT num2

result = multiply(num1, num2)

OUTPUT "Result of multiplication: " + result
Program:
1. First File (calculator.py) - The Module:

# calculator.py - module

def add(x, y):
    return x + y

def subtract(x, y):
    return x - y

def multiply(x, y):
    return x * y

def divide(x, y):
    if y != 0:
        return x / y
    return "Error: Division by zero"
2. Second File (main.py) - Main Program:

# Import specific function from calculator module
from calculator import multiply

# Get input from user
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))

# Use the imported multiply function
result = multiply(num1, num2)

# Display result
print(f"Result of multiplication: {result}")
Flowchart:
flowchart TD subgraph Module[calculator.py Module] A1[Add Function] A2[/Input: x, y/] A3[Return x + y] S1[Subtract Function] S2[/Input: x, y/] S3[Return x - y] M1[Multiply Function] M2[/Input: x, y/] M3[Return x * y] D1[Divide Function] D2[/Input: x, y/] D3{y != 0?} D4[Return x / y] D5[Return Error] A1 --> A2 --> A3 S1 --> S2 --> S3 M1 --> M2 --> M3 D1 --> D2 --> D3 D3 -->|Yes| D4 D3 -->|No| D5 end subgraph Main[Main Program] P1([Start]) P2[[Import multiply from calculator]] P3[/Input first number/] P4[/Input second number/] P5[Call multiply function] P6[/Display result/] P7([End]) P1 --> P2 P2 --> P3 P3 --> P4 P4 --> P5 P5 --> P6 P6 --> P7 end

Important!
If you find any mistakes in my code or flowchart, please comment below this post. I will be happy to correct them and clear up any doubts you may have.

Program to define a module and import a specific function in that module to another program
Program to define a module and import a specific function in that module to another program


1 comment

  1. NB: for calling a function in a flowchart, you use the rectangle with vertical side-lines. This symbol represents a predefined process or a module.
Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
Site is Blocked
Sorry! This site is not available in your country.