diff options
author | Jovina Dsouza | 2014-06-18 12:43:07 +0530 |
---|---|---|
committer | Jovina Dsouza | 2014-06-18 12:43:07 +0530 |
commit | 206d0358703aa05d5d7315900fe1d054c2817ddc (patch) | |
tree | f2403e29f3aded0caf7a2434ea50dd507f6545e2 /Programming_in_C/Chapter_04.ipynb | |
parent | c6f0d6aeb95beaf41e4b679e78bb42c4ffe45a40 (diff) | |
download | Python-Textbook-Companions-206d0358703aa05d5d7315900fe1d054c2817ddc.tar.gz Python-Textbook-Companions-206d0358703aa05d5d7315900fe1d054c2817ddc.tar.bz2 Python-Textbook-Companions-206d0358703aa05d5d7315900fe1d054c2817ddc.zip |
adding book
Diffstat (limited to 'Programming_in_C/Chapter_04.ipynb')
-rw-r--r-- | Programming_in_C/Chapter_04.ipynb | 262 |
1 files changed, 262 insertions, 0 deletions
diff --git a/Programming_in_C/Chapter_04.ipynb b/Programming_in_C/Chapter_04.ipynb new file mode 100644 index 00000000..f7f7405b --- /dev/null +++ b/Programming_in_C/Chapter_04.ipynb @@ -0,0 +1,262 @@ +{ + "metadata": { + "name": "Chapter IV" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 4: Variables, data types, and arithmetic expressions" + ] + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 4.1, Page number: 26" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#4.1.py\n", + "#Using the Basic Data Types\n", + "\n", + "#Variable Declarations\n", + "integerVar=100\n", + "floatingVar=331.79\n", + "doubleVar=8.44e+11\n", + "charVar='w'\n", + "boolVar=bool(0)\n", + "\n", + "#Result\n", + "print(\"integerVar={0}\".format(integerVar))\n", + "print(\"floatingVar={0}\".format(floatingVar))\n", + "print(\"doubleVar={0}\".format(doubleVar))\n", + "print(\"charVar={0}\".format(charVar))\n", + "print(\"boolVar={0}\".format(boolVar))" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "integerVar=100\n", + "floatingVar=331.79\n", + "doubleVar=8.44e+11\n", + "charVar=w\n", + "boolVar=False\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 4.2, Page number: 30" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#4.2.py\n", + "#Using the Arithmetic Operators\n", + "\n", + "#Variable declarations\n", + "a=100\n", + "b=2\n", + "c=25\n", + "d=4\n", + "\n", + "#Calculations/Results\n", + "result=a-b #subtraction\n", + "print(\"a-b={0}\".format(result)) #result\n", + "\n", + "result=b*c #multiplication\n", + "print(\"b*c={0}\".format(result)) #result\n", + "\n", + "result=a/c #division\n", + "print(\"a/c={0}\".format(result)) #result\n", + "\n", + "result=a+b*c #precedence\n", + "print(\"a+b*c={0}\".format(result)) #result \n", + "\n", + "print(\"a*b+c*d={0}\".format(a*b+c*d)) #direct calculation" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "a-b=98\n", + "b*c=50\n", + "a/c=4\n", + "a+b*c=150\n", + "a*b+c*d=300\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 4.3, Page number: 33" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#4.3.py\n", + "#More Examples with Arithmetic Operations\n", + "\n", + "#Variable Declarations\n", + "a=25\n", + "b=2\n", + "c=25.0\n", + "d=2.0\n", + "\n", + "#Calculations/Result\n", + "print(\"6 + a / 5 * b = {0}\".format(6+a/5*b))\n", + "print(\"a / b * b = {0}\".format(a/b*b))\n", + "print(\"c / d * d = {0}\".format(c/d*d))\n", + "print(\"-a={0}\".format(-a))" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "6 + a / 5 * b = 16\n", + "a / b * b = 24\n", + "c / d * d = 25.0\n", + "-a=-25\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 4.4, Page number: 35" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#4.4.py\n", + "#Illustrating the Modulus Operator\n", + "\n", + "#Variable Declarations\n", + "a=25\n", + "b=5\n", + "c=10\n", + "d=7\n", + "\n", + "#Calculations/Result\n", + "print(\"a % b = {0}\".format(a%b))\n", + "print(\"a % c = {0}\".format(a%c))\n", + "print(\"a % d = {0}\".format(a%d))\n", + "print(\"a / d * d + a % d = {0}\".format(a/d*d+a%d))" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "a % b = 0\n", + "a % c = 5\n", + "a % d = 4\n", + "a / d * d + a % d = 25\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Program 4.5, Page number: 36" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#4.5.py\n", + "#Converting Between Integers and Floats\n", + "\n", + "#Variable Declarations\n", + "f1=123.125\n", + "f2=1.0\n", + "i1=1\n", + "i2=-150\n", + "c='a'\n", + "\n", + "\n", + "i1=f1 #floating to integer conversion\n", + "print(\"{0} assigned to an int produces {1:.0f}\".format(f1,i1))\n", + "\n", + "f1=i2 #integer to floating conversion\n", + "print(\"{0} assigned to a float produces {1}\".format(i2,f1))\n", + "\n", + "f1=i2/100 #integer divided by integer\n", + "print(\"{0} divided by 100 produces {1}\".format(i2,f1))\n", + "\n", + "f2=i2/100.0 #integer divided by a float\n", + "print(\"{0} divided by 100.0 produces {1}\".format(i2,f2))\n", + "\n", + "f2=float(i2/100) #type cast operator\n", + "print(\"{0} divided by 100 produces {1}\".format(i2,f2))" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "123.125 assigned to an int produces 123\n", + "-150 assigned to a float produces -150\n", + "-150 divided by 100 produces -2\n", + "-150 divided by 100.0 produces -1.5\n", + "-150 divided by 100 produces -2.0\n" + ] + } + ], + "prompt_number": 5 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file |