Page 1 of 2

Function

Posted: Mon Apr 14, 2025 12:11 pm
by admin

Code: Select all

def is_even(number):
    if number % 2 == 0:
        return True
    else:
        return False

# Example usage:
num = int(input("Enter a number: "))
if is_even(num):
    print(f"{num} is even.")
else:
    print(f"{num} is not even.")

Re: Function GST Structure Validator

Posted: Mon Apr 14, 2025 12:34 pm
by admin

Code: Select all

import re

def is_valid_gstin(gstin):
    # Define regex pattern for GSTIN structure
    pattern = r'^[0-9]{2}[A-Z]{5}[0-9]{4}[A-Z]{1}[1-9A-Z]{1}Z[0-9A-Z]{1}$'
    
    # Match against pattern
    if re.match(pattern, gstin):
        return True
    else:
        return False

# Example usage
gstin = input("Enter GSTIN: ").upper()
if is_valid_gstin(gstin):
    print(f"{gstin} is a valid GSTIN.")
else:
    print(f"{gstin} is NOT a valid GSTIN.")

Re: Function - GST with Check Digit Validator

Posted: Tue Apr 15, 2025 4:20 am
by admin

Code: Select all

import re

def is_valid_gstin(gstin):
    gstin = gstin.upper()

    # Check format using regex
    pattern = r'^\d{2}[A-Z]{5}\d{4}[A-Z]{1}[A-Z\d]{1}[Z]{1}[A-Z\d]{1}$'
    if not re.match(pattern, gstin):
        return False

    # GSTIN Check Digit Validation
    def calculate_check_digit(gstin_without_check_digit):
        factor = 2
        modulus = 36
        total = 0
        code_points = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'

        for char in reversed(gstin_without_check_digit):
            code_point = code_points.index(char)
            addend = factor * code_point

            # Alternate the factor
            factor = 1 if factor == 2 else 2

            # Sum the digits in base 36
            addend = (addend // modulus) + (addend % modulus)
            total += addend

        # Calculate the check code point
        remainder = total % modulus
        check_code_point = (modulus - remainder) % modulus
        return code_points[check_code_point]

    # Separate parts
    core = gstin[:-1]
    given_check_digit = gstin[-1]
    expected_check_digit = calculate_check_digit(core)

    return given_check_digit == expected_check_digit

# Example usage:
print(is_valid_gstin("33AAAAT7798M2ZQ"))  # ✅ Example valid GSTIN
print(is_valid_gstin("33AAAAA0000A1Z4"))  # ❌ Invalid check digit

Income Tax Calculator Function

Posted: Tue Jul 22, 2025 10:41 am
by jollyks

Code: Select all

def calculate_income_tax(income):
    """
    Calculate income tax based on tax slabs.
    :param income: Annual taxable income
    :return: Tax amount
    """
    tax = 0
    
    # Example tax slabs (Modify as per your country)
    slabs = [
        (250000, 0.00),    # No tax for income up to 250,000
        (500000, 0.05),    # 5% for income between 250,001 - 500,000
        (1000000, 0.20),   # 20% for income between 500,001 - 1,000,000
        (float('inf'), 0.30)  # 30% for income above 1,000,000
    ]
    
    previous_limit = 0
    for limit, rate in slabs:
        if income > previous_limit:
            taxable_income = min(income, limit) - previous_limit
            tax += taxable_income * rate
            previous_limit = limit
        else:
            break
    
    return tax

# Example usage
if __name__ == "__main__":
    income = float(input("Enter your annual taxable income: "))
    tax_amount = calculate_income_tax(income)
    print(f"Income Tax Payable: {tax_amount:.2f}")

calculate_emi

Posted: Thu Aug 21, 2025 9:02 am
by admin

Code: Select all

def calculate_emi(principal, annual_rate, years):
    """Calculate EMI given principal, interest rate (per annum), and tenure in years."""
    monthly_rate = annual_rate / (12 * 100)  # convert annual rate % to monthly decimal
    n = years * 12
    emi = principal * monthly_rate * ((1 + monthly_rate) ** n) / ((1 + monthly_rate) ** n - 1)
    return round(emi, 2)

# Example usage
loan_amount = 500000
rate = 8.5
tenure = 10
print("Monthly EMI:", calculate_emi(loan_amount, rate, tenure))

TDS

Posted: Thu Aug 21, 2025 9:03 am
by admin

Code: Select all

def calculate_tds(amount, rate=10):
    """Calculate TDS deducted at given rate (%) on amount."""
    tds = (amount * rate) / 100
    net_payment = amount - tds
    return {"TDS": tds, "Net Payment": net_payment}

# Example usage
bill_amount = 50000
result = calculate_tds(bill_amount, 10)
print("TDS:", result["TDS"])
print("Net Payment:", result["Net Payment"])

Split GST

Posted: Thu Aug 21, 2025 9:03 am
by admin

Code: Select all

def split_gst(amount, gst_rate=18):
    """Split GST into CGST and SGST."""
    gst = (amount * gst_rate) / 100
    cgst = sgst = gst / 2
    total = amount + gst
    return {"CGST": cgst, "SGST": sgst, "Total": total}

# Example usage
invoice_value = 100000
gst_breakup = split_gst(invoice_value)
print("CGST:", gst_breakup["CGST"])
print("SGST:", gst_breakup["SGST"])
print("Total Invoice Value:", gst_breakup["Total"])

depreciation_wdv

Posted: Thu Aug 21, 2025 9:04 am
by admin

Code: Select all

def depreciation_wdv(opening_value, rate, years):
    """Calculate depreciation using WDV method."""
    value = opening_value
    for y in range(1, years + 1):
        dep = value * (rate / 100)
        value -= dep
        print(f"Year {y}: Depreciation = {round(dep,2)}, Closing WDV = {round(value,2)}")
    return round(value, 2)

# Example usage
depreciation_wdv(100000, 15, 3)

delayed_interest

Posted: Thu Aug 21, 2025 9:04 am
by admin

Code: Select all

def delayed_interest(amount, rate, delay_days):
    """Calculate simple interest for delay."""
    interest = (amount * rate * delay_days) / (100 * 365)
    return round(interest, 2)

# Example usage
tax_due = 250000
interest = delayed_interest(tax_due, 12, 45)
print("Interest for delay:", interest)

future_value

Posted: Thu Aug 21, 2025 9:09 am
by admin

Code: Select all

def future_value(pv, rate, years):
    """Future Value with compounding annually."""
    fv = pv * ((1 + rate/100) ** years)
    return round(fv, 2)

# Example usage
print("Future Value:", future_value(100000, 8, 5))