Calculating profit or loss using Python

In this article, we will learn how to create Python programs to calculate profit or loss by comparing cost price and selling price. We'll explore two different approaches to solve this business problem.

Understanding Cost Price, Selling Price, Profit and Loss

Before diving into the code, let's understand the basic concepts ?

The cost price (CP) is the amount paid to purchase a product. The selling price (SP) is the amount received when selling that product.

Profit occurs when selling price is greater than cost price ?

Profit = Selling Price - Cost Price

Loss occurs when cost price is greater than selling price ?

Loss = Cost Price - Selling Price

Method 1: Using Conditional Statements

This approach uses separate functions to calculate profit and loss based on conditions ?

# Function to calculate profit
def calculateProfit(cp, sp):
    profit = sp - cp
    return profit

# Function to calculate loss
def calculateLoss(cp, sp):
    loss = cp - sp
    return loss

# Input values
cost_price = 500
selling_price = 700

# Check conditions and calculate result
if selling_price == cost_price:
    print("Neither profit nor loss")
elif selling_price > cost_price:
    profit = calculateProfit(cost_price, selling_price)
    print("The Profit is", profit)
else:
    loss = calculateLoss(cost_price, selling_price)
    print("The Loss is", loss)

The output of the above code is ?

The Profit is 200

Method 2: Using the abs() Function

This approach uses the abs() function to calculate the absolute difference, eliminating the need for separate profit and loss functions ?

# Function to calculate absolute difference
def calculateDifference(cp, sp):
    difference = abs(sp - cp)
    return difference

# Input values
cost_price = 800
selling_price = 600

# Check conditions and calculate result
if selling_price == cost_price:
    print("Neither profit nor loss")
elif selling_price > cost_price:
    profit = calculateDifference(cost_price, selling_price)
    print("The Profit is", profit)
else:
    loss = calculateDifference(cost_price, selling_price)
    print("The Loss is", loss)

The output of the above code is ?

The Loss is 200

Complete Example with User Input

Here's a practical example that takes user input and calculates profit or loss ?

def calculate_profit_loss():
    # Simulate user input with predefined values
    cost_price = 1200
    selling_price = 1500
    
    print(f"Cost Price: ${cost_price}")
    print(f"Selling Price: ${selling_price}")
    
    if selling_price == cost_price:
        print("Result: Neither profit nor loss")
    elif selling_price > cost_price:
        profit = selling_price - cost_price
        profit_percentage = (profit / cost_price) * 100
        print(f"Result: Profit of ${profit}")
        print(f"Profit Percentage: {profit_percentage:.2f}%")
    else:
        loss = cost_price - selling_price
        loss_percentage = (loss / cost_price) * 100
        print(f"Result: Loss of ${loss}")
        print(f"Loss Percentage: {loss_percentage:.2f}%")

# Call the function
calculate_profit_loss()

The output of the above code is ?

Cost Price: $1200
Selling Price: $1500
Result: Profit of $300
Profit Percentage: 25.00%

Comparison

Method Functions Needed Best For
Conditional Statements 2 (profit & loss) Clear separation of logic
abs() Function 1 (difference) Code simplicity

Conclusion

Both methods effectively calculate profit or loss. The abs() function approach is more concise, while separate functions provide clearer logic separation. Choose based on your specific requirements and code organization preferences.

Updated on: 2026-03-26T23:48:55+05:30

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements