{ "metadata": { "name": "common-confusions" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Common points of confusion\n", "==========================\n", "Learning to program is hard and there are lots of points of confusion among beginners.\n", "Here are some reminders and demonstrations to help overcome some of the most common." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Functions\n", "---------\n", "\n", "* You have to call/run/execute a function for it to do anything.\n", "* You must store the output of the function to work with it (or use it immediately)\n", "* You must ``return`` something from the function to us it.\n", "Variables inside the function do not exist after the function has finished executing.\n", "This ``return`` is the last thing done by the function.\n", "\n", "\n", "### Python Tutor demo\n", "On it's own, the following function does nothing" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def concatenate(string1, string2):\n", " combined_string = string1 + string2\n", " return combined_string" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we call it but do not do something with the output, it is lost." ] }, { "cell_type": "code", "collapsed": false, "input": [ "concatenate('ethan', 'white')\n", "print combined_string" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'combined_string' is not defined", "output_type": "pyerr", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[0mconcatenate\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'ethan'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'white'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[1;32mprint\u001b[0m \u001b[0mcombined_string\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mNameError\u001b[0m: name 'combined_string' is not defined" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we store the output then we can use it, or we can use it directly." ] }, { "cell_type": "code", "collapsed": false, "input": [ "result = concatenate('ethan', 'white')\n", "print result" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "ethanwhite\n" ] } ], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "print \"The name of our professor is %s\" % concatenate('ethan ', 'white')" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The name of our professor is ethan white\n" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "But, this only works if we have returned the value explicitly." ] }, { "cell_type": "code", "collapsed": false, "input": [ "def concatenate(string1, string2):\n", " combined_string = string1 + string2\n", "\n", "result = concatenate('ethan', 'white')\n", "print result" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "None\n" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "If/elif/else\n", "------------\n", "Only one set of commands in an if/elif/else block will execute. If multiple sets\n", "of conditions are met, then the first set of conditions will be used.\n" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = 10\n", "b = 20\n", "if a > 15:\n", " print 1\n", "elif a == 10 and b < 100:\n", " print 2\n", "elif b == 20:\n", " print 3\n", "else:\n", " print 4" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "2\n" ] } ], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we wanted to actually check all of the conditions, regardless of what had\n", "already happened, then we would just want a series of if statements." ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = 10\n", "b = 20\n", "if a > 15:\n", " print 1\n", "if a == 10 and b < 100:\n", " print 2\n", "if b == 20:\n", " print 3\n", "else:\n", " print 4" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "2\n", "3\n" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "However, in this case the ``else`` will only apply to the last if statement." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Methods\n", "-------\n", "Methods are functions that are attached to particular objects and they operate on those objects.\n", "As a result, sometimes they do not require arguments (but sometimes they do)." ] }, { "cell_type": "code", "collapsed": false, "input": [ "mystring = \"hello world\"\n", "print mystring.upper()\n", "print mystring.count('l')" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "HELLO WORLD\n", "3\n" ] } ], "prompt_number": 8 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### List methods often return ``None``\n", "\n", "In contrast to String methods, methods that work on lists often change the list\n", "and return a special keyword ``None``." ] }, { "cell_type": "code", "collapsed": false, "input": [ "mylist = [1, 3, 4, 2, 7]\n", "print mylist.sort()\n", "print mylist" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "None\n", "[1, 2, 3, 4, 7]\n" ] } ], "prompt_number": 9 }, { "cell_type": "markdown", "metadata": {}, "source": [ "This means that an easy mistake to make is to treat lists methods like we do most things\n", "and end up replacing our list with ``None``." ] }, { "cell_type": "code", "collapsed": false, "input": [ "mylist = [1, 3, 4, 2, 7]\n", "mylist = mylist.sort()\n", "print mylist" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "None\n" ] } ], "prompt_number": 10 } ], "metadata": {} } ] }