{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"# Chapter 4: Mathematical Operators and Functions\n",
"## \n",
"Operators are the commands that manipulate the values of variables, especially for numeric data types.\n",
"- Can be used to change the value of an object using basic math operators ( + - x / )\n",
"- There are also operators, which can truncate, compare, and round.\n",
"- More sophisticated math functions can performed using the numpy module (trigonometry, exponentials/logarithms, inverse trig. and hyperbolic, and other numeric functions.\n",
"\n",
"
\n",
"**Before starting:** Make sure that you open up a Jupyter notebook session using OnDemand so you can interactively follow along with today's lecture! If you have forgotten how to do this, refer to the previous lecture and class notes. Also, be sure to copy this script into your atmos_5340/module_3 subdirectory!\n",
"\n",
"
\n",
"\n",
"# Operators\n",
"You can use Python much like a calculator. Type the following statements and press `enter` to run the line of code: \n",
"\n",
" 1+2\n",
"\n",
" 1/2\n",
"\n",
" 2**2\n",
" \n",
" 5%2\n",
"> ## Do it yourself\n",
"> Try different numbers and find out what each of the operators do.\n",
">- What does the `+` operator do? \n",
">- What does the `-` operator do? \n",
">- What does the `*` operator do? \n",
">- What does the `/` operator do? \n",
">- What does `//` operator do? \n",
">- What does the `**` operator do? \n",
">- What does the `%` \"modulo\" operator do? \n",
"\n",
"[Reference: Python Operators](https://www.tutorialspoint.com/python3/python_basic_operators.htm)\n"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"3 // 2"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Package Version\n",
"----------------------------- ----------------------------\n",
"affine 2.3.1\n",
"aiobotocore 2.3.4\n",
"aiohttp 3.8.1\n",
"aioitertools 0.10.0\n",
"aiosignal 1.2.0\n",
"anyio 3.6.1\n",
"appdirs 1.4.4\n",
"argon2-cffi 21.3.0\n",
"argon2-cffi-bindings 21.2.0\n",
"asttokens 2.0.5\n",
"async-timeout 4.0.2\n",
"attrs 21.4.0\n",
"Babel 2.10.3\n",
"backcall 0.2.0\n",
"backports.functools-lru-cache 1.6.4\n",
"beautifulsoup4 4.11.1\n",
"bleach 5.0.1\n",
"botocore 1.24.21\n",
"Bottleneck 1.3.5\n",
"branca 0.5.0\n",
"brotlipy 0.7.0\n",
"Cartopy 0.20.3\n",
"certifi 2022.6.15.1\n",
"cffi 1.15.0\n",
"cfgrib 0.9.10.1\n",
"cftime 1.6.1\n",
"charset-normalizer 2.1.0\n",
"click 8.1.3\n",
"click-plugins 1.1.1\n",
"cligj 0.7.2\n",
"contextily 1.2.0\n",
"cryptography 37.0.4\n",
"cycler 0.11.0\n",
"debugpy 1.6.0\n",
"decorator 5.1.1\n",
"defusedxml 0.7.1\n",
"descartes 1.1.0\n",
"eccodes 1.4.2\n",
"entrypoints 0.4\n",
"executing 0.8.3\n",
"fastjsonschema 2.16.1\n",
"findlibs 0.0.2\n",
"Fiona 1.8.21\n",
"flit_core 3.7.1\n",
"folium 0.12.1.post1\n",
"fonttools 4.25.0\n",
"frozenlist 1.3.0\n",
"fsspec 2022.5.0\n",
"future 0.18.2\n",
"GDAL 3.5.1\n",
"geographiclib 1.52\n",
"geojson 2.5.0\n",
"geopandas 0.11.0\n",
"geoplot 0.5.1\n",
"geoplotlib 0.3.2\n",
"geopy 2.2.0\n",
"idna 3.3\n",
"importlib-metadata 4.11.4\n",
"importlib-resources 5.8.0\n",
"ipykernel 6.15.1\n",
"ipython 8.4.0\n",
"ipython-genutils 0.2.0\n",
"jedi 0.18.1\n",
"Jinja2 3.1.2\n",
"jmespath 1.0.1\n",
"joblib 1.1.0\n",
"json5 0.9.5\n",
"jsonschema 4.7.2\n",
"jupyter-client 7.3.4\n",
"jupyter_core 4.11.1\n",
"jupyter-server 1.18.1\n",
"jupyterlab 3.4.7\n",
"jupyterlab-pygments 0.2.2\n",
"jupyterlab-server 2.15.0\n",
"kiwisolver 1.4.2\n",
"mapclassify 2.4.3\n",
"MarkupSafe 2.1.1\n",
"matplotlib 3.5.2\n",
"matplotlib-inline 0.1.3\n",
"mercantile 1.2.1\n",
"MetPy 1.3.1\n",
"mistune 0.8.4\n",
"multidict 6.0.2\n",
"munch 2.5.0\n",
"munkres 1.1.4\n",
"nbclassic 0.4.3\n",
"nbclient 0.6.6\n",
"nbconvert 6.5.0\n",
"nbformat 5.4.0\n",
"nest-asyncio 1.5.5\n",
"netCDF4 1.6.0\n",
"networkx 2.8.5\n",
"notebook 6.4.12\n",
"notebook-shim 0.1.0\n",
"numexpr 2.8.3\n",
"numpy 1.22.3\n",
"packaging 21.3\n",
"pandas 1.4.3\n",
"pandocfilters 1.5.0\n",
"parso 0.8.3\n",
"patsy 0.5.2\n",
"pexpect 4.8.0\n",
"pickleshare 0.7.5\n",
"Pillow 9.2.0\n",
"Pint 0.19.2\n",
"pip 22.1.2\n",
"ply 3.11\n",
"pooch 1.6.0\n",
"prometheus-client 0.14.1\n",
"prompt-toolkit 3.0.30\n",
"protobuf 3.20.1\n",
"psutil 5.9.1\n",
"ptyprocess 0.7.0\n",
"pure-eval 0.2.2\n",
"pycparser 2.21\n",
"pyglet 1.5.16\n",
"Pygments 2.12.0\n",
"pygrib 2.1.4\n",
"pyOpenSSL 22.0.0\n",
"pyparsing 3.0.9\n",
"pyproj 3.3.1\n",
"PyQt5 5.15.7\n",
"PyQt5-sip 12.11.0\n",
"pyrsistent 0.18.1\n",
"pyshp 2.3.0\n",
"PySocks 1.7.1\n",
"pyspharm 1.0.9\n",
"python-dateutil 2.8.2\n",
"pytz 2022.1\n",
"pyzmq 23.2.0\n",
"rasterio 1.3.0\n",
"requests 2.28.1\n",
"Rtree 1.0.0\n",
"s3fs 2022.5.0\n",
"scikit-learn 1.1.1\n",
"scipy 1.8.1\n",
"seaborn 0.11.2\n",
"Send2Trash 1.8.0\n",
"setuptools 61.2.0\n",
"Shapely 1.8.2\n",
"sip 6.6.2\n",
"siphon 0.9\n",
"six 1.16.0\n",
"sniffio 1.2.0\n",
"snuggs 1.4.7\n",
"soupsieve 2.3.2.post1\n",
"stack-data 0.3.0\n",
"statsmodels 0.13.2\n",
"terminado 0.15.0\n",
"threadpoolctl 3.1.0\n",
"tinycss2 1.1.1\n",
"toml 0.10.2\n",
"tomli 2.0.1\n",
"tornado 6.1\n",
"traitlets 5.3.0\n",
"typing_extensions 4.3.0\n",
"urllib3 1.26.10\n",
"wcwidth 0.2.5\n",
"webencodings 0.5.1\n",
"websocket-client 1.3.3\n",
"wheel 0.37.1\n",
"windspharm 0+untagged.58.g5bbec33.dirty\n",
"wrapt 1.14.1\n",
"xarray 2022.6.0\n",
"xyzservices 2022.6.0\n",
"yarl 1.7.2\n",
"zipp 3.8.0\n"
]
}
],
"source": [
"!pip list"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4.5"
]
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(9/2)"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1.4142135623730951"
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"2**(1./2.)"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1.4142135623730951"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import numpy as np\n",
"np.sqrt(2)"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"5%3"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"
\n",
"# Variable Assignment\n",
"In math/physics, variables are often part of equations/formulas. In computing languages, variables are much more flexible and can be assigned a value, then reused with a different value, etc.\n",
"\n",
"Variables are names that values are assigned to with the `=` operator. In programing, `=` means \"assigned to\" _not_ \"equals\".\n",
"\n",
"A variable is a value of some quantity, but to describe it requires:\n",
"- a name\n",
"- a storage location\n",
"\n",
"Normally, the variable name references the stored value. Keeping the name and content separately allows the name to be used independently of the information it represents.\n",
"\n",
"When a program executes, the name is bound by the computer source code to a value but the value of the variable may change during program execution.\n",
"\n",
"
\n",
"\n",
"\n",
"|Rules for Variable Names| Example|\n",
"|--|--|\n",
"|Can be a combos of letters and numbers.|`alpha3`, `Timer8`\n",
"|Names are case sensitive.|`Pressure` is different from `pressure` and `PRESSURE`\n",
"|Must start with a letter.| Good: `var1 = 5`
Bad: `1var = 5`\n",
"|Can use underscores.| `potential_temperature`|\n",
"|Should be descriptive.| `temp` or `temperature` is better than `t`.|\n",
"|By convention, use all CAPS for constants:| `TEMP_0C_IN_K = 273.15`\n",
"\n",
"> Note: There are 32 keyword names that are reserved becuase they have special meaning in Python. Variable names cannot be words like `del`, `and`, `if`, `global`, `for`, etc. \n",
">\n",
">[Reference: Python Keywords](https://www.programiz.com/python-programming/keyword-list)\n",
"\n",
"\n",
"We can assign values to a variable and use the variables in expressions.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5.0\n"
]
}
],
"source": [
"# Calculate the hypotenuse of a triangle\n",
"a = 3\n",
"b = 4\n",
"c = (a**2 + b**2)**(1/2)\n",
"print(c)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
\n",
"\n",
"## Do it Yourself #1\n",
"Try assigning a few variables and add the variables together. For this example, let's convert temperature in Celsius to degrees Fahrenheit! Let us know when you are done!"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"30 86.0 90\n"
]
}
],
"source": [
"C = 30\n",
"f = 1.8 * C + 32\n",
"F = 2 * C + 30\n",
"print(C,f,F)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Augmented Assignment Operators\n",
"\n",
"Equal sign (=) assigns a value to a variable. Augmented Assignment Operators are just shorthand to repeat an operation on the original variable as follows\n",
"\n",
"
x += y | \n", "x = x + y | \n", "
x -= y | \n", "x = x - y | \n", "
x *= y | \n", "x = x * y | \n", "
x /= y | \n", "x = x / y | \n", "