{ "metadata": { "name": "", "signature": "sha256:e0192e34ad6ee5352df14e6445eee01302e3e7af1b8414d8a9190c3936e04b7a" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 10: C Preprocessor" ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Example 10.1, Page number: 172" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "data = []\n", "twice = []\n", "\n", "# Calculation and result\n", "for index in range (0, 10) :\n", " data.append(index)\n", " twice.append(2 * index)\n", "\n", "print (data)\n", "print (twice)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n", "[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Example 10.2, Page number: 173" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "data = []\n", "twice = []\n", "\n", "# Calculation and result\n", "for index in range (0, 20) :\n", " data.append(index)\n", " twice.append(2 * index)\n", "\n", "print (data)\n", "print (twice)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]\n", "[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38]\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Example 10.3, Page number: 175" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "BIG_NUMBER = 10 * 10\n", "index = 1\n", "\n", "# Calculation and result\n", "while (index < BIG_NUMBER) :\n", " index = index * 8\n", " print ('%d' % index)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "8\n", "64\n", "512\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Example 10.4, Page number: 176" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "FIRST_PART = 7\n", "LAST_PART = 5\n", "ALL_PARTS = FIRST_PART + LAST_PART\n", "\n", "# Calculation and result\n", "print ('The square of all the parts is %d' % (ALL_PARTS * ALL_PARTS))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The square of all the parts is 144\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Example 10.5, Page number: 177" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "for i in reversed (range(10)) :\n", " print ('Hi there')" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Hi there\n", "Hi there\n", "Hi there\n", "Hi there\n", "Hi there\n", "Hi there\n", "Hi there\n", "Hi there\n", "Hi there\n", "Hi there\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Example 10.6, Page number: 177" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "size = 10\n", "fudge = size - 2\n", "\n", "# Calculation and result\n", "size = fudge\n", "print ('Size is %d' % size)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Size is 8\n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Example 10.7, Page number: 178" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "import sys\n", "value = 1\n", "\n", "# Calculation and result\n", "if (value < 0) :\n", " sys.exit()\n", "\n", "print ('We did not die')" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "We did not die\n" ] } ], "prompt_number": 7 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Example 10.8, Page number: 184" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def SQR (x) :\n", " return x * x\n", " \n", "for counter in range (0, 5) :\n", " print ('x %d, x squared %d\\n' % (counter + 1, SQR (counter + 1)))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "x 1, x squared 1\n", "\n", "x 2, x squared 4\n", "\n", "x 3, x squared 9\n", "\n", "x 4, x squared 16\n", "\n", "x 5, x squared 25\n", "\n" ] } ], "prompt_number": 8 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Example 10.9, Page number: 184" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "counter = 0\n", "\n", "# Function declaration, calculation and result\n", "def SQR (x) :\n", " return x * x\n", " \n", "while (counter < 5) :\n", " print ('x %d square %d\\n' % (counter + 1, SQR (counter + 1)))\n", " counter += 1" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "x 1 square 1\n", "\n", "x 2 square 4\n", "\n", "x 3 square 9\n", "\n", "x 4 square 16\n", "\n", "x 5 square 25\n", "\n" ] } ], "prompt_number": 9 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Example 10.10, Page number: 185" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def reciprocal (number) :\n", " return 1 / number\n", " \n", "for counter in range (1, 10) :\n", " print ('1/%f = %f\\n' % (counter, reciprocal (counter)))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1/1.000000 = 1.000000\n", "\n", "1/2.000000 = 0.000000\n", "\n", "1/3.000000 = 0.000000\n", "\n", "1/4.000000 = 0.000000\n", "\n", "1/5.000000 = 0.000000\n", "\n", "1/6.000000 = 0.000000\n", "\n", "1/7.000000 = 0.000000\n", "\n", "1/8.000000 = 0.000000\n", "\n", "1/9.000000 = 0.000000\n", "\n" ] } ], "prompt_number": 10 } ], "metadata": {} } ] }