{ "metadata": { "name": "", "signature": "sha256:c8fa15513a66bcc3c782e246851674786a1e5f0313759a5d553c588db3808162" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 5: Operators & Expressions" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 5.1, page no. 62" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "a = 20.5\n", "b = 6.4\n", "\n", "print \"a = \", a\n", "print \"b = \", b\n", "print \"a+b = \", (a+b)\n", "print \"a-b = \", (a-b)\n", "print \"a*b = \", (a*b)\n", "print \"a/b = \", (a/b)\n", "print \"a%b = \", (a%b)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "a = 20.5\n", "b = 6.4\n", "a+b = 26.9\n", "a-b = 14.1\n", "a*b = 131.2\n", "a/b = 3.203125\n", "a%b = 1.3\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 5.2, page no. 64" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "a = 15.0\n", "b = 20.75\n", "c = 15.0\n", "print \"a = \", a\n", "print \"b = \", b\n", "print \"c = \", c\n", "print \"ab is \", (a>b)\n", "print \"a==c is \", (a==c)\n", "print \"a<=c is \", (a<=c)\n", "print \"a>=b is \", (a>=b)\n", "print \"b!=c is \", (b!=c)\n", "print \"b==(a+c) is \", (b==(a+c))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "a = 15.0\n", "b = 20.75\n", "c = 15.0\n", "ab is False\n", "a==c is True\n", "a<=c is True\n", "a>=b is False\n", "b!=c is True\n", "b==(a+c) is False\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 5.3, page no. 67" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "#there is no inrement operator in Python, we will use += instead\n", "\n", "m = 10\n", "n = 20\n", "print \"m = \", m\n", "print \"n = \", n\n", "m+=1\n", "print \"++m = \", m \n", "print \"n++ = \", n\n", "n+=1\n", "print \"m = \", m\n", "print \"n = \", n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "m = 10\n", "n = 20\n", "++m = 11\n", "n++ = 20\n", "m = 11\n", "n = 21\n" ] } ], "prompt_number": 18 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 5.4, page no. 73" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "my_sum = 0\n", "for i in range(1, 11):\n", " my_sum = my_sum + (1/float(i))\n", " print \"i = \", i,\n", " print \"sum = \", my_sum" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "i = 1 sum = 1.0\n", "i = 2 sum = 1.5\n", "i = 3 sum = 1.83333333333\n", "i = 4 sum = 2.08333333333\n", "i = 5 sum = 2.28333333333\n", "i = 6 sum = 2.45\n", "i = 7 sum = 2.59285714286\n", "i = 8 sum = 2.71785714286\n", "i = 9 sum = 2.82896825397\n", "i = 10 sum = 2.92896825397\n" ] } ], "prompt_number": 21 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 5.5, page no. 74" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "my_list = []\n", "my_list.append(1)\n", "my_list.append(2)\n", "l = len(my_list)-1\n", "total = 0\n", "while l >= 0:\n", " total = total + my_list[l]\n", " l -= 1\n", "print \"The total amount is \", total" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The total amount is 3\n" ] } ], "prompt_number": 23 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "example 5.6, page no. 77" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "a = 10\n", "b = 5\n", "c = 8\n", "d = 2\n", "x = 6.4\n", "y = 3.0\n", "\n", "answer1 = a * b + c / d\n", "answer2 = a * (b+c) / d\n", "\n", "answer3 = a/c\n", "answer4 = float(a)/c\n", "answer5 = a / y\n", "\n", "answer6 = a % c\n", "answer7 = float(x%y)\n", "\n", "bool1 = a > b and c > d\n", "bool2 = a < b and c > d\n", "bool3 = a < b or c > d\n", "bool4 = not(a - b == c)\n", "\n", "print \"Order of Evaluation\"\n", "print \"a * b + c / d = \", answer1\n", "print \"a * (b+c) / d = \", answer2\n", "\n", "print \"Type Conversion\"\n", "print \"a / c = \", answer3\n", "print \"float(a)/c = \", answer4\n", "print \"a / y = \", answer5\n", "\n", "print \"Modulo Operations\"\n", "print \"a % c = \", answer6\n", "print \"x % y = \", answer6\n", "\n", "print \"Logical Operations\"\n", "print \"a > b and c > d = \", bool1\n", "print \"a < b and c > d = \", bool2\n", "print \"a < b or c > d = \", bool3\n", "print \"not(a - b == c) \", bool4" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Order of Evaluation\n", "a * b + c / d = 54\n", "a * (b+c) / d = 65\n", "Type Conversion\n", "a / c = 1\n", "float(a)/c = 1.25\n", "a / y = 3.33333333333\n", "Modulo Operations\n", "a % c = 2\n", "x % y = 2\n", "Logical Operations\n", "a > b and c > d = True\n", "a < b and c > d = False\n", "a < b or c > d = True\n", "not(a - b == c) True\n" ] } ], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }