In this article, we will create a python module by writing and storing twenty functions into a file ending with the .py extension. The functions included in this file can calculate several types of financial ratios.
The objective of this article is to show the usefulness of python modules and how they can save time when reusing code. In this exercise, we will create a python module that contains several functions. Other programs and scripts can later call these functions to do specific tasks or calculations.
The python module created in this exercise can be useful when writing programs or scripts to automate or conduct financial analysis. You can see an example of how python modules can be helpful by doing the exercise "How to automate a liquidity ratio analysis using python".
Before we begin creating the custom python module, we need to understand what a python module is. We can define a python module as a file with the extension .py that file stores code that can be reused to write other programs and scripts.
A function is a block of related statements that run when called upon and performs a task. In this exercise, our functions will help us break our module into smaller, more manageable pieces of code. Here is an example of a python function:
def get_price_earning_ratio(share_price, earning_per_share):
"""Calculates the Price Earning Ratio. You need to pass two values. The first one is the value for share price and the second value is for the earning per share."""
price_earning_ratio = share_price / earning_per_share
return(price_earning_ratio)
A python function typically contains the following components:
def.def is followed by the name we want to give the function. In our case this name is get_price_earning_ratio.(share_price, earning_per_share).You can learn more about python functions and python modules by reading the python's documentation.
We want to save time writing code when creating scripts that automate financial ratio analysis tasks. We want to be able to reuse code that helps us calculate a list of financial ratios. The list of financial ratios are:
At the end of this exercise, we want to be able to call a function to calculate any one of these ratios when writing other programs or scripts.
The following resources are made available to help you create the financial ratios module:
The instructions for this exercise are straightforward. You need to write a function for each financial ratio and then save the file with the file extension .py. Below the function for each financial ratio.
In this section, we will write the functions for the current ratio, quick ratio, cash ratio, and the operating cash flow ratio.
The function for the current ratio can be written as follows:
def get_current_ratio(current_assets, current_liabilities):
"Returns the current ratio. You will need to pass two values. The first value is for the parameter current assets and a second value for the parameter current liabilities"
current_ratio = current_assets/current_liabilities
return(current_ratio)
The function for the quick ratio or acid test can be written as follows:
def get_quick_ratio(current_assets, inventories, current_liabilities):
"Returns the quick or acid test ratio. Your need to pass three values. The first value is for the parameter current assets, the second value for the parameter inventories, and a third value for the parameter current liabilities"
quick_ratio = (current_assets - inventories)/current_liabilities
return(quick_ratio)
The function for the cash ratio can be written as follows:
def get_cash_ratio(cash, current_liabilities, cash_equivalent=0):
"Returns the cash ratio. Your need to pass atleast two values. The first value for the parameter cash account, the second value for parameter current liabilities, and an optional third value for the parameter cash equivalent assets."
cash_ratio = (cash + cash_equivalent)/current_liabilities
return(cash_ratio)
The function for the operating cash flow ratio can be written as follows:
def get_operating_cashflow_ratio(operating_cashflow, current_liabilities):
"Returns the operating cashflow ratio. Your need to pass at least two values. The first value for the parameter operating cashflow and the second value for parameter current liabilities."
operating_cashflow_ratio = operating_cashflow / current_liabilities
return(operating_cashflow_ratio)
In this section, we will write the functions for the Debt-To-Equity ratio, Debt-To-Total-Assets ratio, Interest Coverage Ratio, and the Debt Service Coverage Ratio.
The function for the debt-to-equity ratio can be written as follows:
def get_debt_to_equity_ratio(total_liabilities, shareholders_equity):
"Calculates the debt to equity ratio. You need to pass atleast two values. The first one is the value for total liabilities and the second value is shareholders equity."
debt_to_equity_ratio = total_liabilities / shareholders_equity
return(debt_to_equity_ratio)
The function for the debt-to-assets ratio can be written as follows:
def get_debt_to_total_assets_ratio(total_liabilities, total_assets):
"Calculates the debt to total assets ratio. You need to pass two values. The first one is the value for total liabilities and the second value is for total assets."
debt_to_total_assets_ratio = total_liabilities / total_assets
return(debt_to_total_assets_ratio)
The function for the interest coverage ratio can be written as follows:
def get_interest_coverage_ratio(operating_income, interest_expenses):
"Calculates the interest coverage ratio. You need to pass two values. The first one is the value for operating income and the second value is for interest expenses."
interest_coverage_ratio = operating_income / interest_expenses
return(interest_coverage_ratio)
The function for the debt service coverage ratio can be written as follows:
def get_debt_service_coverage_ratio(operating_income, total_debt_service):
"Calculates the service coverage ratio. You need to pass two values. The first value is for operating income and the second value is for total_debt_service."
debt_service_coverage_ratio = operating_income / total_debt_service
return(debt_service_coverage_ratio)
In this section, we will write the functions for the asset turnover ratio, inventory turnover ratio, receivable turnover ratio, and days in sales inventory ratio.
The function for the asset turnover ratio can be written as follows:
def get_asset_turnonver_ratio(net_sales, total_assets):
"Calculates the asset turnover ratio. You need to pass two values. The first one is the value for net sales and the second value is for total assets."
asset_turnover_ratio = net_sales / total_assets
return(asset_turnover_ratio)
The function for the inventory turnover ratio can be written as follows:
def get_inventory_turnonver_ratio(cost_of_good_sold, average_inventory):
"Calculates the inventory turnover ratio. You need to pass two values. The first one is the value for cost of goods sold and the average of inventory."
inventory_turnover_ratio = cost_of_good_sold / average_inventory
return(inventory_turnover_ratio)
The function for the receivable turnover ratio can be written as follows:
def get_receivable_turnonver_ratio(net_credit_sales, average_account_receivables):
"Calculates the receivable turnover ratio. You need to pass two values. The first one is the value for Net Credit Sales and the second value is for average account receivables."
receivable_turnover_ratio = net_credit_sales / average_account_receivables
return(receivable_turnover_ratio)
The function for the days in sales inventory ratio can be written as follows:
def get_day_sale_inventory_ratio(cost_of_good_sold, average_inventory):
"Calculates the day sale inventory ratio. You need to pass two values. The first one is the value for cost of good sold and the second value is for average inventory."
day_sale_inventory_ratio = 365 / \
get_inventory_turnonver_ratio(cost_of_good_sold, average_inventory)
return(day_sale_inventory_ratio)
In this section, we will write the functions for the gross margin ratio, operating margin ratio, return on assets ratio, and the return on equity ratio.
The function for the gross margin ratio can be written as follows:
def get_gross_margin_ratio(gross_profit, net_sales):
"Calculates the gross margin ratio. You need to pass two values. The first one is the value for gross profit and the second value is for net sales."
gross_margin_ratio = gross_profit / net_sales
return(gross_margin_ratio)
The function for the operating margin ratio can be written as follows:
def get_operating_margin_ratio(operating_income, net_sales):
"Calculates the operating margin ratio. You need to pass two values. The first one is the value for operating income and the second value is for net sales."
operating_margin_ratio = operating_income / net_sales
return(operating_margin_ratio)
The function for the return on assets ratio can be written as follows:
def get_return_on_asset_ratio(net_income, total_assets):
"Calculates the return on asset ratio. You need to pass two values. The first one is the value for net income and the second value is for total assets."
return_on_assets_ratio = net_income / total_assets
return(return_on_assets_ratio)
The function for the return on equity ratio can be written as follows:
def get_return_on_equity_ratio(net_income, shareholders_equity):
"Calculates the return on equity ratio. You need to pass two values. The first one is the value for net income and the second value is for shareholders equity."
return_on_equity_ratio = net_income / shareholders_equity
return(return_on_equity_ratio)
In this section, we will write the functions for the book value per share ratio, dividend yield ratio, earning per share ratio, and price-earning ratio.
The function for the book value per share ratio can be written as follows:
def get_bookvalue__per_share_ratio(shareholders_equity, total_share_outstanding):
"Calculates the Book Value per Share Ratio. You need to pass two values. The first one is the value for shareholders equity and the second value is for total shares outstanding."
bookvalue_per_share_ratio = shareholders_equity / total_share_outstanding
return(bookvalue_per_share_ratio)
The function for the dividend yield ratio can be written as follows:
def get_dividend_yield_ratio(dividend_per_share, share_price):
"Calculates the Dividend Yield Ratio. You need to pass two values. The first one is the value for dividend per share and the second value is for the share price."
dividend_yield_ratio = dividend_per_share / share_price
return(dividend_yield_ratio)
The function for the earning per share ratio can be written as follows:
def get_earning_per_share_ratio(net_earning, total_share_outstanding):
"Calculates the Earning per Share Ratio. You need to pass two values. The first one is the value for net earning and the second value is for the total sahres outstanding."
earning_per_share_ratio = net_earning / total_share_outstanding
return(earning_per_share_ratio)
The function for the price-earning ratio can be written as follows:
def get_price_earning_ratio(share_price, earning_per_share):
"Calculates the Price Earning Ratio. You need to pass two values. The first one is the value for share price and the second value is for the earning per share."
price_earning_ratio = share_price / earning_per_share
return(price_earning_ratio)
I hope you found this exercise useful. Please remember that there are other types of financial ratios that you can use, so feel free to expand this python module to your liking.
You can also practice using this python module by doing the exercise included in the article "Automate the Calculation of Liquidity Ratios in 10 Simple Steps Using Python".
You can also create other python modules for other financial calculations, different ratios, or indicators. Here are some ideas: