custom module -2

Creating and Using More Custom Python Modules

Module 1: text_operations.py

# text_operations.py

def count_characters(text):
    return len(text)

def convert_to_uppercase(text):
    return text.upper()

def convert_to_lowercase(text):
    return text.lower()

Module 2: list_operations.py

# list_operations.py

def get_first_element(lst):
    if len(lst) > 0:
        return lst[0]
    else:
        return "Error: The list is empty."

def get_last_element(lst):
    if len(lst) > 0:
        return lst[-1]
    else:
        return "Error: The list is empty."

Module 3: math_operations.py

# math_operations.py

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

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

Main Program: main_program.py

import text_operations
import list_operations
import math_operations

print(text_operations.count_characters("Hello, World!"))     # Output: 13
print(text_operations.convert_to_uppercase("Hello, World!")) # Output: HELLO, WORLD!
print(text_operations.convert_to_lowercase("Hello, World!")) # Output: hello, world!

print(list_operations.get_first_element([1, 2, 3, 4, 5]))    # Output: 1
print(list_operations.get_last_element([1, 2, 3, 4, 5]))     # Output: 5

print(math_operations.add_numbers(10, 20))                   # Output: 30
print(math_operations.subtract_numbers(20, 10))              # Output: 10

Leave a Comment

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

Scroll to Top