Skip to content

Instantly share code, notes, and snippets.

@ctb
Created September 28, 2012 01:17
Show Gist options
  • Save ctb/3797440 to your computer and use it in GitHub Desktop.
Save ctb/3797440 to your computer and use it in GitHub Desktop.
{
"metadata": {
"name": "lecture-week3-questions"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": "## Bird list stuff\n\nFor the dictionary lecture (lecture-week03-dictionaries), I wrote this code:"
},
{
"cell_type": "code",
"collapsed": false,
"input": "birdlist = ['swan', 'egret', 'egret', 'robin', 'robin', 'swan', 'swan', 'raven']\n\ndef another_bird(result_list, new_bird_name):\n for i in range(len(result_list)):\n if result_list[i][0] == new_bird_name:\n result_list[i][1] += 1\n return\n result_list.append([new_bird_name, 1])\n",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": "This code lets you add in results one at a time to the 'result_list':"
},
{
"cell_type": "code",
"collapsed": false,
"input": "\nresult_list = []\nanother_bird(result_list, 'swan')\nprint result_list\n\nanother_bird(result_list, 'swan')\nprint result_list\n\nanother_bird(result_list, 'egret')\nprint result_list\n\n",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "[['swan', 1]]\n[['swan', 2]]\n[['swan', 2], ['egret', 1]]\n"
}
],
"prompt_number": 6
},
{
"cell_type": "markdown",
"metadata": {},
"source": "Abhijna asked: why do we have this for loop? :\n\n for i in range(len(result_list)):\n if result_list[i][0] == new_bird_name:\n result_list[i][1] += 1\n return\n\ninstead of:\n\n for (bird, count) in result_list:\n if bird == new_bird_name:\n count += 1\n\n?\n\nThe reason is that 'count' no longer refers to the count contained in 'result_list' -- it refers to the number that *was* contained in result_list. If you increment it you're not incrementing the number stored in the list.\n\nTo do that, you have to have the index and refer directly to the element in the list, e.g. result_list[i][1]."
},
{
"cell_type": "markdown",
"metadata": {},
"source": "## Side effects\n\nOn one of the slides from the quizlet two Monday's ok, I wrote:\n"
},
{
"cell_type": "code",
"collapsed": false,
"input": "z = [5, 6, 7]\ndef fn(aaa):\n aaa = list(aaa)\n aaa.append(8)\n return aaa\n\nfn(z)\n",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 9,
"text": "[5, 6, 7, 8]"
}
],
"prompt_number": 9
},
{
"cell_type": "markdown",
"metadata": {},
"source": "and asked: what is the value of 'z' at the end of this code?\n\nEveryone said [5,6,7, 8]. But that's not right:"
},
{
"cell_type": "code",
"collapsed": false,
"input": "z",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 10,
"text": "[5, 6, 7]"
}
],
"prompt_number": 10
},
{
"cell_type": "markdown",
"metadata": {},
"source": "What's actually happening here is that 'z' is copied within the function 'fn' and then returned. So the return value of the *function* is [5,6,7,8] but 'z' is unchanged:"
},
{
"cell_type": "code",
"collapsed": false,
"input": "z = [5, 6, 7]\nprint 'z before fn is:', z\nprint 'fn returns:', fn(z)\nprint 'z after fn is:', z",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": "z before fn is: [5, 6, 7]\nfn returns: [5, 6, 7, 8]\nz after fn is: [5, 6, 7]\n"
}
],
"prompt_number": 11
},
{
"cell_type": "markdown",
"metadata": {},
"source": "If you change 'fn' to look like this:"
},
{
"cell_type": "code",
"collapsed": false,
"input": "z = [5, 6, 7]\ndef fn2(aaa):\n bbb = list(aaa)\n bbb.append(8)\n return bbb\n\nfn2(z)\n",
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 12,
"text": "[5, 6, 7, 8]"
}
],
"prompt_number": 12
},
{
"cell_type": "markdown",
"metadata": {},
"source": "maybe the behavior is clearer?"
},
{
"cell_type": "code",
"collapsed": false,
"input": "",
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment