pleabargain

Learn Python3 The Hard Way with ipython exe32

Jun 2nd, 2013
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.66 KB | None | 0 0
  1. {
  2.  "metadata": {
  3.   "name": "Learn Python The Hard Way exe 32"
  4.  },
  5.  "nbformat": 3,
  6.  "nbformat_minor": 0,
  7.  "worksheets": [
  8.   {
  9.    "cells": [
  10.     {
  11.      "cell_type": "raw",
  12.      "metadata": {},
  13.      "source": "from http://learnpythonthehardway.org/book/ex32.html\nremember he wrote it for python 2.x . Mine are for python3.x"
  14.     },
  15.     {
  16.      "cell_type": "code",
  17.      "collapsed": false,
  18.      "input": "the_count = [1,2,3,4,5]\nfruits = ['apples','oranges','pears','apricots']\nchange = [1, 'pennies', 2, 'dimes',3, 'quarters']\n\n#this first of for-loop goes through\n\nfor number in the_count:\n    print (\"This is count {}\".format(number))\n    \nfor fruit in fruits:\n    print (\"A fruit of type: {}\".format(fruit))\n\n#here is a mixed list\n#not sure what is going on with the %r and i\nfor i in change:\n    print (\"I got %r\" %i)\n    \n\n\n\n\n\n\n\n",
  19.      "language": "python",
  20.      "metadata": {},
  21.      "outputs": [
  22.       {
  23.        "output_type": "stream",
  24.        "stream": "stdout",
  25.        "text": "This is count 1\nThis is count 2\nThis is count 3\nThis is count 4\nThis is count 5\nA fruit of type: apples\nA fruit of type: oranges\nA fruit of type: pears\nA fruit of type: apricots\nI got 1\nI got 'pennies'\nI got 2\nI got 'dimes'\nI got 3\nI got 'quarters'\n"
  26.       }
  27.      ],
  28.      "prompt_number": 3
  29.     },
  30.     {
  31.      "cell_type": "raw",
  32.      "metadata": {},
  33.      "source": "Let's try that again... with .format"
  34.     },
  35.     {
  36.      "cell_type": "code",
  37.      "collapsed": false,
  38.      "input": "\nfor i in change:\n    print (\"I got {}\" %i.format(change))\n    \n",
  39.      "language": "python",
  40.      "metadata": {},
  41.      "outputs": [
  42.       {
  43.        "ename": "AttributeError",
  44.        "evalue": "'int' object has no attribute 'format'",
  45.        "output_type": "pyerr",
  46.        "traceback": [
  47.         "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
  48.         "\u001b[1;32m<ipython-input-4-ab5e2831c7b7>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mchange\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m     \u001b[0mprint\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;34m\"I got {}\"\u001b[0m \u001b[1;33m%\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mchange\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      4\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
  49.         "\u001b[1;31mAttributeError\u001b[0m: 'int' object has no attribute 'format'"
  50.        ]
  51.       }
  52.      ],
  53.      "prompt_number": 4
  54.     },
  55.     {
  56.      "cell_type": "raw",
  57.      "metadata": {},
  58.      "source": "That didn't work... so we'll try something different."
  59.     },
  60.     {
  61.      "cell_type": "raw",
  62.      "metadata": {},
  63.      "source": ""
  64.     },
  65.     {
  66.      "cell_type": "code",
  67.      "collapsed": false,
  68.      "input": "\nfor i in change:\n    print (\"I got {}\".format(change))\n    \n",
  69.      "language": "python",
  70.      "metadata": {},
  71.      "outputs": [
  72.       {
  73.        "output_type": "stream",
  74.        "stream": "stdout",
  75.        "text": "I got [1, 'pennies', 2, 'dimes', 3, 'quarters']\nI got [1, 'pennies', 2, 'dimes', 3, 'quarters']\nI got [1, 'pennies', 2, 'dimes', 3, 'quarters']\nI got [1, 'pennies', 2, 'dimes', 3, 'quarters']\nI got [1, 'pennies', 2, 'dimes', 3, 'quarters']\nI got [1, 'pennies', 2, 'dimes', 3, 'quarters']\n"
  76.       }
  77.      ],
  78.      "prompt_number": 6
  79.     },
  80.     {
  81.      "cell_type": "raw",
  82.      "metadata": {},
  83.      "source": "That didn't work either...moving on"
  84.     },
  85.     {
  86.      "cell_type": "code",
  87.      "collapsed": false,
  88.      "input": "#we'll build a list\nelements =[]\n\n#then a range function\nfor i in range (0,6):\n    print (\"Adding %d to the list.\" %i)\n    #append is a function that lists understand\n    elements.append(i)\n    \n#now print them\nfor i in elements:\n    print (\"element was: %d\" %i)\n\n\n\n",
  89.      "language": "python",
  90.      "metadata": {},
  91.      "outputs": [
  92.       {
  93.        "output_type": "stream",
  94.        "stream": "stdout",
  95.        "text": "Adding 0 to the list.\nAdding 1 to the list.\nAdding 2 to the list.\nAdding 3 to the list.\nAdding 4 to the list.\nAdding 5 to the list.\nelement was: 0\nelement was: 1\nelement was: 2\nelement was: 3\nelement was: 4\nelement was: 5\n"
  96.       }
  97.      ],
  98.      "prompt_number": 7
  99.     }
  100.    ],
  101.    "metadata": {}
  102.   }
  103.  ]
  104. }
Add Comment
Please, Sign In to add comment