{ "cells": [ { "cell_type": "markdown", "id": "collectible-interval", "metadata": {}, "source": [ "# Python Tip #1: Functions" ] }, { "cell_type": "markdown", "id": "warming-wiring", "metadata": {}, "source": [ "Functions are separately defined code snippets that you can then use in your main code." ] }, { "cell_type": "code", "execution_count": 1, "id": "objective-korean", "metadata": {}, "outputs": [], "source": [ "def double(number): # arguments = input\n", " new_number = 2*number\n", " return new_number" ] }, { "cell_type": "code", "execution_count": 2, "id": "metric-monday", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "double(5)" ] }, { "cell_type": "code", "execution_count": 3, "id": "announced-spokesman", "metadata": {}, "outputs": [], "source": [ "def print_hello():\n", " print(\"hello\")" ] }, { "cell_type": "code", "execution_count": 5, "id": "american-contemporary", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello\n" ] } ], "source": [ "print_hello()" ] }, { "cell_type": "code", "execution_count": null, "id": "productive-illness", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 6, "id": "moving-lending", "metadata": {}, "outputs": [], "source": [ "def double(number=7): # number will default to 7 if you don't specify it\n", " new_number = 2*number\n", " return new_number" ] }, { "cell_type": "code", "execution_count": 7, "id": "answering-textbook", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "14" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "double()" ] }, { "cell_type": "code", "execution_count": 8, "id": "assured-regard", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "double(5)" ] }, { "cell_type": "code", "execution_count": 9, "id": "black-sheet", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "double(number=5)" ] }, { "cell_type": "code", "execution_count": 10, "id": "continental-delivery", "metadata": {}, "outputs": [], "source": [ "def double(number): # arguments = input\n", " new_number = 2*number\n", " return new_number" ] }, { "cell_type": "markdown", "id": "greater-motel", "metadata": {}, "source": [ "\"Lambda Functions\" sound very fancy, but they are just a quicker way to define very simple functions." ] }, { "cell_type": "markdown", "id": "serial-explanation", "metadata": {}, "source": [ "```py\n", "double = lambda x : 2*x\n", "```" ] }, { "cell_type": "markdown", "id": "worth-mystery", "metadata": {}, "source": [ "` [name] = lambda [inputs] : [outputs]`" ] }, { "cell_type": "code", "execution_count": 11, "id": "allied-matthew", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "new_double = lambda number : 2*number\n", "new_double(5)" ] }, { "cell_type": "code", "execution_count": 13, "id": "together-frederick", "metadata": {}, "outputs": [], "source": [ "# to do a to the power of b in python, the\n", "# syntax is a ** b, not a ^ b\n", "combine = lambda x, y: 2*x + 3 * y**2" ] }, { "cell_type": "code", "execution_count": 14, "id": "bronze-dynamics", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "22" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "combine(5,2)" ] }, { "cell_type": "code", "execution_count": 26, "id": "turkish-diploma", "metadata": {}, "outputs": [], "source": [ "# default argument with a lambda function\n", "new_combine = lambda x=1, y=1 : 2*x + 3*y**2" ] }, { "cell_type": "code", "execution_count": 30, "id": "8e7d7186", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "22" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "new_combine(5,2)" ] }, { "cell_type": "code", "execution_count": 28, "id": "7ecb8467", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "13" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "new_combine(5)" ] }, { "cell_type": "code", "execution_count": 20, "id": "20fd253a", "metadata": {}, "outputs": [], "source": [ "def example(x, y=3):\n", " return x + y" ] }, { "cell_type": "code", "execution_count": 22, "id": "3a603ad3", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "example(y=1, x=2)" ] }, { "cell_type": "code", "execution_count": null, "id": "30561ecb", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "34788319", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "subtle-prompt", "metadata": {}, "source": [ "They are often useful (as we'll see later) for extracting one component of a tuple or list." ] }, { "cell_type": "code", "execution_count": 31, "id": "found-minute", "metadata": {}, "outputs": [], "source": [ "second_component = lambda r : r[1]" ] }, { "cell_type": "code", "execution_count": 32, "id": "individual-stevens", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-8" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "second_component([5, -8, 1])" ] }, { "cell_type": "markdown", "id": "applied-lawrence", "metadata": {}, "source": [ "This is totally equivalent to:" ] }, { "cell_type": "markdown", "id": "expanded-advice", "metadata": {}, "source": [ "```py\n", "def second_component(r):\n", " return r[1]\n", "```" ] }, { "cell_type": "markdown", "id": "automated-fitting", "metadata": {}, "source": [ "This is mostly useful when you just want to use the function in one spot, and not define it forever." ] }, { "cell_type": "markdown", "id": "fancy-edmonton", "metadata": {}, "source": [ "When sorting a list, you can give it a \"key\" function to tell it what to sort by." ] }, { "cell_type": "code", "execution_count": 33, "id": "interracial-fifteen", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[-5, 1, 0, 7, -10]\n", "[-10, -5, 0, 1, 7]\n" ] } ], "source": [ "L = [-5, 1, 0, 7, -10]\n", "print(L)\n", "L.sort()\n", "print(L)" ] }, { "cell_type": "code", "execution_count": 34, "id": "educated-jurisdiction", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0, 1, -5, 7, -10]\n" ] } ], "source": [ "# this sorts numbers according to their absolute\n", "# value, not the number itself\n", "L.sort(key=lambda x : abs(x))\n", "print(L)" ] }, { "cell_type": "code", "execution_count": null, "id": "useful-container", "metadata": {}, "outputs": [], "source": [ "L" ] }, { "cell_type": "code", "execution_count": null, "id": "pressed-starter", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 36, "id": "analyzed-myanmar", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[(0, 3), (-1, 7), (2, 5)]\n" ] } ], "source": [ "L = [(0, 3), (-1, 7), (2, 5)]\n", "print(L)" ] }, { "cell_type": "code", "execution_count": 37, "id": "aggressive-experience", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[(-1, 7), (0, 3), (2, 5)]" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sorted(L)" ] }, { "cell_type": "code", "execution_count": null, "id": "endless-relation", "metadata": {}, "outputs": [], "source": [ "L" ] }, { "cell_type": "code", "execution_count": 38, "id": "impaired-rover", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[(0, 3), (2, 5), (-1, 7)]" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sorted(L, key=lambda x : x[1])" ] }, { "cell_type": "code", "execution_count": 40, "id": "communist-occasions", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[(0, 3), (2, 5), (-1, 7)]" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# other version\n", "def second_component(x):\n", " return x[1]\n", "sorted(L, key=second_component)" ] }, { "cell_type": "code", "execution_count": 41, "id": "592eb824", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[-3, 4, 0]\n", "[-3, 0, 4]\n" ] } ], "source": [ "# when you do L.sort(), it sorts the variable in place\n", "# when you do sorted(L), it leaves L alone but returns\n", "# a new list that has been sorted\n", "L = [-3, 4, 0]\n", "R = sorted(L)\n", "print(L)\n", "print(R)" ] }, { "cell_type": "code", "execution_count": 42, "id": "9087c0d8", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[-3, 0, 4]\n" ] } ], "source": [ "L = [-3, 4, 0]\n", "L.sort()\n", "print(L)" ] }, { "cell_type": "code", "execution_count": null, "id": "5f8b26c1", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.11.1" } }, "nbformat": 4, "nbformat_minor": 5 }