From f9d17a4f94b9e101f3789160c875ae8ffdb4e017 Mon Sep 17 00:00:00 2001 From: rasbt Date: Sat, 24 May 2014 20:40:01 -0400 Subject: [PATCH] added input() section --- ...ey_differences_between_python_2_and_3.html | 121 +++++++++++++++++- ...y_differences_between_python_2_and_3.ipynb | 105 ++++++++++++++- 2 files changed, 223 insertions(+), 3 deletions(-) diff --git a/tutorials/key_differences_between_python_2_and_3.html b/tutorials/key_differences_between_python_2_and_3.html index 815982a..d4311cb 100644 --- a/tutorials/key_differences_between_python_2_and_3.html +++ b/tutorials/key_differences_between_python_2_and_3.html @@ -1761,6 +1761,7 @@ document.write(''+'email'+'<\/'+'a'+'>');
  • The next() function and .next() method

  • For-loop variables and the global namespace leak

  • Comparing unorderable types

  • +
  • Parsing user inputs via input()

  • More articles about Python 2 and Python 3

  • @@ -1926,7 +1927,7 @@ unicode_literals
    -In [1]: +In [3]:
    @@ -3714,6 +3715,124 @@ Python 3.4.1
    +
    +
    +
    +
    +
    +
    +



    +
    +
    +
    +
    +
    +
    +
    +
    +

    +
    +
    +
    +
    +
    +
    +
    +
    +

    Parsing user inputs via input()

    +
    +
    +
    + + +
    +
    +
    +
    +
    +

    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.

    +
    +
    +
    +
    +
    +
    +
    +
    +

    Python 2

    +
    +
    +
    +
    +
    +
    +
    +
    +
    Python 2.7.6 
    +[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
    +Type "help", "copyright", "credits" or "license" for more information.
    +
    +>>> my_input = input('enter a number: ')
    +
    +enter a number: 123
    +
    +>>> type(my_input)
    +<type 'int'>
    +
    +>>> my_input = raw_input('enter a number: ')
    +
    +enter a number: 123
    +
    +>>> type(my_input)
    +<type 'str'>
    +
    +
    +
    +
    +
    +
    +
    +
    +
    +


    +
    +
    +
    +
    +
    +
    +
    +
    +

    Python 3

    +
    +
    +
    +
    +
    +
    +
    +
    +
    Python 3.4.1 
    +[GCC 4.2.1 (Apple Inc. build 5577)] on darwin
    +Type "help", "copyright", "credits" or "license" for more information.
    +
    +>>> my_input = input('enter a number: ')
    +
    +enter a number: 123
    +
    +>>> type(my_input)
    +<class 'str'>
    +
    +
    +
    diff --git a/tutorials/key_differences_between_python_2_and_3.ipynb b/tutorials/key_differences_between_python_2_and_3.ipynb index 92ee2ab..22a8f86 100644 --- a/tutorials/key_differences_between_python_2_and_3.ipynb +++ b/tutorials/key_differences_between_python_2_and_3.ipynb @@ -1,7 +1,7 @@ { "metadata": { "name": "", - "signature": "sha256:bce64714d9af46abdf20f98e6b5b0b51cd1240612c1dbd99a40d812aea22dcdf" + "signature": "sha256:2d8d614150aba437b28f7fe22d2e6a05b76ddb1a447d645c7d5085ab0c286c44" }, "nbformat": 3, "nbformat_minor": 0, @@ -93,6 +93,8 @@ "\n", "- [Comparing unorderable types](#Comparing-unorderable-types)\n", "\n", + "- [Parsing user inputs via input()](#Parsing-user-inputs-via-input)\n", + "\n", "- [More articles about Python 2 and Python 3](#More-articles-about-Python-2-and-Python-3)" ] }, @@ -196,7 +198,7 @@ "language": "python", "metadata": {}, "outputs": [], - "prompt_number": 1 + "prompt_number": 3 }, { "cell_type": "markdown", @@ -1495,6 +1497,105 @@ "
    " ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "" + ] + }, + { + "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": [ + "
    Python 2.7.6 \n",
    +      "[GCC 4.0.1 (Apple Inc. build 5493)] on darwin\n",
    +      "Type "help", "copyright", "credits" or "license" for more information.\n",
    +      "\n",
    +      ">>> my_input = input('enter a number: ')\n",
    +      "\n",
    +      "enter a number: 123\n",
    +      "\n",
    +      ">>> type(my_input)\n",
    +      "<type 'int'>\n",
    +      "\n",
    +      ">>> my_input = raw_input('enter a number: ')\n",
    +      "\n",
    +      "enter a number: 123\n",
    +      "\n",
    +      ">>> type(my_input)\n",
    +      "<type 'str'>\n",
    +      "
    \n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "
    " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Python 3" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "
    Python 3.4.1 \n",
    +      "[GCC 4.2.1 (Apple Inc. build 5577)] on darwin\n",
    +      "Type "help", "copyright", "credits" or "license" for more information.\n",
    +      "\n",
    +      ">>> my_input = input('enter a number: ')\n",
    +      "\n",
    +      "enter a number: 123\n",
    +      "\n",
    +      ">>> type(my_input)\n",
    +      "<class 'str'>\n",
    +      "
    \n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "
    \n", + "
    " + ] + }, { "cell_type": "heading", "level": 2,