{ "metadata": { "name": "Chapter 4" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": "Chapter 4: Basic declarations and expressions" }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": "Example 4.1, Page number: 71" }, { "cell_type": "code", "collapsed": false, "input": "# Example 4.1.py\n# To perform a simple calculation\n\n\n# Variable declaration\nx = (1 + 2) * 4\n\n# Result\nprint ('1 plus 2 multiplied by 4 gives %d' % x)", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "1 plus 2 multiplied by 4 gives 12\n" } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": "Example 4.2, Page number: 75" }, { "cell_type": "code", "collapsed": false, "input": "# Example 4.2.py\n# To calculate the twice and thrice of 15\n\n\n# Variable declaration\nterm = 3 * 5\nterm_2 = 2 * term\nterm_3 = 3 * term\n\n# Result\nprint ('Twice %d is %d' % (term, term_2))\nprint ('Three times %d is %d' % (term, term_3))", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "Twice 15 is 30\nThree times 15 is 45\n" } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": "Example 4.3, Page number: 77" }, { "cell_type": "code", "collapsed": false, "input": "# Example 4.3.py\n# To calculate the twice and thrice of 15\n\n\n# Variable declaration\nterm = 3 * 5\nterm_2 = 2 * term\nterm_3 = 3 * term\n\n# Result\nprint ('Twice %d is %d' % (term, term_2))\nprint ('Three times %d is %d' % (term, term_3))", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "Twice 15 is 30\nThree times 15 is 45\n" } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": "Example 4.4, Page number: 79" }, { "cell_type": "code", "collapsed": false, "input": "# Example 4.4.py\n# To determine the value of 1/3\n\n\n# Variable declaration\nanswer = 1/3\n\n# Result\nprint ('The value of 1/3 is %f' % answer)", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "The value of 1/3 is 0.000000\n" } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": "Example 4.5, Page number: 80" }, { "cell_type": "code", "collapsed": false, "input": "# Example 4.5.py\n# To determine the value of 2 + 2\n\n\n# Variable declaration\nanswer = 2 + 2\n\n# Result\nprint ('The answer is %d' % answer)", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "The answer is 4\n" } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": "Example 4.6, Page number: 80" }, { "cell_type": "code", "collapsed": false, "input": "# Example 4.6.py\n# To determine the integer equivalent of 7.0/22.0 \n\n\n# Variable declaration\nresult = 7.0/22.0\n\n# Result\nprint ('The result is %d' % result)", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "The result is 0\n" } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": "Example 4.7, Page number: 82" }, { "cell_type": "code", "collapsed": false, "input": "# Example 4.7.py\n# To determine the reverse of 3 characters\n\n\n# Variable declaration\nchar1 = 'A'\nchar2 = 'B'\nchar3 = 'C'\n\n# Result\nprint ('%c%c%c reversed is %c%c%c' % (char1, char2, char3, char3, char2, char1))", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "ABC reversed is CBA\n" } ], "prompt_number": 7 } ], "metadata": {} } ] }