diff options
author | tslee | 2014-11-27 17:17:59 +0530 |
---|---|---|
committer | tslee | 2014-11-27 17:17:59 +0530 |
commit | 6e3407ba85ae84e1cee1ae0c972fd32c5504d827 (patch) | |
tree | b89808101c39b1db1e3793eada2c8b702f856606 /Mastering_C/chapter2.ipynb | |
parent | 36a03d6d76bac315dba73b2ba9555c7e3fe0234f (diff) | |
download | Python-Textbook-Companions-6e3407ba85ae84e1cee1ae0c972fd32c5504d827.tar.gz Python-Textbook-Companions-6e3407ba85ae84e1cee1ae0c972fd32c5504d827.tar.bz2 Python-Textbook-Companions-6e3407ba85ae84e1cee1ae0c972fd32c5504d827.zip |
added books
Diffstat (limited to 'Mastering_C/chapter2.ipynb')
-rw-r--r-- | Mastering_C/chapter2.ipynb | 981 |
1 files changed, 981 insertions, 0 deletions
diff --git a/Mastering_C/chapter2.ipynb b/Mastering_C/chapter2.ipynb new file mode 100644 index 00000000..03566b1a --- /dev/null +++ b/Mastering_C/chapter2.ipynb @@ -0,0 +1,981 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:c47c99279bcc1ed930609c37047619e07a7ba5a5f3802de8eeb54d6cf31ebdd4" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 2: Variables and Expressions" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example integer.c, page no. 35" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "val = 10\n", + "print \"%d\" %val" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "10\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example float.c, page no. 36" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "i_var = 10\n", + "f_var = 100.3125\n", + "print \"i_var is %d and f_var is %f\" %(i_var, f_var)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "i_var is 10 and f_var is 100.312500\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example decimal.c, page no. 37" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "inp_int = int(raw_input())\n", + "print \"The input integer (in decimal) is %u\" %inp_int\n", + "print \"The input integer (in hexadecimal) is %x\" %inp_int" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "45\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The input integer (in decimal) is 45\n", + "The input integer (in hexadecimal) is 2d\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example scanf.c, page no. 37" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "years = float(raw_input(\"Input your age in years: \"))\n", + "secs = years*365*24*60*60\n", + "print \"You have lived for %0.1f seconds\" %secs" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input your age in years: 21\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "You have lived for 662256000.0 seconds\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example char.c, page no. 38" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "c = '#'\n", + "print \"This is the hash symbol: %s\" %c" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "This is the hash symbol: #\n" + ] + } + ], + "prompt_number": 12 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example ascii.c, page no. 39 " + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "code = int(raw_input(\"Input an ASCII code (0 to 127):\" ))\n", + "symbol = chr(code)\n", + "print \"The symbol corresponding to %d is %s\" %(code,symbol)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input an ASCII code (0 to 127):75\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The symbol corresponding to 75 is K\n" + ] + } + ], + "prompt_number": 14 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example string.c, page no. 40" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "my_string = \"13 is unlucky.\"\n", + "print \"my_string: %s\" %my_string" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "my_string: 13 is unlucky.\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example size.c ,page no. 41" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import sys\n", + "print \"Size of a int is %d\" %sys.getsizeof(int)\n", + "print \"Size of a long int is %d\" %sys.getsizeof(long)\n", + "\n", + "#Note: there is no short interger data type in Python\n", + "#Answers will differ" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Size of a int is 872\n", + "Size of a long int is 872\n" + ] + } + ], + "prompt_number": 27 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example inout.c, page no. 42" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "shorti = int(raw_input(\"Input short int: \"))\n", + "longi = int(raw_input(\"Input long int: \"))\n", + "print \"shorti = %d and longi = %d\" %(shorti, longi)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input short int: 12\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input long int: 200000000\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "shorti = 12 and longi = 200000000\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example beep.c, page no. 47" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "print \"%c\" %('\\x07')\n", + "\n", + "#note: there won't be any beep in Python" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\u0007\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example tax.c, page no. 49" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "sal = float(raw_input(\"Salary: \"))\n", + "tax = sal * 0.3\n", + "print \"tax = %.1f\" %tax" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Salary: 200000\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "tax = 60000.0\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example tax1.c, page no. 49" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "tax_rate = 0.3\n", + "sal = float(raw_input(\"Salary: \"))\n", + "tax = sal * tax_rate\n", + "print \"tax = %.1f\" %tax" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Salary: 200000\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "tax = 60000.0\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example const.c, page no. 50" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "tax_rate = 0.3\n", + "sal = float(raw_input(\"Salary: \"))\n", + "tax = sal * tax_rate\n", + "print \"tax = %.1f\" %tax\n", + "\n", + "#Note: there is no constant data type in Python so the program will remain same as above" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Salary: 200000\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "tax = 60000.0\n" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example const.c, page no. 50" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "tax_rate = 0.3\n", + "sal = float(raw_input(\"Salary: \"))\n", + "tax = sal * tax_rate\n", + "print \"tax = %.1f\" %tax\n", + "\n", + "#Note: there is no #define preprocessor in Python so the program will remain same as above" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Salary: 200000\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "tax = 60000.0\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example relation.c, page no. 53" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "print \"Input two numbers\"\n", + "i = int(raw_input(\"num1: \"))\n", + "j = int(raw_input(\"num2: \"))\n", + "if (i == j):\n", + " print \"They are equal.\"\n", + "print \"Got the message?\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input two numbers\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "num1: 34\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "num2: 34\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "They are equal.\n", + "Got the message?\n" + ] + } + ], + "prompt_number": 11 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example char.c, page no. 54" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "c = 255\n", + "d = -1\n", + "if c < 0:\n", + " print \"c is less than 0\"\n", + "else:\n", + " print \"c is not less than 0\"\n", + "if d < 0:\n", + " print \"d is less than 0\"\n", + "else:\n", + " print \"d is not less than 0\"\n", + " \n", + "#the program will not have output as that given in the textbook\n", + "#because, there is no concept of char, signed or unsigned values in Python\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "c is not less than 0\n", + "d is less than 0\n" + ] + } + ], + "prompt_number": 22 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example char8.c, page no. 55" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "c = '255'\n", + "d = '-1'\n", + "if c < 0:\n", + " print \"c is less than 0\"\n", + "else:\n", + " print \"c is not less than 0\"\n", + "if d < 0:\n", + " print \"d is less than 0\"\n", + "else:\n", + " print \"d is not less than 0\"\n", + "\n", + "#the program will not have output as that given in the textbook\n", + "#because, there is no concept of char, signed or unsigned values in Python" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "c is not less than 0\n", + "d is not less than 0\n" + ] + } + ], + "prompt_number": 23 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example extract.c, page no. 59" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "i = int(raw_input(\"Input an integer: \"))\n", + "n = int(raw_input(\"Bit position to extract: \"))\n", + "bit = (i >> n)&1\n", + "print \"The bit is: %d\" %bit" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input an integer: 10\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Bit position to extract: 1\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The bit is: 1\n" + ] + } + ], + "prompt_number": 29 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example max.c, page no. 62" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "print \"Input two numbers\"\n", + "i = int(raw_input(\"num1: \"))\n", + "j = int(raw_input(\"num2: \"))\n", + "larger = i if i > j else j\n", + "print \"The larger of the two is %d\" %larger" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Input two numbers\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "num1: 34\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "num2: 45\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The larger of the two is 45\n" + ] + } + ], + "prompt_number": 33 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example interest.c, page no. 65" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "p = float(raw_input(\"Principal: \"))\n", + "r = float(raw_input(\"Rate: \"))\n", + "t = float(raw_input(\"Time: \"))\n", + "ncmp_year = float(raw_input(\"Compoundings per year: \"))\n", + "simple = p * r * t / 100\n", + "cmp_amount = p * pow(1 + r / (ncmp_year * 100), ncmp_year * t)\n", + "compound = cmp_amount - p\n", + "print \"The simple interest is : %f\" %simple\n", + "print \"The compound interest is : %f\" %compound" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Principal: 1000\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Rate: 10\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Time: 2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Compoundings per year: 4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The simple interest is : 200.000000\n", + "The compound interest is : 218.402898\n" + ] + } + ], + "prompt_number": 35 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.10, page no. 66" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "c = float(raw_input(\"Enter temperature in celsius: \"))\n", + "f = 1.8 * c + 32\n", + "print \"Equivalent fahrenheit = %f\" %f\n", + "f = float(raw_input(\"Enter temperature in fahrenheit: \"))\n", + "c = (f - 32) / 1.8\n", + "print \"Equivalent celsius = %f\" %c\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter temperature in celsius: 10\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Equivalent fahrenheit = 50.000000\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter temperature in fahrenheit: 50\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Equivalent celsius = 10.000000\n" + ] + } + ], + "prompt_number": 36 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.11, page no. 66" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import math\n", + "print \"Enter the 3 sides: \"\n", + "a = float(raw_input(\"side1:\"))\n", + "b = float(raw_input(\"side2:\"))\n", + "c = float(raw_input(\"side3:\"))\n", + "peri = a + b + c\n", + "s = peri / 2\n", + "area = math.sqrt(s * (s - a) * (s - b) * (s - c))\n", + "print \"Area of the triangle is %f\" %area" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the 3 sides: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "side1:4\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "side2:5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "side3:6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Area of the triangle is 9.921567\n" + ] + } + ], + "prompt_number": 39 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.12, page no. 67" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "b = float(raw_input(\"Enter the base: \"))\n", + "h = float(raw_input(\"Enter the height: \"))\n", + "area = b * h / 2\n", + "print \"Area of the triangle is %f\" %area" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the base: 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the height: 2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Area of the triangle is 3.000000\n" + ] + } + ], + "prompt_number": 40 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file |