{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Week 5 Notes - Functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Built-in functions" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "8\n" ] } ], "source": [ "a = pow(2,3)\n", "print(a)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(2+3j)\n" ] } ], "source": [ "b = complex(2,3)\n", "print(b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Functions that are part of Python's Standard Library" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-0.9589242746631385\n" ] } ], "source": [ "from math import sin\n", "\n", "c = sin(5)\n", "print(c)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Functions that are part of external packages" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-0.9589242746631385\n" ] } ], "source": [ "import numpy as np\n", "\n", "d = np.sin(5)\n", "print(d)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Positional Arguments" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(5+3j)\n" ] } ], "source": [ "f = complex(5,3)\n", "print(f)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Keyword Arguments" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(5+3j)\n" ] } ], "source": [ "g = complex(imag=3,real=5)\n", "print(g)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Define a function in Python" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "def plus3(n):\n", " out = n + 3\n", " return out" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n" ] } ], "source": [ "h = plus3(2)\n", "print(h)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "def tri_area(b,h):\n", " area = 0.5*b*h\n", " return area" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5.0\n" ] } ], "source": [ "k = tri_area(2,5)\n", "print(k)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "# Create a function that calculates the distance an object will fall in a certain amount of time\n", "# d = 0.5 * g * t**2\n", "# on earth g=9.81 m/s^2\n", "# on the mooon g=1.62 m/s^2" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "def fall_dist(t,g=9.81):\n", " d = 0.5*g*t**2\n", " return d" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "44.145\n" ] } ], "source": [ "# calculate the distance an object falls on earth in 3 seconds\n", "m = fall_dist(3)\n", "print(m)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "7.290000000000001\n" ] } ], "source": [ "# calculate the distnace an object falls on the moon in 3 seconds\n", "n = fall_dist(3,g=1.62)\n", "print(n)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Including documentation and help in your functions" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on built-in function pow in module builtins:\n", "\n", "pow(x, y, z=None, /)\n", " Equivalent to x**y (with two arguments) or x**y % z (with three arguments)\n", " \n", " Some types, such as ints, are able to use a more efficient algorithm when\n", " invoked using the three argument form.\n", "\n" ] } ], "source": [ "help(pow)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on function fall_dist in module __main__:\n", "\n", "fall_dist(t, g=9.81)\n", "\n" ] } ], "source": [ "help(fall_dist)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [], "source": [ "def fall_dist(t,g=9.81):\n", " \"\"\"\n", " The fall_dist() function calculates the distance an object falls in a given amount of time\n", " \n", " input: time, int or float and optionally g= int, or float for the acceleration due to gravity, default g=9.81\n", " output: fall distance, int or float\n", " \n", " m = fall_dist(3)\n", " print(m)\n", " \n", " 44.145\n", " \n", " n = fall_dist(3,g=1.62)\n", " print(n)\n", " \n", " 7.290000000000001\n", " \n", " \"\"\"\n", " d = 0.5*g*t**2\n", " return d" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on function fall_dist in module __main__:\n", "\n", "fall_dist(t, g=9.81)\n", " The fall_dist() function calculates the distance an object falls in a given amount of time\n", " \n", " input: time, int or float and optionally g= int, or float for the acceleration due to gravity, default g=9.81\n", " output: fall distance, int or float\n", " \n", " m = fall_dist(3)\n", " print(m)\n", " \n", " 44.145\n", " \n", " n = fall_dist(3,g=1.62)\n", " print(n)\n", " \n", " 7.290000000000001\n", "\n" ] } ], "source": [ "help(fall_dist)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Define a function in an external file, import it and run it" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " Volume in drive C is Windows\n", " Volume Serial Number is B899-AB94\n", "\n", " Directory of C:\\Users\\student\\Desktop\n", "\n", "10/22/2019 02:37 PM .\n", "10/22/2019 02:37 PM ..\n", "10/22/2019 02:02 PM .ipynb_checkpoints\n", "10/22/2019 01:32 PM 1,457 Kaltura Capture.lnk\n", "05/13/2016 01:33 PM 210 MSDS Online.url\n", "10/22/2019 02:37 PM 57 myfuncs.py\n", "08/01/2018 04:14 PM Safety Information\n", "10/22/2019 02:32 PM 9,322 Week5_notes_functions.ipynb\n", " 4 File(s) 11,046 bytes\n", " 4 Dir(s) 321,217,835,008 bytes free\n" ] } ], "source": [ "%ls" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import myfuncs\n", "\n", "myfuncs.plus2(3)" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from myfuncs import plus2\n", "\n", "plus2(3)" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import myfuncs\n", "\n", "dir(myfuncs)\n", "myfuncs.plus2(3)" ] }, { "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 }