{ "metadata": { "name": "Chapter IV" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 4: Variables, data types, and arithmetic expressions" ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Program 4.1, Page number: 26" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "integerVar=100\n", "floatingVar=331.79\n", "doubleVar=8.44e+11\n", "charVar='w'\n", "boolVar=bool(0)\n", "\n", "print(\"integerVar={0}\".format(integerVar))\n", "print(\"floatingVar={0}\".format(floatingVar))\n", "print(\"doubleVar={0}\".format(doubleVar))\n", "print(\"charVar={0}\".format(charVar))\n", "print(\"boolVar={0}\".format(boolVar))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "integerVar=100\n", "floatingVar=331.79\n", "doubleVar=8.44e+11\n", "charVar=w\n", "boolVar=False\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Program 4.2, Page number: 30" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "a=100\n", "b=2\n", "c=25\n", "d=4\n", "\n", "result=a-b #subtraction\n", "print(\"a-b={0}\".format(result)) #result\n", "\n", "result=b*c #multiplication\n", "print(\"b*c={0}\".format(result)) #result\n", "\n", "result=a/c #division\n", "print(\"a/c={0}\".format(result)) #result\n", "\n", "result=a+b*c #precedence\n", "print(\"a+b*c={0}\".format(result)) #result \n", "\n", "print(\"a*b+c*d={0}\".format(a*b+c*d)) #direct calculation" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "a-b=98\n", "b*c=50\n", "a/c=4\n", "a+b*c=150\n", "a*b+c*d=300\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Program 4.3, Page number: 33" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "a=25\n", "b=2\n", "c=25.0\n", "d=2.0\n", "\n", "print(\"6 + a / 5 * b = {0}\".format(6+a/5*b))\n", "print(\"a / b * b = {0}\".format(a/b*b))\n", "print(\"c / d * d = {0}\".format(c/d*d))\n", "print(\"-a={0}\".format(-a))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "6 + a / 5 * b = 16\n", "a / b * b = 24\n", "c / d * d = 25.0\n", "-a=-25\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Program 4.4, Page number: 35" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "a=25\n", "b=5\n", "c=10\n", "d=7\n", "\n", "print(\"a % b = {0}\".format(a%b))\n", "print(\"a % c = {0}\".format(a%c))\n", "print(\"a % d = {0}\".format(a%d))\n", "print(\"a / d * d + a % d = {0}\".format(a/d*d+a%d))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "a % b = 0\n", "a % c = 5\n", "a % d = 4\n", "a / d * d + a % d = 25\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Program 4.5, Page number: 36" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "f1=123.125\n", "f2=1.0\n", "i1=1\n", "i2=-150\n", "c='a'\n", "\n", "\n", "i1=f1 #floating to integer conversion\n", "print(\"{0} assigned to an int produces {1:.0f}\".format(f1,i1))\n", "\n", "f1=i2 #integer to floating conversion\n", "print(\"{0} assigned to a float produces {1}\".format(i2,f1))\n", "\n", "f1=i2/100 #integer divided by integer\n", "print(\"{0} divided by 100 produces {1}\".format(i2,f1))\n", "\n", "f2=i2/100.0 #integer divided by a float\n", "print(\"{0} divided by 100.0 produces {1}\".format(i2,f2))\n", "\n", "f2=float(i2/100) #type cast operator\n", "print(\"{0} divided by 100 produces {1}\".format(i2,f2))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "123.125 assigned to an int produces 123\n", "-150 assigned to a float produces -150\n", "-150 divided by 100 produces -2\n", "-150 divided by 100.0 produces -1.5\n", "-150 divided by 100 produces -2.0\n" ] } ], "prompt_number": 5 } ], "metadata": {} } ] }