{ "metadata": { "name": "", "signature": "sha256:2d1838d005f866ea4e20c93ff6de54ebfd0d0c26c2e3055fc05467a06502c1dc" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "

Chapter 22: The C++ Preprocessor

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 22.1, Page Number: 550

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def MIN(a,b):\n", " if aExample 22.2, Page Number: 551

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "def EVEN(a):\n", " if a%2==0:\n", " return 1\n", " else:\n", " return 0\n", "\n", "if EVEN(9+1):\n", " print \"is even\"\n", "else:\n", " print \"is odd\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "is even\n" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 22.3, Page Number: 551

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def EVEN(a):\n", " if a%2==0:\n", " return 1\n", " else:\n", " return 0\n", "\n", "if EVEN(9+1):\n", " print \"is even\"\n", "else:\n", " print \"is odd\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "is even\n" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 22.4, Page Number: 553

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def MAX():\n", " return 100\n", "\n", "if MAX()>10:\n", " print \"Extra memory required.\"\n", " " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Extra memory required.\n" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 22.5, Page Number: 554

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def MAX():\n", " return 6\n", "\n", "if MAX()>10:\n", " print \"Extra memory required.\"\n", "else:\n", " print \"Current memory OK.\"\n", " " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Current memory OK.\n" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 22.6, Page Number: 556

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def TOM():\n", " pass\n", "\n", "\n", "try:\n", " TOM()\n", "except NameError:\n", " print \"Programmer is unknown.\"\n", "else:\n", " print \"Programmer is Tom.\"\n", " \n", "try:\n", " RALPH()\n", "except NameError:\n", " print \"RALPH not defined.\"\n", " " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Programmer is Tom.\n", "RALPH not defined.\n" ] } ], "prompt_number": 8 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 22.7, Page Number: 558

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "import inspect\n", " \n", "def lineno():\n", " return inspect.currentframe().f_back.f_lineno\n", " \n", "print lineno()+200\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "209\n" ] } ], "prompt_number": 9 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 22.8, Page Number: 559

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "def mkstr(s):\n", " return str(s)\n", "\n", "#Result\n", "print mkstr('I like C++')\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "I like C++\n" ] } ], "prompt_number": 10 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 22.9, Page Number: 560

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def concat(a,b):\n", " return a+b\n", "#Variable declaration\n", "xy=10\n", "\n", "#Result\n", "exec(\"print %s\")%concat('x','y')" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "10\n" ] } ], "prompt_number": 11 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }