{ "metadata": { "name": "", "signature": "sha256:72c49f1aed7f4d43ac1084e67245cfba7b011cb43f84b1ab3ef13b081aec557f" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 6: Decision and control statements" ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Example 6.1, Page number: 114" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "# Variable declaration\n", "old_number = 1\n", "current_number = 1\n", "\n", "print ('1')\n", "\n", "# Calculation and result\n", "while (current_number < 100) :\n", " print (current_number)\n", " next_number = current_number + old_number\n", "\n", " old_number = current_number\n", " current_number = next_number" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1\n", "1\n", "2\n", "3\n", "5\n", "8\n", "13\n", "21\n", "34\n", "55\n", "89\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Example 6.2, Page number: 115" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "# Variable declaration\n", "total = 0\n", "i = 0\n", "\n", "# Calculation\n", "while (i < 10) :\n", " item = 1\n", "\n", " if item == 0 :\n", " break\n", "\n", " total += item\n", " print ('Total: %d' % total)\n", " i = i + 1\n", " \n", "# Result\n", "print ('Final total: %d' % total)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Total: 1\n", "Total: 2\n", "Total: 3\n", "Total: 4\n", "Total: 5\n", "Total: 6\n", "Total: 7\n", "Total: 8\n", "Total: 9\n", "Total: 10\n", "Final total: 10\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Example 6.3, Page number: 116" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "# Variable declaration\n", "total = 0\n", "minus_items = 0\n", "i = 0\n", "\n", "# Calculation\n", "while (i < 10) :\n", " item = 1\n", " \n", " if item == 0 :\n", " break\n", "\n", " if item < 0 :\n", " minus_items += 1\n", " continue\n", "\n", " total += item\n", " print ('Total: %d' % total)\n", " i = i + 1\n", " \n", "# Result\n", "print ('Final total: %d' % total)\n", "print ('with %d negative items omitted' % minus_items)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Total: 1\n", "Total: 2\n", "Total: 3\n", "Total: 4\n", "Total: 5\n", "Total: 6\n", "Total: 7\n", "Total: 8\n", "Total: 9\n", "Total: 10\n", "Final total: 10\n", "with 0 negative items omitted\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Example 6.4, Page number: 118" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "# Variable declaration\n", "balance_owed = 100\n", "\n", "# Calculation and result\n", "if balance_owed == 0 :\n", " print ('You owe nothing.')\n", "else :\n", " print ('You owe %d dollars.' % balance_owed)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "You owe 100 dollars.\n" ] } ], "prompt_number": 3 } ], "metadata": {} } ] }