{ "metadata": { "name": "", "signature": "sha256:2165fd6887703267c26bff51b8c400d1a8e77008ec017dafbe2d89ad91d495e9" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 3: Control of Flow and Logical Expressions" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 3.1, page no. 82" ] }, { "cell_type": "code", "collapsed": false, "input": [ "i = -10;\n", "\n", "while(i <= 5):\n", " print \"value of i is %d \" %( i)\n", " print \"i == 0 = %d \" %( i==0 )\n", " print \"i > -5 = %d\\n \" %(i > -5)\n", " i+=1" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "value of i is -10 \n", "i == 0 = 0 \n", "i > -5 = 0\n", " \n", "value of i is -9 \n", "i == 0 = 0 \n", "i > -5 = 0\n", " \n", "value of i is -8 \n", "i == 0 = 0 \n", "i > -5 = 0\n", " \n", "value of i is -7 \n", "i == 0 = 0 \n", "i > -5 = 0\n", " \n", "value of i is -6 \n", "i == 0 = 0 \n", "i > -5 = 0\n", " \n", "value of i is -5 \n", "i == 0 = 0 \n", "i > -5 = 0\n", " \n", "value of i is -4 \n", "i == 0 = 0 \n", "i > -5 = 1\n", " \n", "value of i is -3 \n", "i == 0 = 0 \n", "i > -5 = 1\n", " \n", "value of i is -2 \n", "i == 0 = 0 \n", "i > -5 = 1\n", " \n", "value of i is -1 \n", "i == 0 = 0 \n", "i > -5 = 1\n", " \n", "value of i is 0 \n", "i == 0 = 1 \n", "i > -5 = 1\n", " \n", "value of i is 1 \n", "i == 0 = 0 \n", "i > -5 = 1\n", " \n", "value of i is 2 \n", "i == 0 = 0 \n", "i > -5 = 1\n", " \n", "value of i is 3 \n", "i == 0 = 0 \n", "i > -5 = 1\n", " \n", "value of i is 4 \n", "i == 0 = 0 \n", "i > -5 = 1\n", " \n", "value of i is 5 \n", "i == 0 = 0 \n", "i > -5 = 1\n", " \n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 3.2, page no. 86" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "s = raw_input(\"Enter String.. : \")\n", "print s" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter String.. : python\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "python\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 3.3, page no. 87" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "i = 0\n", "for i in range(11):\n", " print i" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n", "10\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 3.4, page no. 88" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "i=0\n", "while(i<=10):\n", " print \"%d \\n\" %i\n", " i += 1\n", "\n", "for i in range(11):\n", " print \"%d \\n\" %i" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0 \n", "\n", "1 \n", "\n", "2 \n", "\n", "3 \n", "\n", "4 \n", "\n", "5 \n", "\n", "6 \n", "\n", "7 \n", "\n", "8 \n", "\n", "9 \n", "\n", "10 \n", "\n", "0 \n", "\n", "1 \n", "\n", "2 \n", "\n", "3 \n", "\n", "4 \n", "\n", "5 \n", "\n", "6 \n", "\n", "7 \n", "\n", "8 \n", "\n", "9 \n", "\n", "10 \n", "\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 3.5 & 3.6, page no. 90-91" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "for i in range(11):\n", " if i==1 or i==2:\n", " print \"1 or 2\\n\"\n", " elif i==7:\n", " print \"7\\n\"\n", " else:\n", " print \"default\\n\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "default\n", "\n", "1 or 2\n", "\n", "1 or 2\n", "\n", "default\n", "\n", "default\n", "\n", "default\n", "\n", "default\n", "\n", "7\n", "\n", "default\n", "\n", "default\n", "\n", "default\n", "\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 3.7, page no. 92" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "for i in range(-10,11):\n", " if(i == 0):\n", " continue;\n", " print \"%f\\n\" % (15.0/i)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "-1.500000\n", "\n", "-1.666667\n", "\n", "-1.875000\n", "\n", "-2.142857\n", "\n", "-2.500000\n", "\n", "-3.000000\n", "\n", "-3.750000\n", "\n", "-5.000000\n", "\n", "-7.500000\n", "\n", "-15.000000\n", "\n", "15.000000\n", "\n", "7.500000\n", "\n", "5.000000\n", "\n", "3.750000\n", "\n", "3.000000\n", "\n", "2.500000\n", "\n", "2.142857\n", "\n", "1.875000\n", "\n", "1.666667\n", "\n", "1.500000\n", "\n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 3.9, page no. 98" ] }, { "cell_type": "code", "collapsed": false, "input": [ "'''\n", "This example demonstrates ?: operator in the textbook.\n", "There isin't a ?: operator in python. Instead we have [value1,value2][condition] \n", "operator.\n", "'''\n", "\n", "\n", "for i in range(11):\n", " print [\"odd\\n\",\"Even\\n\"][i&1]\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "odd\n", "\n", "Even\n", "\n", "odd\n", "\n", "Even\n", "\n", "odd\n", "\n", "Even\n", "\n", "odd\n", "\n", "Even\n", "\n", "odd\n", "\n", "Even\n", "\n", "odd\n", "\n" ] } ], "prompt_number": 7 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 3.10, page no. 99" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "for i in range(11):\n", " print \"i %d j %d\\n\" %( i,i*i)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "i 0 j 0\n", "\n", "i 1 j 1\n", "\n", "i 2 j 4\n", "\n", "i 3 j 9\n", "\n", "i 4 j 16\n", "\n", "i 5 j 25\n", "\n", "i 6 j 36\n", "\n", "i 7 j 49\n", "\n", "i 8 j 64\n", "\n", "i 9 j 81\n", "\n", "i 10 j 100\n", "\n" ] } ], "prompt_number": 8 } ], "metadata": {} } ] }