diff --git a/_sources/materials/08-Loops.ipynb b/_sources/materials/08-Loops.ipynb index 221acc28a..41193ff2e 100644 --- a/_sources/materials/08-Loops.ipynb +++ b/_sources/materials/08-Loops.ipynb @@ -1,5 +1,21 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Course Announcements**\n", + "\n", + "Due Date:\n", + "- **CL4** due Friday (11:59 PM)\n", + "- **A3** due next Tuesday (11:59 PM) \n", + "\n", + "Notes:\n", + "- Apologies for the take-home exam error\n", + "- A2 Scores have been posted\n", + "- CL3 answer key posted" + ] + }, { "cell_type": "markdown", "metadata": { diff --git a/_sources/materials/09-Methods.ipynb b/_sources/materials/09-Methods.ipynb new file mode 100644 index 000000000..a687d0a88 --- /dev/null +++ b/_sources/materials/09-Methods.ipynb @@ -0,0 +1,901 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Course Announcements**\n", + "\n", + "Due Date:\n", + "- **CL4** due tomorrow (11:59 PM)\n", + "- **A3** due Tuesday (11:59 PM) \n", + "\n", + "Notes:\n", + "- Exam scores (in-class & take-home) will be posted later today" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "# Methods\n", + "\n", + "- string, list, & dictionary\n", + "- in place vs not in place\n", + "- relationship to functions" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "#### Clicker Question #1\n", + "\n", + "What will the following code snippet print?" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "def my_func(my_dictionary):\n", + " \n", + " output = []\n", + "\n", + " for item in my_dictionary:\n", + " value = my_dictionary[item]\n", + " output.append(value) #append method adds an item to end of a list\n", + " \n", + " return output\n", + "\n", + "# create dictionary and execute function\n", + "dictionary = {'first' : 1, 'second' : 2, 'third' : 3}\n", + "out = my_func(dictionary)\n", + "\n", + "print(out)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "source": [ + "- A) ['first', 'second', 'third'] \n", + "- B) {1, 2, 3}\n", + "- C) ['first', 1, 'second', 2, 'third', 3] \n", + "- D) [1, 2, 3] \n", + "- E) None" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true, + "jupyter": { + "outputs_hidden": true + }, + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Methods" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "source": [ + "
\n", + "Methods are functions that are defined and called directly on an object. \n", + "
\n", + "\n", + "
\n", + "For our purposes, objects are any data variable. \n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "### Method Examples\n", + "\n", + "A method is a function applied directly to the object you call it on." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "source": [ + "General form of a method:\n", + "\n", + "```python\n", + "object.method()\n", + "```" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "source": [ + "In other words: methods \"belong to\" an object." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "# The `append` method, defined on lists\n", + "my_list = [1, 2, 3]\n", + "my_list.append(4)\n", + "print(my_list)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "source": [ + "The method `append()` is called directly on the list `my_list`" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "# append is a method for lists\n", + "# this will error with a string\n", + "my_string = 'cogs18'\n", + "my_string.append('!')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "# The `is_integer()` method, defined on floats\n", + "my_float = 12.2\n", + "my_float.is_integer()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "# The `is_integer()` method, attempted on an integer\n", + "# this code will produce an error\n", + "my_int = 12\n", + "my_int.is_integer()" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## String Methods\n", + "\n", + "There are a whole bunch of string methods, all described [here](https://www.w3schools.com/python/python_ref_string.asp). We'll review a few of the most commonly used here." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "# Make a string all lower case\n", + "'aBc'.lower()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "# Make a string all upper case\n", + "'aBc'.upper()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "# Capitalize a string\n", + "'python is great'.capitalize()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "# Find the index of where a string starts \n", + "'Hello, my name is'.find('name')" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "#### Clicker Question #2\n", + "\n", + "What will the following code snippet print out?" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "inputs = ['fIx', 'tYpiNg', 'lIkE', 'tHiS']\n", + "output = ''\n", + "\n", + "for element in inputs:\n", + " output = output + element.lower() + ' '\n", + "\n", + "output.capitalize()" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "source": [ + "- A) 'fix typing like this ' \n", + "- B) ['fix', 'typing', 'like', 'this'] \n", + "- C) 'Fix typing like this ' \n", + "- D) 'Fix typing like this' \n", + "- E) 'Fixtypinglikethis'" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## List Methods\n", + "\n", + "There are also a bunch of list methods, all described [here](https://www.w3schools.com/python/python_ref_list.asp). You've seen some of these before, but we'll review a few of the most commonly used here." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "# sort sorts integers in numerical orders\n", + "ints = [16, 88, 33, 40]\n", + "ints.sort()\n", + "ints" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "ints.sort(reverse=True)\n", + "ints" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "# append adds to the end of a list\n", + "ints.append(2)\n", + "ints" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "scrolled": true, + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "# remove value from list\n", + "ints.remove(40)\n", + "ints" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "list.remove?" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "scrolled": true, + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "# reverse order of list\n", + "ints.reverse()\n", + "ints" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "#### Clicker Question #3\n", + "\n", + "What will the following code snippet print out?" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "list_string = ['a', 'c', 'd', 'b']\n", + "list_string.sort()\n", + "list_string.reverse()\n", + "list_string" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "source": [ + "- A) ['a', 'c', 'd', 'b']\n", + "- B) ['a', 'b', 'c', 'd']\n", + "- C) ['d', 'c', 'b', 'a'] \n", + "- D) ['d', 'b', 'a', 'c'] \n", + "- E) ['d', 'a', 'b', 'c']" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Dictionary Methods\n", + "\n", + "As with string and list methods, there are many described [here](https://www.w3schools.com/python/python_ref_dictionary.asp) that are helpful when working with dictionaries.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "car = {\n", + " \"brand\": \"BMW\",\n", + " \"model\": \"M5\",\n", + " \"year\": 2019\n", + "}\n", + "\n", + "# keys() returns the keys of a dictionary\n", + "car.keys()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "# get returns the value of a specified key\n", + "mod = car.get('model')\n", + "\n", + "# equivalent\n", + "mod2 = car['model']\n", + "\n", + "print(mod)\n", + "print(mod2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "slideshow": { + "slide_type": "skip" + } + }, + "outputs": [], + "source": [ + "# previously done this by indexing\n", + "print(car['model'])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "# update adds a key-value pair\n", + "car.update({\"color\": \"Black\"})\n", + "\n", + "print(car) " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "#### Clicker Question #4\n", + "\n", + "Assuming `dictionary` is a dictionary that exists, what would the following accomplish:\n", + "\n", + "```python\n", + "\n", + "dictionary.get('letter')\n", + "\n", + "```\n", + "\n", + "- A) Return the key for the value 'letter' from `dictionary`\n", + "- B) Add the key 'letter' to `dictionary`\n", + "- C) Add the value 'letter' to `dictionary`\n", + "- D) Return the value for the key 'letter' from `dictionary`\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "#### Clicker Question #5\n", + "\n", + "Which method would you use to add a new key-value pair to a dictionary?\n", + "\n", + "- A) `.append()`\n", + "- B) `.get()`\n", + "- C) `.keys()` \n", + "- D) `.update()`\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "### Methods: In Place vs Not In Place" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "source": [ + "
\n", + "Some methods update the object directly (in place), whereas others return an updated version of the input. \n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "#### List methods that are in place" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "scrolled": true, + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "# Reverse a list\n", + "my_list = ['a', 'b', 'c']\n", + "my_list.reverse()\n", + "\n", + "print(my_list)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "# Sort a list\n", + "my_numbers = [13, 3, -1]\n", + "my_numbers.sort()\n", + "\n", + "print(my_numbers)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "#### Dictionary methods that are not in place" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "car" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "# Return the keys in the dictionary\n", + "out = car.keys() " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "# print keys\n", + "print(type(out))\n", + "print(out)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "# car has not changed\n", + "print(type(car))\n", + "print(car)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "# Return the values in the dicionary\n", + "car.values()" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Finding Methods" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "source": [ + "Typing the object/variable name you want to find methods for followed by a '.' and then pressing tab will display all the methods available for that type of object." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "# Define a test string\n", + "my_string = 'Python'" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "# See all the available methods on an object with tab complete\n", + "my_string." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "source": [ + "Using the function `dir()` returns all methods available" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "# For our purposes now, you can ignore any leading underscores (these are special methods)\n", + "dir(my_string)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Correspondance Between Functions & Methods" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "source": [ + "**All methods are functions**. Methods are special functions *attached* to a variable type. All functions are NOT methods. \n", + "\n", + "Note that:\n", + "\n", + "```python\n", + "my_variable.function_call(...)\n", + "```\n", + "\n", + "acts like:\n", + "\n", + "```python\n", + "function_call(my_variable, ...)\n", + "```\n", + "\n", + "A function that we can call directly on a variable (a method) acts like a shortcut for passing that variable into a function. " + ] + } + ], + "metadata": { + "celltoolbar": "Slideshow", + "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.8" + }, + "rise": { + "scroll": true + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/assets/intro/assignments/overview.html b/assets/intro/assignments/overview.html index 36a1f9b8c..48238aa9f 100644 --- a/assets/intro/assignments/overview.html +++ b/assets/intro/assignments/overview.html @@ -185,6 +185,7 @@
  • 06-Conditionals
  • 07-Collection
  • 08-Loops
  • +
  • 09-Methods
  • Coding Labs

    Coding Labs

    Coding Labs

    Coding Labs

    Coding Labs

    Coding Labs

    Coding Labs

    Coding Labs

    Coding Labs

    Coding Labs

    Coding Labs

    Coding Labs

    Coding Labs