{ "metadata": { "name": "", "signature": "sha256:cacc571d4837d5307aef4322356742c15639b9ac8aaaa6bc1bc1580c03c1a8fb" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Hour 10: Controlling Programming flow" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Exmaple 10.1, Page No.157" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"Integers that can be divided by both 2 and 3\"\n", "print \"(within the range of 0 to 100):\"\n", "for i in range(0,100):\n", " if i%2==0 and i%3==0:\n", " print \" \",i\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Integers that can be divided by both 2 and 3\n", "(within the range of 0 to 100):\n", " 0\n", " 6\n", " 12\n", " 18\n", " 24\n", " 30\n", " 36\n", " 42\n", " 48\n", " 54\n", " 60\n", " 66\n", " 72\n", " 78\n", " 84\n", " 90\n", " 96\n" ] } ], "prompt_number": 7 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Exmaple 10.2, Page No.159" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"Even Number Odd Number\"\n", "for i in range(0,10):\n", " if(i%2==0):\n", " print i,\n", " else:\n", " print \"{:14d}\".format(i)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Even Number Odd Number\n", "0 1\n", "2 3\n", "4 5\n", "6 7\n", "8 9\n" ] } ], "prompt_number": 10 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Exmaple 10.3, Page No.160" ] }, { "cell_type": "code", "collapsed": false, "input": [ "for i in range(-5,6):\n", " if i>0:\n", " if i%2==0:\n", " print \"{:d} is an even number.\".format(i)\n", " else:\n", " print \"{:d} is an odd number.\".format(i)\n", " elif i==0:\n", " print \"The number is zero.\"\n", " else:\n", " print \"Negative number:\",i" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Negative number: -5\n", "Negative number: -4\n", "Negative number: -3\n", "Negative number: -2\n", "Negative number: -1\n", "The number is zero.\n", "1 is an odd number.\n", "2 is an even number.\n", "3 is an odd number.\n", "4 is an even number.\n", "5 is an odd number.\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 10.4, Page No.162" ] }, { "cell_type": "code", "collapsed": false, "input": [ "day=raw_input(\"Please enter a sigle digit for a day\\n(within the range of 1 to 3:)\")\n", "if day=='1':\n", " print \"Day 1\"\n", "elif day=='2':\n", " print \"Day 2\"\n", "elif day=='3':\n", " print \"Day 3\"" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Please enter a sigle digit for a day\n", "(within the range of 1 to 3:)3\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Day 3\n" ] } ], "prompt_number": 17 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 10.5, Page No.164" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#This program is specially used to understand the \"break\" statement in switch..case. \n", "#But in python we can't use break statement outside of the loop so break is not written with each if block.\n", "day=raw_input(\"Please enter a sigle digit for a day\\n(within the range of 1 to 7:)\")\n", "if day=='1':\n", " print \"Day 1 is Sunday\"\n", " \n", "elif day=='2':\n", " print \"Day 2 is Monday\"\n", " \n", "elif day=='3':\n", " print \"Day 3 is Tuesday\"\n", " \n", "elif day=='4':\n", " print \"Day 4 is Wednesday\"\n", " \n", "elif day=='5':\n", " print \"Day 5 is Thursday\"\n", " \n", "elif day=='6':\n", " print \"Day 6 is Friday\"\n", " \n", "elif day=='7':\n", " print \"Day 7 is Saturday\"\n", " \n", "else:\n", " print \"The digit is not within the range of 1 to 7\"\n", " " ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Please enter a sigle digit for a day\n", "(within the range of 1 to 7:)1\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Day 1 is Sunday\n" ] } ], "prompt_number": 22 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 10.6, Page No.166" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"Enter a character:\\n (enter X to exit)\\n\"\n", "while 1:\n", " c=raw_input()\n", " if c=='x':\n", " break\n", "print \"Break the infinite loop. Bye!\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter a character:\n", " (enter X to exit)\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "H\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "I\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "x\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Break the infinite loop. Bye!\n" ] } ], "prompt_number": 24 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 10.7, Page No.167" ] }, { "cell_type": "code", "collapsed": false, "input": [ "sum=0\n", "for i in range(1,8):\n", " if i==3 or i==5:\n", " continue\n", " sum=sum+i\n", "print \"The sum of 1,2,4,6, and 7 is:\",sum" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The sum of 1,2,4,6, and 7 is: 20\n" ] } ], "prompt_number": 26 } ], "metadata": {} } ] }