Python Function Tutorial – Part II


Visit: QuantInsti

See Part I of this installment to learn more about Python functions.

There are three types of Python functions:

  • Built-in Python functions such as print to print on the standard output device, type to check data type of an object, etc. These are the functions that Python provides to accomplish common tasks.
  • User-Defined Python functions: As the name suggests these are custom functions to help/resolve/achieve a particular task.
  • Anonymous Python functions, also known as lambda functions are custom-made without having any name identifier.

Built-in Python functions

Built-in functions are the ones provided by Python. We will go through a few of them now:

  • type(object) is used to check the data type of an object.
  • float([value]) returns a floating point number constructed from a number or string value.
  • int([value]) returns an integer object constructed from a float or string value, or return 0 if no arguments are given.
  • round(number[, ndigits]) is used to round a float number up to digits specified by ndigits.
  • abs(value) returns the absolute value of a value provided as an argument.
  • format(value[, format_spec]) converts a value to a ‘formatted’ representation, as controlled by format_spec.
  • str([object]) returns a string version of object. If the object is not provided, returns the empty string.
  • bool([value]) return a Boolean value, i.e. one of True or Falsevalue is converted using the standard truth testing procedure[1]. If the value is false or omitted, this returns False; otherwise, it returns True.
  • dir([object]) returns the list of names in the current local scope when an argument is not provided. With an argument, it attempts to return a list of valid attributes for that object.
  • len(object) returns the length (the number of items) of an object. The argument may be a sequence (such as a string, bytes, tuple, list, or range) or a collection (such as a dictionary, set, or frozen set).

It is worth noting that almost all built-in Python functions take one or more arguments, perform the specific operation on it and return the output. We will keep learning about many more built-in Python functions as we progress through our Python learning journey. More information about various built-in functions can be obtained from Python official documentation

In the next installment, the author will discuss the user defined Python functions.

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.