Python Function Tutorial – Part V


Visit: QuantInsti

See Part IPart II , Part III and Part IV of this installment to learn more about Python functions.

Python functions with multiple arguments and a return statement

Both versions of the greet Python functions defined above were actually straightforward in terms of functionality that they perform. One more functionality that functions are capable of performing is to return a value to the calling statement using the keyword return. Consider a function that takes several parameters, performs some mathematical calculation on it and returns the output. For example:

# Function with two parameters ‘a’ and ‘b’
def add(a, b):
“””Computes the addition and returns the result.
It does not implement the print statement.”””
result = a + b # Computes addition
return result # Returns the result variable

This user defined function add takes two parameters a and b, sums them together and assigns its output to a variable result and ultimately returns the variable to calling statement as shown below:

# Calling the add function
x = 5
y =
print(f’The addition of {x} and {y} is {add(x, y)}.’)

We call the function add with two arguments x and y (as the function definition has two parameters) initialized with 5 and 6 respectively, and the addition returned by the function gets printed via the print statement as shown below:

The addition of 5 and 6 is 11.

Similarly, Python functions can also return multiple values based on the implementation. The following function demonstrates the same.

# Function definition
def upper_lower(x):
“””Returns the upper and lower version of the string.
The value must be a string, else it will result in an error.
This function does not implement any error handling mechanism.”””
upper = x.upper() # Convert x to upper string
lower = x.lower() # Convert x to lower string
return upper, lower # Return both variables upper and lower

The above upper_lower function takes one argument x (a string) and converts it to their upper and lower versions. Let us call it and see the output.

Note: The function upper_lower implicitly assumes to have a string as a parameter. Providing an integer or float value as an argument while calling will result in an error.

# Calling the function
upper, lower = upper_lower(‘Python’)
# Printing output

Here, the call to upper_lower function has been assigned to two variables upper and lower as the function returns two values which will be unpacked to each variable respectively and the same can be verified in the output shown above.

In the next installment, the author will discuss Python functions with default arguments.

Visit for ready-to-use Python functions as applied in trading and data analysis.

Disclosure: Interactive Brokers

Information posted on IBKR Traders’ Insight that is provided by third-parties and not by Interactive Brokers does NOT constitute a recommendation by Interactive Brokers that you should contract for the services of that third party. Third-party participants who contribute to IBKR Traders’ Insight are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.

This material is from QuantInsti and is being posted with permission from QuantInsti. The views expressed in this material are solely those of the author and/or QuantInsti and IBKR is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to sell or the solicitation of an offer to buy any security. To the extent that this material discusses general market activity, industry or sector trends or other broad based economic or political conditions, it should not be construed as research or investment advice. To the extent that it includes references to specific securities, commodities, currencies, or other instruments, those references do not constitute a recommendation to buy, sell or hold such security. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.

In accordance with EU regulation: The statements in this document shall not be considered as an objective or independent explanation of the matters. Please note that this document (a) has not been prepared in accordance with legal requirements designed to promote the independence of investment research, and (b) is not subject to any prohibition on dealing ahead of the dissemination or publication of investment research.

Any trading symbols displayed are for illustrative purposes only and are not intended to portray recommendations.