{ "metadata": { "name": "", "signature": "sha256:fc3a82351dcd292af7cdc66294a40a180758bb26d4545d98b8be5c4947393f3d" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 4: Expressions" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example upc.c, Page Number-57" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def main():\n", " #input from user\n", " d=int(raw_input(\"Enter the first (single) digit: \")) \n", " firstfive= raw_input(\"Enter first group of five digits\")\n", " list1=list(firstfive) \n", " i1=list1[0]\n", " i2=list1[1]\n", " i3=list1[2]\n", " i4=list1[3]\n", " i5=list1[4]\n", " secondfive= raw_input(\"Enter second group of five digits\")\n", " list2=list(firstfive) \n", " j1=list2[0]\n", " j2=list2[1]\n", " j3=list2[2]\n", " j4=list2[3]\n", " j5=list2[4]\n", " first_sum=d+int(i2)+int(i4)+int(j1)+int(j3)+int(j5)\n", " second_sum=int(i1)+int(i3)+int(i5)+int(j2)+int(j4)\n", " total=3*first_sum+second_sum\n", " s=9-((total - 1) % 10)\n", " print \"Check digit: %d\" % s #print result\n", " \n", "if __name__=='__main__':\n", " main()" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter the first (single) digit: 0\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter first group of five digits13800\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter second group of five digits15713\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Check digit: 2\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example on Page number-59" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "#variable declaration\n", "i = 1\n", "#k = l + (j=i)\n", "j=i\n", "k=1+j\n", "print \"%d %d %d\" % (i,j,k) #prints \"1 1 2\" \n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1 1 2\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example on Page number-61" ] }, { "cell_type": "code", "collapsed": false, "input": [ "i=1 #variable declaration\n", "i+=1\n", "print \"i is %d\" % i #++i\n", "print \"i is %d\" % i #i\n", "\n", "i = 1\n", "j=i+1\n", "print \"i is %d\" % i #i++\n", "print \"i is %d\" % j #i\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "i is 2\n", "i is 2\n", "i is 1\n", "i is 2\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example on Page number-62" ] }, { "cell_type": "code", "collapsed": false, "input": [ "i=1 #variable declaration\n", "i-=1\n", "print \"i is %d\" % i #--i\n", "print \"i is %d\" % i #i\n", "\n", "i = 1\n", "j=i-1\n", "print \"i is %d\" % i #i--\n", "print \"i is %d\" % j #i" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "i is 0\n", "i is 0\n", "i is 1\n", "i is 0\n" ] } ], "prompt_number": 6 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }