{ "metadata": { "name": "", "signature": "sha256:53d2aae81c824182838b074246429ee4c039bafa2ea2a0b5b38839beb8288df9" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 9: Processing macros" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 9.1, Page No.153" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"This is a simple test program\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "This is a simple test program\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 9.2, Page No.154" ] }, { "cell_type": "code", "collapsed": false, "input": [ "BOOK=\"C++ Programming in easy steps\"\n", "NUM=200\n", "RULE=\"*****************************\"\n", "print RULE\n", "print BOOK\n", "print RULE\n", "print \"NUM is:\",NUM\n", "print \"Double NUM:\",NUM*2" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "*****************************\n", "C++ Programming in easy steps\n", "*****************************\n", "NUM is: 200\n", "Double NUM: 400\n" ] } ], "prompt_number": 7 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 9.3, Page No.156" ] }, { "cell_type": "code", "collapsed": false, "input": [ "if not 'BOOK' in locals():\n", " BOOK=\"C++ Programming in easy steps\"\n", "\n", "print BOOK,\n", "if not 'AUTHOR' in locals():\n", " AUTHOR=\"Mike McGrath\"\n", "print \" by \",AUTHOR\n", "if not 'BOOK' in locals():\n", " BOOK=\"\"\n", "if 'BOOK' in locals():\n", " BOOK=\"Linux in easy steps\"\n", "\n", "print BOOK,\" by \",AUTHOR" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Linux in easy steps by Mike McGrath\n", "Linux in easy steps by Mike McGrath\n" ] } ], "prompt_number": 26 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 9.4, Page No.159" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "import platform\n", "plat=platform.system()\n", "print plat,\" system\"\n", "if plat==\"Windows\":\n", " print \"Performing Windows-only tasks\"\n", "else:\n", " print \"Performing Linux-only tasks\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Windows system\n", "Performing Windows-only tasks\n" ] } ], "prompt_number": 32 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 9.5, Page No.160" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def add(x,y):\n", " return x+y\n", "def triple(x):\n", " return (add(x,add(x,x)))\n", "print \"9 + 3 = \",add(9,3)\n", "print \"9 x 3 = \",triple(9)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "9 + 3 = 12\n", "9 x 3 = 27\n" ] } ], "prompt_number": 34 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 9.6, Page No.162" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def SQUARE(n):\n", " return n*n\n", "def CUBE(n):\n", " return n*n*n\n", "num=5\n", "print \"Macro SQUARE:\",SQUARE(num)\n", "print \"inline square:\",SQUARE(num)\n", "print \"Macro CUBE:\",CUBE(num)\n", "print \"inline CUBE:\",CUBE(num)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Macro SQUARE: 25\n", "inline square: 25\n", "Macro CUBE: 125\n", "inline CUBE: 125\n" ] } ], "prompt_number": 35 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 9.7, Page No.164" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def LINEOUT(str):\n", " print str\n", "def GLUEOUT(a,b):\n", " print a,\" \",b\n", "longer=\"They carried a net \"\n", "line=\"and their hearts \"\n", "LINEOUT(\"In a bowl to sea went wise men three\")\n", "LINEOUT(\"On a brilliant night in June\")\n", "GLUEOUT(longer,line)\n", "LINEOUT(\"On fishing up the moon.\")" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "In a bowl to sea went wise men three\n", "On a brilliant night in June\n", "They carried a net and their hearts \n", "On fishing up the moon.\n" ] } ], "prompt_number": 36 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 9.8, Page No.166" ] }, { "cell_type": "code", "collapsed": false, "input": [ "DEBUG=0\n", "if DEBUG == 1:\n", " def ASSERT(expr):\n", " print expr, \"...\", num\n", " if expr != True:\n", " print \"Fails in file: \"# There is no concept of line in python\n", " print \"at line\"# There is no concept of line in python\n", " else:\n", " print \"Succeeds\"\n", "elif DEBUG == 0:\n", " def ASSERT(expr):\n", " print \"Number is \",num\n", "num = 9\n", "ASSERT(num < 10)\n", "num = num + num\n", "ASSERT(num < 10)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Number is 9\n", "Number is 18\n" ] } ], "prompt_number": 10 } ], "metadata": {} } ] }