{ "metadata": { "name": "", "signature": "sha256:a924e86900c65c3fd8c20ee5aee9bc052eac287d056598356137191816cafe4b" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 7: Decision Making & Looping" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 7.1, page no. 108" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\"\"\"\n", "the method shown in textbook is not possible in python.\n", "We will implement in a different way\n", "\"\"\"\n", "\n", "print \"Enter a string. Enter quit to stop\"\n", "\n", "my_str = \"\"\n", "while True:\n", " c = raw_input()\n", " if c == \"quit\":\n", " break\n", " my_str = my_str + c + \" \"\n", "print \"String you entered is: \", my_str" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter a string. Enter quit to stop\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Java\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "is\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Good\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "quit\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "String you entered is: Java is Good \n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 7.2, page no. 109" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Do.. While loop\n", "\n", "\n", "row = 1\n", "column = 1\n", "\n", "while row <= 3:\n", " column = 1\n", " while column <= 3:\n", " y = row * column\n", " print \" \", y,\n", " column += 1\n", " print \"\"\n", " row += 1" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " 1 2 3 \n", " 2 4 6 \n", " 3 6 9 \n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 7.3, page no. 112" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "print \"2 to power-n n 2 to power n\"\n", "p = 1\n", "for n in range(0,10):\n", " if n == 0:\n", " p = 1\n", " else:\n", " p = p * 2\n", " q = float(1.0/p)\n", " print q,\" \",n,\" \",p" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "2 to power-n n 2 to power n\n", "1.0 0 1\n", "0.5 1 2\n", "0.25 2 4\n", "0.125 3 8\n", "0.0625 4 16\n", "0.03125 5 32\n", "0.015625 6 64\n", "0.0078125 7 128\n", "0.00390625 8 256\n", "0.001953125 9 512\n" ] } ], "prompt_number": 20 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 7.4, page no. 116" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "states = [\"TamilNadu\", \"AndhraPradesh\", \"UttarPradesh\", \"Rajasthan\"]\n", "for i in range(0, len(states)):\n", " print \"Standard for-loop: state name: \", states[i]\n", "print \"\"\n", "for state in states:\n", " print \"Enhanced for-loop: state name: \", state\n", "\n", "print \"\"\n", "cities = []\n", "cities.append(\"Delhi\")\n", "cities.append(\"Mumbai\")\n", "cities.append(\"Calcutta\")\n", "cities.append(\"Chennai\")\n", "\n", "for i in range(0, len(cities)):\n", " print \"Standard for-loop: state name: \", cities[i]\n", "print \"\"\n", "for city in cities:\n", " print \"Enhanced for-loop: enhanced name: \", city\n", "\n", "print \"\\nIn Collection\"\n", "for i in cities:\n", " print i\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Standard for-loop: state name: TamilNadu\n", "Standard for-loop: state name: AndhraPradesh\n", "Standard for-loop: state name: UttarPradesh\n", "Standard for-loop: state name: Rajasthan\n", "\n", "Enhanced for-loop: state name: TamilNadu\n", "Enhanced for-loop: state name: AndhraPradesh\n", "Enhanced for-loop: state name: UttarPradesh\n", "Enhanced for-loop: state name: Rajasthan\n", "\n", "Standard for-loop: state name: Delhi\n", "Standard for-loop: state name: Mumbai\n", "Standard for-loop: state name: Calcutta\n", "Standard for-loop: state name: Chennai\n", "\n", "Enhanced for-loop: enhanced name: Delhi\n", "Enhanced for-loop: enhanced name: Mumbai\n", "Enhanced for-loop: enhanced name: Calcutta\n", "Enhanced for-loop: enhanced name: Chennai\n", "\n", "In Collection\n", "Delhi\n", "Mumbai\n", "Calcutta\n", "Chennai\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 7.5, page no. 120" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "for i in range(1,100):\n", " print \" \"\n", " if i >= 10:\n", " break\n", " for j in range(1, 100):\n", " print \" * \",\n", " if j == i:\n", " break" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " \n", " * \n", " * * \n", " * * * \n", " * * * * \n", " * * * * * \n", " * * * * * * \n", " * * * * * * * \n", " * * * * * * * * \n", " * * * * * * * * * \n" ] } ], "prompt_number": 28 } ], "metadata": {} } ] }