{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Call Expressions\n", "\n", "*Call expressions* invoke functions, which are named operations. The name of the function appears first, followed by expressions in parentheses. " ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "12" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "abs(-12)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "round(5 - 1.3)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "max(2, 2 + 3, 4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this last example, the `max` function is *called* on three *arguments*: 2, 5, and 4. The value of each expression within parentheses is passed to the function, and the function *returns* the final value of the full call expression. The `max` function can take any number of arguments and returns the maximum.\n", "\n", "A few functions are available by default, such as `abs` and `round`, but most functions that are built into the Python language are stored in a collection of functions called a *module*. An *import statement* is used to provide access to a module, such as `math` or `operator`." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3.0" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import math\n", "import operator\n", "math.sqrt(operator.add(4, 5))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "An equivalent expression could be expressed using the `+` and `**` operators instead." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3.0" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(4 + 5) ** 0.5" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Operators and call expressions can be used together in an expression. The *percent difference* between two values is used to compare values for which neither one is obviously `initial` or `changed`. For example, in 2014 Florida farms produced 2.72 billion eggs while Iowa farms produced 16.25 billion eggs (http://quickstats.nass.usda.gov/). The percent difference is 100 times the absolute value of the difference between the values, divided by their average. In this case, the difference is larger than the average, and so the percent difference is greater than 100." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "142.6462836056932" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "florida = 2.72\n", "iowa = 16.25\n", "100*abs(florida-iowa)/((florida+iowa)/2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Learning how different functions behave is an important part of learning a programming language. A Jupyter notebook can assist in remembering the names and effects of different functions. When editing a code cell, press the *tab* key after typing the beginning of a name to bring up a list of ways to complete that name. For example, press *tab* after `math.` to see all of the functions available in the `math` module. Typing will narrow down the list of options. To learn more about a function, place a `?` after its name. For example, typing `math.log?` will bring up a description of the `log` function in the `math` module." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "math.log?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " log(x[, base])\n", "\n", " Return the logarithm of x to the given base.\n", " If the base not specified, returns the natural logarithm (base e) of x." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The square brackets in the example call indicate that an argument is optional. That is, `log` can be called with either one or two arguments." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4.0" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.log(16, 2)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4.0" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.log(16)/math.log(2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The list of [Python's built-in functions](https://docs.python.org/3/library/functions.html) is quite long and includes many functions that are never needed in data science applications. The list of [mathematical functions in the `math` module](https://docs.python.org/3/library/math.html) is similarly long. This text will introduce the most important functions in context, rather than expecting the reader to memorize or understand these lists." ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.5" } }, "nbformat": 4, "nbformat_minor": 2 }