{
 "metadata": {
  "name": "",
  "signature": "sha256:1a71ccc70829239143d02cebcb97bec031b45e676ebad340fc04c9bd4a5760bf"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "[Sebastian Raschka](http://sebastianraschka.com)  \n",
      "\n",
      "last updated 05/27/2014\n",
      "\n",
      "- [Open in IPython nbviewer](http://nbviewer.ipython.org/github/rasbt/python_reference/blob/master/tutorials/key_differences_between_python_2_and_3.ipynb?create=1) \n",
      "\n",
      "- [Link to this IPython notebook on Github](https://github.com/rasbt/python_reference/blob/master/tutorials/key_differences_between_python_2_and_3.ipynb)  \n",
      "\n",
      "- [Link to the GitHub repository python_reference](https://github.com/rasbt/python_reference)\n"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<hr>\n",
      "I would be happy to hear your comments and suggestions.  \n",
      "Please feel free to drop me a note via\n",
      "[twitter](https://twitter.com/rasbt), [email](mailto:bluewoodtree@gmail.com), or [google+](https://plus.google.com/118404394130788869227).\n",
      "<hr>"
     ]
    },
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Key differences between Python 2.7.x and Python 3.x"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<br>\n",
      "Many beginning Python users are wondering with which version of Python they should start. My answer to this question is usually something along the lines \"just go with the version your favorite tutorial was written in, and check out the differences later on.\"\n",
      "\n",
      "But what if you are starting a new project and have the choice to pick? I would say there is currently no \"right\" or \"wrong\" as long as both Python 2.7.x and Python 3.x support the libraries that you are planning to use. However, it is worthwhile to have a look at the major differences between those two most popular versions of Python to avoid common pitfalls when writing the code for either one of them, or if you are planning to port your project."
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<br>\n",
      "<br>"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Sections"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "\n",
      "- [Using the `__future__` module](#future_module)\n",
      "\n",
      "- [The print function](#The-print-function)\n",
      "\n",
      "- [Integer division](#Integer-division)\n",
      "\n",
      "- [Unicode](#Unicode)\n",
      "\n",
      "- [xrange](#xrange)\n",
      "\n",
      "- [Raising exceptions](#Raising-exceptions)\n",
      "\n",
      "- [Handling exceptions](#Handling-exceptions)\n",
      "\n",
      "- [The next() function and .next() method](#The-next-function-and-next-method)\n",
      "\n",
      "- [For-loop variables and the global namespace leak](#For-loop-variables-and-the-global-namespace-leak)\n",
      "\n",
      "- [Comparing unorderable types](#Comparing-unorderable-types)\n",
      "\n",
      "- [Parsing user inputs via input()](#Parsing-user-inputs-via-input)\n",
      "\n",
      "- [Returning iterable objects instead of lists](#Returning-iterable-objects-instead-of-lists)\n",
      "\n",
      "- [More articles about Python 2 and Python 3](#More-articles-about-Python-2-and-Python-3)"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<a id='future_module'></a>"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<br>\n",
      "<br>"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "### The `__future__` module"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Python 3.x introduced some Python 2-incompatible keywords and features that can be imported via the in-built `__future__` module in Python 2. It is recommended to use `__future__` imports it if you are planning Python 3.x support for your code. For example, if we want Python 3.x's integer division behavior in Python 2, we can import it via\n",
      "\n",
      "    from __future__ import division\n",
      "    \n",
      "More features that can be imported from the `__future__` module are listed in the table below:"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<table>\n",
      "<tr><th class=\"head\">feature</th>\n",
      "<th class=\"head\">optional in</th>\n",
      "<th class=\"head\">mandatory in</th>\n",
      "<th class=\"head\">effect</th>\n",
      "</tr>\n",
      "\n",
      "<tr><td>nested_scopes</td>\n",
      "<td>2.1.0b1</td>\n",
      "<td>2.2</td>\n",
      "<td><a href=\"http://www.python.org/dev/peps/pep-0227\"><strong>PEP 227</strong></a>:\n",
      "<em>Statically Nested Scopes</em></td>\n",
      "</tr>\n",
      "<tr><td>generators</td>\n",
      "<td>2.2.0a1</td>\n",
      "<td>2.3</td>\n",
      "<td><a href=\"http://www.python.org/dev/peps/pep-0255\"><strong>PEP 255</strong></a>:\n",
      "<em>Simple Generators</em></td>\n",
      "</tr>\n",
      "<tr><td>division</td>\n",
      "<td>2.2.0a2</td>\n",
      "<td>3.0</td>\n",
      "<td><a href=\"http://www.python.org/dev/peps/pep-0238\"><strong>PEP 238</strong></a>:\n",
      "<em>Changing the Division Operator</em></td>\n",
      "</tr>\n",
      "<tr><td>absolute_import</td>\n",
      "<td>2.5.0a1</td>\n",
      "<td>3.0</td>\n",
      "<td><a href=\"http://www.python.org/dev/peps/pep-0328\"><strong>PEP 328</strong></a>:\n",
      "<em>Imports: Multi-Line and Absolute/Relative</em></td>\n",
      "</tr>\n",
      "<tr><td>with_statement</td>\n",
      "<td>2.5.0a1</td>\n",
      "<td>2.6</td>\n",
      "<td><a href=\"http://www.python.org/dev/peps/pep-0343\"><strong>PEP 343</strong></a>:\n",
      "<em>The &#8220;with&#8221; Statement</em></td>\n",
      "</tr>\n",
      "<tr><td>print_function</td>\n",
      "<td>2.6.0a2</td>\n",
      "<td>3.0</td>\n",
      "<td><a href=\"http://www.python.org/dev/peps/pep-3105\"><strong>PEP 3105</strong></a>:\n",
      "<em>Make print a function</em></td>\n",
      "</tr>\n",
      "<tr><td>unicode_literals</td>\n",
      "<td>2.6.0a2</td>\n",
      "<td>3.0</td>\n",
      "<td><a href=\"http://www.python.org/dev/peps/pep-3112\"><strong>PEP 3112</strong></a>:\n",
      "<em>Bytes literals in Python 3000</em></td>\n",
      "</tr>\n",
      "</table>\n",
      "<br>\n",
      "<center>(Source: [https://docs.python.org/2/library/__future__.html](https://docs.python.org/2/library/__future__.html#module-__future__))</center>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from platform import python_version"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 1
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<br>\n",
      "<br>"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "The print function"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "[[back to the section-overview](#Sections)]"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Very trivial, and the change in the print-syntax is probably the most widely known change, but still it is worth mentioning: Python 2's print statement has been replaced by the `print()` function, meaning that we have to wrap the object that we want to print in parantheses. \n",
      "\n",
      "Python 2 doesn't have a problem with additional parantheses, but in contrast, Python 3 would raise a `SyntaxError` if we called the print function the Python 2-way without the parentheses.  \n"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "#### Python 2"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print 'Python', python_version()\n",
      "print 'Hello, World!'\n",
      "print('Hello, World!')\n",
      "print \"text\", ; print 'print more text on the same line'"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Python 2.7.6\n",
        "Hello, World!\n",
        "Hello, World!\n",
        "text print more text on the same line\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<br>"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "#### Python 3"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print('Python', python_version())\n",
      "print('Hello, World!')\n",
      "\n",
      "print(\"some text,\", end=\"\") \n",
      "print(' print more text on the same line')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Python 3.4.1\n",
        "Hello, World!\n",
        "some text, print more text on the same line\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print 'Hello, World!'"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "ename": "SyntaxError",
       "evalue": "invalid syntax (<ipython-input-3-139a7c5835bd>, line 1)",
       "output_type": "pyerr",
       "traceback": [
        "\u001b[0;36m  File \u001b[0;32m\"<ipython-input-3-139a7c5835bd>\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m    print 'Hello, World!'\u001b[0m\n\u001b[0m                        ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "**Note:**\n",
      "\n",
      "Printing \"Hello, World\" above  via Python 2 looked quite \"normal\". However, if we have multiple objects inside the parantheses, we will create a tuple, since `print` is a \"statement\" in Python 2, not a function call."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print 'Python', python_version()\n",
      "print('a', 'b')\n",
      "print 'a', 'b'"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Python 2.7.7\n",
        "('a', 'b')\n",
        "a b\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<br>\n",
      "<br>"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Integer division"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "[[back to the section-overview](#Sections)]"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "This change is particularly dangerous if you are porting code, or if you are executing Python 3 code in Python 2, since the change in integer-division behavior can often go unnoticed (it doesn't raise a `SyntaxError`).  \n",
      "So, I still tend to use a `float(3)/2` or `3/2.0` instead of a `3/2` in my Python 3 scripts to save the Python 2 guys some trouble (and vice versa, I recommend a `from __future__ import division` in your Python 2 scripts)."
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "#### Python 2"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print 'Python', python_version()\n",
      "print '3 / 2 =', 3 / 2\n",
      "print '3 // 2 =', 3 // 2\n",
      "print '3 / 2.0 =', 3 / 2.0\n",
      "print '3 // 2.0 =', 3 // 2.0"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Python 2.7.6\n",
        "3 / 2 = 1\n",
        "3 // 2 = 1\n",
        "3 / 2.0 = 1.5\n",
        "3 // 2.0 = 1.0\n"
       ]
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<br>"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "#### Python 3"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print('Python', python_version())\n",
      "print('3 / 2 =', 3 / 2)\n",
      "print('3 // 2 =', 3 // 2)\n",
      "print('3 / 2.0 =', 3 / 2.0)\n",
      "print('3 // 2.0 =', 3 // 2.0)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Python 3.4.1\n",
        "3 / 2 = 1.5\n",
        "3 // 2 = 1\n",
        "3 / 2.0 = 1.5\n",
        "3 // 2.0 = 1.0\n"
       ]
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<br>\n",
      "<br>"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Unicode"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "[[back to the section-overview](#Sections)]"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Python 2 has ASCII `str()` types, separate `unicode()`, but no `byte` type.  \n",
      "\n",
      "Now, in Python 3, we finally have Unicode (utf-8) `str`ings, and 2 byte classes: `byte` and `bytearray`s."
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "#### Python 2"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print 'Python', python_version()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Python 2.7.6\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print type(unicode('this is like a python3 str type'))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "<type 'unicode'>\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print type(b'byte type does not exist')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "<type 'str'>\n"
       ]
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print 'they are really' + b' the same'"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "they are really the same\n"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print type(bytearray(b'bytearray oddly does exist though'))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "<type 'bytearray'>\n"
       ]
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<br>"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "#### Python 3"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print('Python', python_version())\n",
      "print('strings are now utf-8 \\u03BCnico\\u0394\u00e9!')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Python 3.4.1\n",
        "strings are now utf-8 \u03bcnico\u0394\u00e9!\n"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print('Python', python_version(), end=\"\")\n",
      "print(' has', type(b' bytes for storing data'))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Python 3.4.1 has <class 'bytes'>\n"
       ]
      }
     ],
     "prompt_number": 8
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print('and Python', python_version(), end=\"\")\n",
      "print(' also has', type(bytearray(b'bytearrays')))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "and Python 3.4.1 also has <class 'bytearray'>\n"
       ]
      }
     ],
     "prompt_number": 11
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "'note that we cannot add a string' + b'bytes for data'"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "ename": "TypeError",
       "evalue": "Can't convert 'bytes' object to str implicitly",
       "output_type": "pyerr",
       "traceback": [
        "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
        "\u001b[0;32m<ipython-input-13-d3e8942ccf81>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;34m'note that we cannot add a string'\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;34mb'bytes for data'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
        "\u001b[0;31mTypeError\u001b[0m: Can't convert 'bytes' object to str implicitly"
       ]
      }
     ],
     "prompt_number": 13
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<br>\n",
      "<br>"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "xrange"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "[[back to the section-overview](#Sections)]"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      " \n",
      "The usage of `xrange()` is very popular in Python 2.x for creating an iterable object, e.g., in a for-loop or list/set-dictionary-comprehension.  \n",
      "The behavior was quite similar to a generator (i.e., \"lazy evaluation\"), but here the xrange-iterable is not exhaustible - meaning, you could iterate over it infinitely.  \n",
      "\n",
      "\n",
      "Thanks to its \"lazy-evaluation\", the advantage of the regular `range()` is that `xrange()` is generally faster if you have to iterate over it only once (e.g., in a for-loop). However, in contrast to 1-time iterations, it is not recommended if you repeat the iteration multiple times, since the generation happens every time from scratch!  \n",
      "\n",
      "In Python 3, the `range()` was implemented like the `xrange()` function so that a dedicated `xrange()` function does not exist anymore (`xrange()` raises a `NameError` in Python 3)."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import timeit\n",
      "\n",
      "n = 10000\n",
      "def test_range(n):\n",
      "    return for i in range(n):\n",
      "        pass\n",
      "    \n",
      "def test_xrange(n):\n",
      "    for i in xrange(n):\n",
      "        pass    "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 5
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<br>"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "#### Python 2"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print 'Python', python_version()\n",
      "\n",
      "print '\\ntiming range()'\n",
      "%timeit test_range(n)\n",
      "\n",
      "print '\\n\\ntiming xrange()'\n",
      "%timeit test_xrange(n)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Python 2.7.6\n",
        "\n",
        "timing range()\n",
        "1000 loops, best of 3: 433 \u00b5s per loop"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "\n",
        "\n",
        "timing xrange()\n",
        "1000 loops, best of 3: 350 \u00b5s per loop"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<br>"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "#### Python 3"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print('Python', python_version())\n",
      "\n",
      "print('\\ntiming range()')\n",
      "%timeit test_range(n)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Python 3.4.1\n",
        "\n",
        "timing range()\n",
        "1000 loops, best of 3: 520 \u00b5s per loop"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n"
       ]
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print(xrange(10))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "ename": "NameError",
       "evalue": "name 'xrange' is not defined",
       "output_type": "pyerr",
       "traceback": [
        "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
        "\u001b[0;32m<ipython-input-5-5d8f9b79ea70>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mxrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
        "\u001b[0;31mNameError\u001b[0m: name 'xrange' is not defined"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<a id='contains'></a>\n",
      "<br>\n",
      "<br>\n"
     ]
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "The `__contains__` method for `range` objects in Python 3"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Another thing worth mentioning is that `range` got a \"new\" `__contains__` method in Python 3.x (thanks to [Yuchen Ying](https://github.com/yegle), who pointed this out).  The `__contains__` method can speedup \"look-ups\" in Python 3.x `range` significantly for integer and Boolean types.\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "x = 10000000"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 3
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def val_in_range(x, val):\n",
      "    return val in range(x)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 4
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def val_in_xrange(x, val):\n",
      "    return val in xrange(x)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 5
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print('Python', python_version())\n",
      "assert(val_in_range(x, x/2) == True)\n",
      "assert(val_in_range(x, x//2) == True)\n",
      "%timeit val_in_range(x, x/2)\n",
      "%timeit val_in_range(x, x//2)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Python 3.4.1\n",
        "1 loops, best of 3: 742 ms per loop"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "1000000 loops, best of 3: 1.19 \u00b5s per loop"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n"
       ]
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Based on the `timeit` results above, you see that the execution for the \"look up\" was about 60,000 faster when it was of an integer type rather than a float. However, since Python 2.x's `range` or `xrange` doesn't have a `__contains__` method, the \"look-up speed\" wouldn't be that much different for integers or floats:"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<br>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print 'Python', python_version()\n",
      "assert(val_in_xrange(x, x/2.0) == True)\n",
      "assert(val_in_xrange(x, x/2) == True)\n",
      "assert(val_in_range(x, x/2) == True)\n",
      "assert(val_in_range(x, x//2) == True)\n",
      "%timeit val_in_xrange(x, x/2.0)\n",
      "%timeit val_in_xrange(x, x/2)\n",
      "%timeit val_in_range(x, x/2.0)\n",
      "%timeit val_in_range(x, x/2)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Python 2.7.7\n",
        "1 loops, best of 3: 285 ms per loop"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "1 loops, best of 3: 179 ms per loop"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "1 loops, best of 3: 658 ms per loop"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n",
        "1 loops, best of 3: 556 ms per loop"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<br>"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Below the \"proofs\" that the `__contain__` method wasn't added to Python 2.x yet:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print('Python', python_version())\n",
      "range.__contains__"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Python 3.4.1\n"
       ]
      },
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 8,
       "text": [
        "<slot wrapper '__contains__' of 'range' objects>"
       ]
      }
     ],
     "prompt_number": 8
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print 'Python', python_version()\n",
      "range.__contains__"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Python 2.7.7\n"
       ]
      },
      {
       "ename": "AttributeError",
       "evalue": "'builtin_function_or_method' object has no attribute '__contains__'",
       "output_type": "pyerr",
       "traceback": [
        "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
        "\u001b[0;32m<ipython-input-7-05327350dafb>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;32mprint\u001b[0m \u001b[0;34m'Python'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mpython_version\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mrange\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__contains__\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
        "\u001b[0;31mAttributeError\u001b[0m: 'builtin_function_or_method' object has no attribute '__contains__'"
       ]
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print 'Python', python_version()\n",
      "xrange.__contains__"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Python 2.7.7\n"
       ]
      },
      {
       "ename": "AttributeError",
       "evalue": "type object 'xrange' has no attribute '__contains__'",
       "output_type": "pyerr",
       "traceback": [
        "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
        "\u001b[0;32m<ipython-input-8-7d1a71bfee8e>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;32mprint\u001b[0m \u001b[0;34m'Python'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mpython_version\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mxrange\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__contains__\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
        "\u001b[0;31mAttributeError\u001b[0m: type object 'xrange' has no attribute '__contains__'"
       ]
      }
     ],
     "prompt_number": 8
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<br>\n",
      "<br>"
     ]
    },
    {
     "cell_type": "heading",
     "level": 4,
     "metadata": {},
     "source": [
      "Note about the speed differences in Python 2 and 3"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Some people pointed out the speed difference between Python 3's `range()` and Python2's `xrange()`. Since they are implemented the same way one would expect the same speed. However the difference here just comes from the fact that Python 3 generally tends to run slower than Python 2. "
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def test_while():\n",
      "    i = 0\n",
      "    while i < 20000:\n",
      "        i += 1\n",
      "    return"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 3
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print('Python', python_version())\n",
      "%timeit test_while()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Python 3.4.1\n",
        "100 loops, best of 3: 2.68 ms per loop"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n"
       ]
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print 'Python', python_version()\n",
      "%timeit test_while()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Python 2.7.6\n",
        "1000 loops, best of 3: 1.72 ms per loop"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<br>\n",
      "<br>"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Raising exceptions"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "[[back to the section-overview](#Sections)]"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "\n",
      "\n",
      "Where Python 2 accepts both notations, the 'old' and the 'new' syntax, Python 3 chokes (and raises a `SyntaxError` in turn) if we don't enclose the exception argument in parentheses:"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "#### Python 2"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print 'Python', python_version()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Python 2.7.6\n"
       ]
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "raise IOError, \"file error\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "ename": "IOError",
       "evalue": "file error",
       "output_type": "pyerr",
       "traceback": [
        "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mIOError\u001b[0m                                   Traceback (most recent call last)",
        "\u001b[0;32m<ipython-input-8-25f049caebb0>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mIOError\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"file error\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
        "\u001b[0;31mIOError\u001b[0m: file error"
       ]
      }
     ],
     "prompt_number": 8
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "raise IOError(\"file error\")"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "ename": "IOError",
       "evalue": "file error",
       "output_type": "pyerr",
       "traceback": [
        "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mIOError\u001b[0m                                   Traceback (most recent call last)",
        "\u001b[0;32m<ipython-input-9-6f1c43f525b2>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mIOError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"file error\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
        "\u001b[0;31mIOError\u001b[0m: file error"
       ]
      }
     ],
     "prompt_number": 9
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<br>"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "#### Python 3"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print('Python', python_version())"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Python 3.4.1\n"
       ]
      }
     ],
     "prompt_number": 9
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "raise IOError, \"file error\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "ename": "SyntaxError",
       "evalue": "invalid syntax (<ipython-input-10-25f049caebb0>, line 1)",
       "output_type": "pyerr",
       "traceback": [
        "\u001b[0;36m  File \u001b[0;32m\"<ipython-input-10-25f049caebb0>\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m    raise IOError, \"file error\"\u001b[0m\n\u001b[0m                 ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
       ]
      }
     ],
     "prompt_number": 10
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "The proper way to raise an exception in Python 3:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print('Python', python_version())\n",
      "raise IOError(\"file error\")"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Python 3.4.1\n"
       ]
      },
      {
       "ename": "OSError",
       "evalue": "file error",
       "output_type": "pyerr",
       "traceback": [
        "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mOSError\u001b[0m                                   Traceback (most recent call last)",
        "\u001b[0;32m<ipython-input-11-c350544d15da>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Python'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mpython_version\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mIOError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"file error\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
        "\u001b[0;31mOSError\u001b[0m: file error"
       ]
      }
     ],
     "prompt_number": 11
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<br>\n",
      "<br>"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Handling exceptions"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "[[back to the section-overview](#Sections)]"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Also the handling of exceptions has slightly changed in Python 3. In Python 3 we have to use the \"`as`\" keyword now"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "#### Python 2"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print 'Python', python_version()\n",
      "try:\n",
      "    let_us_cause_a_NameError\n",
      "except NameError, err:\n",
      "    print err, '--> our error message'"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Python 2.7.6\n",
        "name 'let_us_cause_a_NameError' is not defined --> our error message\n"
       ]
      }
     ],
     "prompt_number": 10
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<br>"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "#### Python 3"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print('Python', python_version())\n",
      "try:\n",
      "    let_us_cause_a_NameError\n",
      "except NameError as err:\n",
      "    print(err, '--> our error message')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Python 3.4.1\n",
        "name 'let_us_cause_a_NameError' is not defined --> our error message\n"
       ]
      }
     ],
     "prompt_number": 12
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<br>\n",
      "<br>"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<a id='The-next-function-and-next-method'></a>"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "The next() function and .next() method"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "[[back to the section-overview](#Sections)]"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Since `next()` (`.next()`) is such a commonly used function (method), this is another syntax change (or rather change in implementation) that is worth mentioning: where you can use both the function and method syntax in Python 2.7.5, the `next()` function is all that remains in Python 3 (calling the `.next()` method raises an `AttributeError`)."
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "#### Python 2"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print 'Python', python_version()\n",
      "\n",
      "my_generator = (letter for letter in 'abcdefg')\n",
      "\n",
      "next(my_generator)\n",
      "my_generator.next()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Python 2.7.6\n"
       ]
      },
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 11,
       "text": [
        "'b'"
       ]
      }
     ],
     "prompt_number": 11
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<br>"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "#### Python 3"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print('Python', python_version())\n",
      "\n",
      "my_generator = (letter for letter in 'abcdefg')\n",
      "\n",
      "next(my_generator)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Python 3.4.1\n"
       ]
      },
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 13,
       "text": [
        "'a'"
       ]
      }
     ],
     "prompt_number": 13
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "my_generator.next()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "ename": "AttributeError",
       "evalue": "'generator' object has no attribute 'next'",
       "output_type": "pyerr",
       "traceback": [
        "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
        "\u001b[0;32m<ipython-input-14-125f388bb61b>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mmy_generator\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnext\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
        "\u001b[0;31mAttributeError\u001b[0m: 'generator' object has no attribute 'next'"
       ]
      }
     ],
     "prompt_number": 14
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<br>\n",
      "<br>"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "For-loop variables and the global namespace leak"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "[[back to the section-overview](#Sections)]"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Good news is: In Python 3.x for-loop variables don't leak into the global namespace anymore!\n",
      "\n",
      "This goes back to a change that was made in Python 3.x and is described in [What\u2019s New In Python 3.0](https://docs.python.org/3/whatsnew/3.0.html) as follows:\n",
      "\n",
      "\"List comprehensions no longer support the syntactic form `[... for var in item1, item2, ...]`. Use `[... for var in (item1, item2, ...)]` instead. Also note that list comprehensions have different semantics: they are closer to syntactic sugar for a generator expression inside a `list()` constructor, and in particular the loop control variables are no longer leaked into the surrounding scope.\""
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "#### Python 2"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print 'Python', python_version()\n",
      "\n",
      "i = 1\n",
      "print 'before: i =', i\n",
      "\n",
      "print 'comprehension: ', [i for i in range(5)]\n",
      "\n",
      "print 'after: i =', i"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Python 2.7.6\n",
        "before: i = 1\n",
        "comprehension:  [0, 1, 2, 3, 4]\n",
        "after: i = 4\n"
       ]
      }
     ],
     "prompt_number": 12
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<br>"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "#### Python 3"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print('Python', python_version())\n",
      "\n",
      "i = 1\n",
      "print('before: i =', i)\n",
      "\n",
      "print('comprehension:', [i for i in range(5)])\n",
      "\n",
      "print('after: i =', i)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Python 3.4.1\n",
        "before: i = 1\n",
        "comprehension: [0, 1, 2, 3, 4]\n",
        "after: i = 1\n"
       ]
      }
     ],
     "prompt_number": 15
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<br>\n",
      "<br>"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Comparing unorderable types"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "[[back to the section-overview](#Sections)]"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Another nice change in Python 3 is that a `TypeError` is raised as warning if we try to compare unorderable types."
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "#### Python 2"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print 'Python', python_version()\n",
      "print \"[1, 2] > 'foo' = \", [1, 2] > 'foo'\n",
      "print \"(1, 2) > 'foo' = \", (1, 2) > 'foo'\n",
      "print \"[1, 2] > (1, 2) = \", [1, 2] > (1, 2)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Python 2.7.6\n",
        "[1, 2] > 'foo' =  False\n",
        "(1, 2) > 'foo' =  True\n",
        "[1, 2] > (1, 2) =  False\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<br>"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "#### Python 3"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print('Python', python_version())\n",
      "print(\"[1, 2] > 'foo' = \", [1, 2] > 'foo')\n",
      "print(\"(1, 2) > 'foo' = \", (1, 2) > 'foo')\n",
      "print(\"[1, 2] > (1, 2) = \", [1, 2] > (1, 2))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Python 3.4.1\n"
       ]
      },
      {
       "ename": "TypeError",
       "evalue": "unorderable types: list() > str()",
       "output_type": "pyerr",
       "traceback": [
        "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
        "\u001b[0;32m<ipython-input-16-a9031729f4a0>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Python'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mpython_version\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"[1, 2] > 'foo' = \"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0;34m'foo'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"(1, 2) > 'foo' = \"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0;34m'foo'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      4\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"[1, 2] > (1, 2) = \"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
        "\u001b[0;31mTypeError\u001b[0m: unorderable types: list() > str()"
       ]
      }
     ],
     "prompt_number": 16
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<br>\n",
      "<br>"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<a id='Parsing-user-inputs-via-input'></a>"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Parsing user inputs via input()"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "[[back to the section-overview](#Sections)]"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Fortunately, the `input()` function was fixed in Python 3 so that it always stores the user inputs as `str` objects. In order to avoid the dangerous behavior in Python 2 to read in other types than `strings`, we have to use `raw_input()` instead."
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "#### Python 2"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\"><pre style=\"margin: 0; line-height: 125%\">Python <span style=\"color: #6600EE; font-weight: bold\">2.7</span><span style=\"color: #333333\">.</span><span style=\"color: #0000DD; font-weight: bold\">6</span> \n",
      "[GCC <span style=\"color: #6600EE; font-weight: bold\">4.0</span><span style=\"color: #333333\">.</span><span style=\"color: #0000DD; font-weight: bold\">1</span> (Apple Inc<span style=\"color: #333333\">.</span> build <span style=\"color: #0000DD; font-weight: bold\">5493</span>)] on darwin\n",
      "Type <span style=\"background-color: #fff0f0\">&quot;help&quot;</span>, <span style=\"background-color: #fff0f0\">&quot;copyright&quot;</span>, <span style=\"background-color: #fff0f0\">&quot;credits&quot;</span> <span style=\"color: #000000; font-weight: bold\">or</span> <span style=\"background-color: #fff0f0\">&quot;license&quot;</span> <span style=\"color: #008800; font-weight: bold\">for</span> more information<span style=\"color: #333333\">.</span>\n",
      "\n",
      "<span style=\"color: #333333\">&gt;&gt;&gt;</span> my_input <span style=\"color: #333333\">=</span> <span style=\"color: #007020\">input</span>(<span style=\"background-color: #fff0f0\">&#39;enter a number: &#39;</span>)\n",
      "\n",
      "enter a number: <span style=\"color: #0000DD; font-weight: bold\">123</span>\n",
      "\n",
      "<span style=\"color: #333333\">&gt;&gt;&gt;</span> <span style=\"color: #007020\">type</span>(my_input)\n",
      "<span style=\"color: #333333\">&lt;</span><span style=\"color: #007020\">type</span> <span style=\"background-color: #fff0f0\">&#39;int&#39;</span><span style=\"color: #333333\">&gt;</span>\n",
      "\n",
      "<span style=\"color: #333333\">&gt;&gt;&gt;</span> my_input <span style=\"color: #333333\">=</span> raw_input(<span style=\"background-color: #fff0f0\">&#39;enter a number: &#39;</span>)\n",
      "\n",
      "enter a number: <span style=\"color: #0000DD; font-weight: bold\">123</span>\n",
      "\n",
      "<span style=\"color: #333333\">&gt;&gt;&gt;</span> <span style=\"color: #007020\">type</span>(my_input)\n",
      "<span style=\"color: #333333\">&lt;</span><span style=\"color: #007020\">type</span> <span style=\"background-color: #fff0f0\">&#39;str&#39;</span><span style=\"color: #333333\">&gt;</span>\n",
      "</pre></div>\n"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<br>"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "#### Python 3"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<!-- HTML generated using hilite.me --><div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\"><pre style=\"margin: 0; line-height: 125%\">Python <span style=\"color: #6600EE; font-weight: bold\">3.4</span><span style=\"color: #333333\">.</span><span style=\"color: #0000DD; font-weight: bold\">1</span> \n",
      "[GCC <span style=\"color: #6600EE; font-weight: bold\">4.2</span><span style=\"color: #333333\">.</span><span style=\"color: #0000DD; font-weight: bold\">1</span> (Apple Inc<span style=\"color: #333333\">.</span> build <span style=\"color: #0000DD; font-weight: bold\">5577</span>)] on darwin\n",
      "Type <span style=\"background-color: #fff0f0\">&quot;help&quot;</span>, <span style=\"background-color: #fff0f0\">&quot;copyright&quot;</span>, <span style=\"background-color: #fff0f0\">&quot;credits&quot;</span> <span style=\"color: #000000; font-weight: bold\">or</span> <span style=\"background-color: #fff0f0\">&quot;license&quot;</span> <span style=\"color: #008800; font-weight: bold\">for</span> more information<span style=\"color: #333333\">.</span>\n",
      "\n",
      "<span style=\"color: #333333\">&gt;&gt;&gt;</span> my_input <span style=\"color: #333333\">=</span> <span style=\"color: #007020\">input</span>(<span style=\"background-color: #fff0f0\">&#39;enter a number: &#39;</span>)\n",
      "\n",
      "enter a number: <span style=\"color: #0000DD; font-weight: bold\">123</span>\n",
      "\n",
      "<span style=\"color: #333333\">&gt;&gt;&gt;</span> <span style=\"color: #007020\">type</span>(my_input)\n",
      "<span style=\"color: #333333\">&lt;</span><span style=\"color: #008800; font-weight: bold\">class</span> <span style=\"color: #FF0000; background-color: #FFAAAA\">&#39;</span><span style=\"color: #BB0066; font-weight: bold\">str</span><span style=\"background-color: #fff0f0\">&#39;&gt;</span>\n",
      "</pre></div>\n"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<br>\n",
      "<br>"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Returning iterable objects instead of lists"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "[[back to the section-overview](#Sections)]"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "As we have already seen in the [`xrange`](#xrange) section, some functions and methods return iterable objects in Python 3 now  - instead of lists in Python 2.  \n",
      "\n",
      "Since we usually iterate over those only once anyway, I think this change makes a lot of sense to save memory. However, it is also possible - in contrast to generators - to iterate over those multiple times if needed, it is aonly not so efficient.\n",
      "\n",
      "And for those cases where we really need the `list`-objects, we can simply convert the iterable object into a `list` via the `list()` function."
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "#### Python 2"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print 'Python', python_version() \n",
      "\n",
      "print range(3) \n",
      "print type(range(3))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Python 2.7.6\n",
        "[0, 1, 2]\n",
        "<type 'list'>\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "#### Python 3"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print('Python', python_version())\n",
      "\n",
      "print(range(3))\n",
      "print(type(range(3)))\n",
      "print(list(range(3)))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Python 3.4.1\n",
        "range(0, 3)\n",
        "<class 'range'>\n",
        "[0, 1, 2]\n"
       ]
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<br>"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "**Some more commonly used functions and methods that don't return lists anymore in Python 3:**\n",
      "\n",
      "- `zip()`\n",
      "\n",
      "- `map()`\n",
      "\n",
      "- `filter()`\n",
      "\n",
      "- dictionary's `.keys()` method\n",
      "\n",
      "- dictionary's `.values()` method\n",
      "\n",
      "- dictionary's `.items()` method\n"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<br>\n",
      "<br>"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "More articles about Python 2 and Python 3"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "[[back to the section-overview](#Sections)]"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Here is a list of some good articles concerning Python 2 and 3 that I would recommend as a follow-up.\n",
      "\n",
      "\n",
      "**// Porting to Python 3** \n",
      "\n",
      "- [Should I use Python 2 or Python 3 for my development activity?](https://wiki.python.org/moin/Python2orPython3)\n",
      "\n",
      "- [What\u2019s New In Python 3.0](https://docs.python.org/3.0/whatsnew/3.0.html)\n",
      "\n",
      "- [Porting to Python 3](http://python3porting.com/differences.html)\n",
      "\n",
      "- [Porting Python 2 Code to Python 3](https://docs.python.org/3/howto/pyporting.html)  \n",
      "\n",
      "- [How keep Python 3 moving forward](http://nothingbutsnark.svbtle.com/my-view-on-the-current-state-of-python-3)\n",
      "\n",
      "**// Pro and anti Python 3**\n",
      "\n",
      "- [10 awesome features of Python that you can't use because you refuse to upgrade to Python 3](http://asmeurer.github.io/python3-presentation/slides.html#1)\n",
      "\n",
      "- [Everything you did not want to know about Unicode in Python 3](http://lucumr.pocoo.org/2014/5/12/everything-about-unicode/)\n",
      "\n",
      "- [Python 3 is killing Python](https://medium.com/@deliciousrobots/5d2ad703365d/)\n",
      "\n",
      "- [Python 3 can revive Python](https://medium.com/p/2a7af4788b10)\n",
      "\n",
      "- [Python 3 is fine](http://sealedabstract.com/rants/python-3-is-fine/)\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}