{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Python Functions\n", "## Basic Calculator Function\n", "\n", "This is my first function that show how to pass 2 args and adds them together using a function" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "def myCalc(x,y, tax = \"No\"):\n", " z = x + y\n", " print(z)\n", " print(\"Taxable:\", tax)\n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Call the myCalc function" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10\n", "Taxable: Yes\n" ] } ], "source": [ "myCalc(7,3, \"Yes\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Import Built in Functions from a Built in Module\n", "### These modules and libs are installed by python when you install core" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "# imports\n", "import math" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "# variables\n", "x = 4\n", "y = 9" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The square root of 4 is 2.0\n", "4 raised to the power of 9 is 262144.0\n" ] } ], "source": [ "# operations\n", "print(\"The square root of\", x, \"is\", math.sqrt(x)) #2\n", "print(x, \"raised to the power of\", y, \"is\", math.pow(x,y)) #4**9" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Defined Functions from a Custom Module" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "import myModule as mm # importing a module as a namespace" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Calcuated Values\n", "----------------\n", "\n", "Number of Orders:\n", "20\n", "\n", "End of Report\n" ] } ], "source": [ "mm.reportHeader()\n", "print(\"Number of Orders:\")\n", "mm.myCalcMultiple(5,4)\n", "mm.reportFooter()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Iterative Functions commonly used in loops for, while,switch, ..." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "def create_contact_names(fname, lname, num_contacts):\n", " contact_names = [] # create an empty list to hold the contact names\n", " for contact_name in range(num_contacts): # range is the iterative function\n", " contact_name = fname + \" \" + lname\n", " contact_names.append(contact_name)\n", " # print(contact_names) # functional print coming from sub op of the funct\n", " return contact_names" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['bob smith']\n", "None\n" ] } ], "source": [ "firstName = input(\"Enter first name: \")\n", "lastName = input(\"Enter last name: \")\n", "numContactsReturn = int(input(\"Sample Data Size: \"))\n", "\n", "contact_list = create_contact_names(firstName, lastName, numContactsReturn)\n", "print(contact_list)\n", "\n", "\n" ] } ], "metadata": { "kernelspec": { "display_name": "base", "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.9.12" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }