{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Basic Python Functionality \n", "\n", "### 1.1 Arithmetic, and Code\n", "Quantitative information arises everywhere in data science. In addition to representing commands to `print` out lines, expressions can represent numbers and methods of combining numbers. \n", "\n", "The expression `3.2500` evaluates to the number 3.25. (Run the cell and see.)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "3.2500" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We don't necessarily always need to say \"`print`\", because Jupyter always prints the last line in a code cell. If you want to print more than one line, though, do specify \"`print`\"." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(3)\n", "4\n", "5" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Many basic arithmetic operations are built in to Python, like `*` (multiplication), `+` (addition), `-` (subtraction), and `/` (division). There are many others, which you can find information about [here](http://www.inferentialthinking.com/chapters/03/1/expressions.html). Use parenthesis to specify the order of operations, which act according to PEMDAS, just as you may have learned in school. Use parentheses for a happy new year!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "2 + (6 * 5 - (6 * 3)) ** 2 * (( 2 ** 3 ) / 4 * 7)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1.2 Variables\n", "\n", "We sometimes want to work with the result of some computation more than once. To be able to do that without repeating code everywhere we want to use it, we can store it in a variable with *assignment statements*, which have the variable name on the left, an equals sign, and the expression to be evaluated and stored on the right. In the cell below, `(3 * 11 + 5) / 2 - 9` evaluates to 10, and gets stored in the variable `result`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "result = (3 * 11 + 5) / 2 - 9\n", "result" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1.3 Functions\n", "\n", " \n", "One important form of an expression is the call expression, which first names a function and then describes its arguments. The function returns some value, based on its arguments. Some important mathematical functions are:\n", "\n", "| Function | Description |\n", "|----------|---------------------------------------------------------------|\n", "| `abs` | Returns the absolute value of its argument |\n", "| `max` | Returns the maximum of all its arguments |\n", "| `min` | Returns the minimum of all its arguments |\n", "| `round` | Round its argument to the nearest integer |\n", "\n", "Here are two call expressions that both evaluate to 3\n", "\n", "```python\n", "abs(2 - 5)\n", "max(round(2.8), min(pow(2, 10), -1 * pow(2, 10)))\n", "```\n", "\n", "These function calls first evaluate the expressions in the arguments (inside the parentheses), then evaluate the function on the results. `abs(2-5)` evaluates first to `abs(3)`, then returns `3`.\n", "\n", "A **statement** is a whole line of code. Some statements are just expressions, like the examples above, that can be broken down into its subexpressions which get evaluated individually before evaluating the statement as a whole.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1.4 Calling functions\n", "\n", "The most common way to combine or manipulate values in Python is by calling functions. Python comes with many built-in functions that perform common operations.\n", "\n", "For example, the `abs` function takes a single number as its argument and returns the absolute value of that number. The absolute value of a number is its distance from 0 on the number line, so `abs(5)` is 5 and `abs(-5)` is also 5." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "abs(5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "abs(-5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Functions can be called as above, putting the argument in parentheses at the end, or by using \"dot notation\", and calling the function after finding the arguments, as in the cell immediately below." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "nums = [1, 2, 5] # a list of items, in this case, numbers" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "nums.reverse() # reverses the item order\n", "nums" ] } ], "metadata": { "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.6.5" } }, "nbformat": 4, "nbformat_minor": 2 }