{ "metadata": { "name": "", "signature": "sha256:cbd54959e18d38695240f376809cd86bcdd3f9c461e014b42a620c0846ec3cbf" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 04 : Branching and Looping" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 1: Page No.:4.88" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# addition of numbers using while loop.\n", "\n", "#Varibale Declaration\n", "\n", "i=1\n", "sum=0\n", "while i<=10: #Checking while loop\n", " sum+=i\n", " i+=1 #Increment of i\n", " \n", " #Result \n", "print \"The Sum of the numbers upto is \",sum" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The Sum of the numbers upto is 55\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 2: Page No.: 4.89" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import sys\n", "\n", "#Variable Declaration\n", "\n", "i=1\n", "j=0\n", "\n", " \n", "print \"The maximum value of integer is\", sys.maxsize\n", "print \"The Value of integer after overflow is\", sys.maxint-1" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " The maximum value of integer is 2147483647\n", "The Value of integer after overflow is 2147483646\n" ] } ], "prompt_number": 30 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 3: Page No.: 4.89" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Reverse the given number using while loop\n", "\n", "#Variable Declaration\n", "\n", "number=digit=rev=0\n", "\n", "number=input(\"Give the Value: \")\n", "\n", "while(number!=0):\n", " digit=number%10\n", " rev=rev*10+digit\n", " number= number/10\n", " \n", "print \"The reversible value of given number is\", rev\n" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Give the Value: 7896\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "The reversible value of given number is 6987\n" ] } ], "prompt_number": 33 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4: Page No.: 4.90" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Simple interest using while loop.\n", "\n", "#Variable Declaration\n", "\n", "p=1\n", "n=1\n", "count=1\n", "\n", "r=si=0.0\n", "\n", "while(count<=3):\n", " p=input(\"Give the value of P\" '\\t')\n", " n=input(\"Give the value of N\"'\\t')\n", " r=float(input(\"Give the value of R\"'\\t'))\n", " \n", " si=\"%.3f\"%float((p*n*r)/100)\n", " \n", " print \"Simple Interest of SI\", si\n", " count=count+1\n" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Give the value of P\t2000\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Give the value of N\t3\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Give the value of R\t5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Simple Interest of SI 300.000\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Give the value of P\t3500\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Give the value of N\t6\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Give the value of R\t1.5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Simple Interest of SI 315.000\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Give the value of P\t6000\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Give the value of N\t2\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Give the value of R\t3.5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Simple Interest of SI 420.000\n" ] } ], "prompt_number": 41 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 5: Page No.:4.92" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Numbers upto 10 by using the do..while loop\n", "\n", "#Variable Declaration\n", "\n", "i=1\n", "sum=0\n", "\n", "while True:\n", "\tsum+=i\n", "\ti+=1\n", "\tif(i>10):\n", "\t\tbreak\n", "print \"Sum of the numbers upto 10 is\", sum \n", " \n", " \n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Sum of the numbers upto 10 is 55\n" ] } ], "prompt_number": 42 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6: Page No.: 4.92" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#print n numbers using do...while loop\n", "\n", "i=0\n", "\n", "n=input(\"Enter the number: \")\n", "\n", "while True:\n", " print \"The numbers are: \",i\n", " i=i+1\n", " if(i>=n):\n", " break\n" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter the number: 6\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "The numbers are: 0\n", "The numbers are: 1\n", "The numbers are: 2\n", "The numbers are: 3\n", "The numbers are: 4\n", "The numbers are: 5\n" ] } ], "prompt_number": 73 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 7: page no.: 4.95" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Addition of number upto 10 using for loop\n", "\n", "i=sum=0\n", "\n", "for i in range (0,11):\n", "\tsum+=i\n", "\ti+=1\n", "\n", "print \"The addition of given numbers\",sum\n", " " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The addition of given numbers 55\n" ] } ], "prompt_number": 75 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 8: Page No.:4.96" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#print n numbers using for...loop structure\n", "\n", "n=int(raw_input(\"Give Values of n: \"))\n", "i=0\n", "for i in range(0,n):\n", " print \"The Given numbers are: \",i\n", "\n", "\t" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Give Values of n: 5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "The Given numbers are: 0\n", "The Given numbers are: 1\n", "The Given numbers are: 2\n", "The Given numbers are: 3\n", "The Given numbers are: 4\n" ] } ], "prompt_number": 82 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 9: Page No.:4.97" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#student marks and average using for loop..\n", "\n", "a=b=c=d=e=total=0\n", "\n", "avg=0.0\n", "\n", "m=input(\"Enter the number of Students:\")\n", "\n", "print \"Enter the marks of the five subjects: \"'\\n'\n", "\n", "for i in range (m): \n", " i+=1\n", " print \"\\nStudent:\",i,\"\\n\"\n", " \n", " a,b,c,d,e=input(\"Enter the Student mark1: \") \n", " \n", " if ((a,b,c,d,e)!=0) :\n", " total=a+b+c+d+e\n", " avg=\"%.3f\"%float(total/5)\n", " print \"Total marks of the student:\", total\n", " print \"Average marks of the students: \",avg\n", " \n", " else :\n", " print \"Entered data is invalid\"\n", " \n", " " ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter the number of Students:3\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter the marks of the five subjects: \n", "\n", "\n", "Student: 1 \n", "\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter the Student mark1: 78,56,90,59,87\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Total marks of the student: 370\n", "Average marks of the students: 74.000\n", "\n", "Student: 2 \n", "\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter the Student mark1: 49,87,60,57,93\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Total marks of the student: 346\n", "Average marks of the students: 69.000\n", "\n", "Student: 3 \n", "\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter the Student mark1: 89,100,80,97,65\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Total marks of the student: 431\n", "Average marks of the students: 86.000\n" ] } ], "prompt_number": 89 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 10: Page No.:4.98" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Nested for loop\n", "\n", "for i in range(1,4):\n", " for j in range(1,4):\n", " print \"%d\"%(j),\n", " print \"\\n\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1 2 3 \n", "\n", "1 2 3 \n", "\n", "1 2 3 \n", "\n" ] } ], "prompt_number": 112 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 11: Page No.:4.99" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#student details using nested for loop..\n", "\n", "FIRST=300\n", "SECOND=200\n", "rollno=marks=total=0\n", "\n", "n=input(\"Enter the number of Students: \")\n", "m=input(\"Enter the number of subjects: \")\n", "\n", "for i in range(n):\n", " i+=(+1)\n", " rollno=int(raw_input(\"Enter the student roll number: \"))\n", " total=0\n", " print \"Enter marks of\",m, \"subjects for Roll no: \",rollno\n", " for j in range(m):\n", " marks=int(raw_input(\"Give marks: \"))\n", " total= total+marks\n", " \n", " print \"total marks\", total\n", " if(total>=FIRST):\n", " print \"(First Class)\"'\\n'\n", " elif(total>=SECOND):\n", " print \"(Second Class)\"'\\n'\n", " else:\n", " print \"Fail\"" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter the number of Students: 2\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter the number of subjects: 5\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter the student roll number: 201\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter marks of 5 subjects for Roll no: 201\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Give marks: 90\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Give marks: 78\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Give marks: 90\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Give marks: 68\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Give marks: 89\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "total marks 415\n", "(First Class)\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter the student roll number: 202\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter marks of 5 subjects for Roll no: 202\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Give marks: 40\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Give marks: 50\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Give marks: 49\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Give marks: 39\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Give marks: 47\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "total marks 225\n", "(Second Class)\n", "\n" ] } ], "prompt_number": 85 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 12: Page No.: 4.103" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Demonstrate switch case\n", "\n", "a=1\n", "if a==1:\n", " print \"I am in case 1\"'\\n'\n", "elif a==2:\n", " print \"I am in case2\"'\\n'\n", "else:\n", " print \"I am in case default\"'\\n'" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "I am in case 1\n", "\n" ] } ], "prompt_number": 115 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 13: Page No.:4.104" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Program to use the computer as calculator\n", "\n", "a=b=c=0\n", "\n", "op=raw_input(\"Give the option for calculation like +(ADD), -(SUB), *(MUL), /(DIV): \")\n", "a=input(\"Enter the value of a \")\n", "b=input(\"Enter the value of b \")\n", "\n", "#Calculation\n", "if (op=='+'):\n", " c=a+b\n", " print \"Result of C\", c \n", "elif(op=='-'):\n", " c=a-b\n", " print \"Result of C\", c\n", "elif(op=='*'):\n", " c=a*b\n", " print \"Result of C\", c\n", "else:\n", " c=a/b\n", " print \"Result of C\", c\n" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Give the option for calculation like +(ADD), -(SUB), *(MUL), /(DIV): +\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter the value of a 20\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter the value of b 10\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Result of C 30\n" ] } ], "prompt_number": 118 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 14: Page No.:4.105" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Find even or odd numbers \n", "\n", "#USer Input\n", "a=input(\"Enter a number: \")\n", "\n", "if a%2==0 : \n", " print \" The number is even. \"\n", "else :\n", " print \"The number is odd.\" \n", " " ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter a number: 89\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "The number is odd.\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 15: Page No.:4.106" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Program to count number of 0s, 1s, blank spaces and other characters\n", "\n", "\n", "txt=[30]\n", "count =0 \n", "txt=raw_input(\"Enter Numbers\")\n", "\n", "print \"Total Spaces : %d\"%txt.count(' ')\n", "print \"Total of 1's : %d\"%txt.count('1')\n", "print \"Total of 0's : %d\"%txt.count('0')\n", "print \"String Length : %d\"%len(txt)\n", " \n" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Numbers120010 09100110\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Total Spaces : 2\n", "Total of 1's : 5\n", "Total of 0's : 7\n", "String Length : 16\n" ] } ], "prompt_number": 43 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 16: Page No.: 4.108" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Print the number upto 5 using break statement\n", "\n", "for i in range(1,10):\n", " if i==6:\n", " break\n", " print i," ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1 2 3 4 5\n" ] } ], "prompt_number": 120 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 17: Page No.: 4.109" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Program to calculate the sum of the given positive numbers\n", "\n", "i=n=sum=0\n", "\n", "for i in range(0,5):\n", " n=int(raw_input(\"Give any value of n \"))\n", " if n<0:\n", " continue\n", " else:\n", " sum+=n\n", " \n", "print \"\\nSum is\", sum" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Give any value of n 10\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Give any value of n 15\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Give any value of n 25\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Give any value of n -10\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Give any value of n 50\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Sum is 100\n" ] } ], "prompt_number": 124 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 18: Page No.:4.113" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Program using goto statement\n", "\n", "a=input(\"Give the Value of a \")\n", "b=input(\"Give the value of b \")\n", "\n", "if a==b:\n", " print \"A and B are equal\"\n", "else:\n", " print \"A and B are not equal\"\n", " exit(0)" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Give the Value of a 3\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Give the value of b 3\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "A and B are equal\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Case Study 1: Page No.: 4.112" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Prime or not\n", "\n", "n=input(\"Enter the Positive value of n \")\n", "i=2\n", "\n", "#Calculation\n", "while n>i:\n", " if n%i==0 & i!=n:\n", " #r=n%i\n", " #if r==0:\n", " print \"The Given value of \",n,\" is not a prime\"\n", " break\n", " i+=1\n", "else:\n", " print \"The Given Value\",n, \"is Prime\"\n", " \n" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter the Positive value of n 3\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "The Given Value 3 is Prime\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Case Study 2: Page No.: 4.113" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Find the Greatest Common Division\n", "\n", "i=int(input(\"Give the Value of i \"))\n", "j=int(input(\"Give the value of j \"))\n", " \n", "def gcd(i,j):\n", " if i > j:\n", " k = i%j\n", " if k == 0:\n", " return j\n", " else:\n", " return gcd(j, k)\n", " if i < j:\n", " i = j\n", " j = i\n", " return gcd(i, j)\n", "print \"The value of gcd\", gcd(i,j) " ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Give the Value of i 54\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Give the value of j 36\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "The value of gcd 18\n" ] } ], "prompt_number": 7 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Case Study 3: Page No.: 4.115" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Program to which reads in a Temperature and print the temperature in opposite scale\n", "\n", "\n", "print \"1.Fahrenheit to Celcius\\n2.Celcius to Fahrenheit\\n\"\n", "code=input(\"Choose Option to find Temperature: \")\n", "\n", "while code!=0:\n", " if code==1:\n", " f=float(input(\"Give the Value for Fahrenheit Temperature \"))\n", " c=(f-32.0)*5.0/9.0\n", " break\n", " elif code==2:\n", " c=float(input(\"Give the Value for Celcius Temperature \"))\n", " f=9.0*c/5.0+32.0\n", " break\n", " else:\n", " print \"Give proper Code Value for Temperature Calculation\"\n", " break\n", "print \"C=\", \"%.3f\"%float(c)\n", "print \"F=\",f " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1.Fahrenheit to Celcius\n", "2.Celcius to Fahrenheit\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Choose Option to find Temperature: 1\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Give the Value for Fahrenheit Temperature 100\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "C= 37.778\n", "F= 100.0\n" ] } ], "prompt_number": 17 } ], "metadata": {} } ] }