{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Week 1 Notes: Calculations in Python" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Arithmetic in Python" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 + 2" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 - 2" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "24" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "4 * 6" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.5" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5/2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exponents" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Exponents in Python use the double asterisk symbol ```**``` not the carrot symbol ```^```." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "81" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "9**2" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4.0" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "16**(1/2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Order of Opperations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The order of arithmetic operations in Python is the same as the order of operations in mathematics. Parenthesis ```( )``` can be used to control the order of opperations." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3*(2+1)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.9166666666666665" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3-1/(4+8)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### The modulus or remainder operator" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The modulus or remainder operator in Python is ```%```. The remainder after division of the first number by the second number is returned when ```%``` is used." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5%2" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.040000000000000036" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3.04%1" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.0" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3.0%1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Trig functions in Python's math module" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "note: in Python the pound symbol ```#``` is used to denote comments. Comments are lines of text that can be read by people but are not run as code." ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'sin' is not defined", "output_type": "error", "traceback": [ "\u001b[1;31m--------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0msin\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m30\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mNameError\u001b[0m: name 'sin' is not defined" ] } ], "source": [ "sin(30)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-0.9880316240928618" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import math\n", "\n", "math.sin(30) # sine of angle in radians (not degrees)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-0.27941549819892586" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.sin(6) # once the module is imported, it can be used in the rest of the notebook or .py file" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-0.9880316240928618" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from math import sin\n", "\n", "sin(30)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-0.9880316240928618\n" ] }, { "data": { "text/plain": [ "0.15425144988758405" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from math import sin, cos\n", "\n", "print(sin(30)) # only the output of the last line in a code cell is shown\n", "cos(30)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Logarithms" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.302585092994046" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from math import log, e, log10\n", "\n", "log(10) # natural logarithm" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.0" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "log(e)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "8.477121254719663" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "log10(3*10**8) # base 10 log" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Vairables" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Variables are defined in Python using the equals sign ```=``` also called the assignment operator." ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2226.094855221884\n" ] } ], "source": [ "# Calculate the volumne of a sphere with radius 8.1\n", "import math\n", "\n", "r = 8.1\n", "V = (4/3)*math.pi*r**3\n", "print(V)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.7.0" } }, "nbformat": 4, "nbformat_minor": 2 }