From 8048392490bd2efe0fdfa001945f663cba969841 Mon Sep 17 00:00:00 2001
From: nice
Date: Thu, 9 Oct 2014 18:07:00 +0530
Subject: updated books
---
.../KamthaneChapter10.ipynb | 2940 ++
.../KamthaneChapter11.ipynb | 446 +
.../KamthaneChapter12.ipynb | 849 +
.../KamthaneChapter13.ipynb | 2367 ++
.../KamthaneChapter14.ipynb | 2311 +
.../KamthaneChapter15.ipynb | 754 +
.../KamthaneChapter16.ipynb | 2160 +
.../KamthaneChapter3.ipynb | 1060 +
.../KamthaneChapter4.ipynb | 972 +
.../KamthaneChapter5.ipynb | 2624 ++
.../KamthaneChapter6.ipynb | 42312 +++++++++++++++++++
.../KamthaneChapter7.ipynb | 3644 ++
.../KamthaneChapter8.ipynb | 2750 ++
.../KamthaneChapter9.ipynb | 1999 +
Programming_in_C_using_ANSI_C/README.txt | 10 +
.../screenshots/screenshot1.png | Bin 0 -> 124312 bytes
.../screenshots/screenshot1_1.png | Bin 0 -> 124312 bytes
.../screenshots/screenshot2.png | Bin 0 -> 133043 bytes
.../screenshots/screenshot2_1.png | Bin 0 -> 133043 bytes
.../screenshots/screenshot3.png | Bin 0 -> 125297 bytes
.../screenshots/screenshot3_1.png | Bin 0 -> 125297 bytes
21 files changed, 67198 insertions(+)
create mode 100755 Programming_in_C_using_ANSI_C/KamthaneChapter10.ipynb
create mode 100755 Programming_in_C_using_ANSI_C/KamthaneChapter11.ipynb
create mode 100755 Programming_in_C_using_ANSI_C/KamthaneChapter12.ipynb
create mode 100755 Programming_in_C_using_ANSI_C/KamthaneChapter13.ipynb
create mode 100755 Programming_in_C_using_ANSI_C/KamthaneChapter14.ipynb
create mode 100755 Programming_in_C_using_ANSI_C/KamthaneChapter15.ipynb
create mode 100755 Programming_in_C_using_ANSI_C/KamthaneChapter16.ipynb
create mode 100755 Programming_in_C_using_ANSI_C/KamthaneChapter3.ipynb
create mode 100755 Programming_in_C_using_ANSI_C/KamthaneChapter4.ipynb
create mode 100755 Programming_in_C_using_ANSI_C/KamthaneChapter5.ipynb
create mode 100755 Programming_in_C_using_ANSI_C/KamthaneChapter6.ipynb
create mode 100755 Programming_in_C_using_ANSI_C/KamthaneChapter7.ipynb
create mode 100755 Programming_in_C_using_ANSI_C/KamthaneChapter8.ipynb
create mode 100755 Programming_in_C_using_ANSI_C/KamthaneChapter9.ipynb
create mode 100755 Programming_in_C_using_ANSI_C/README.txt
create mode 100755 Programming_in_C_using_ANSI_C/screenshots/screenshot1.png
create mode 100755 Programming_in_C_using_ANSI_C/screenshots/screenshot1_1.png
create mode 100755 Programming_in_C_using_ANSI_C/screenshots/screenshot2.png
create mode 100755 Programming_in_C_using_ANSI_C/screenshots/screenshot2_1.png
create mode 100755 Programming_in_C_using_ANSI_C/screenshots/screenshot3.png
create mode 100755 Programming_in_C_using_ANSI_C/screenshots/screenshot3_1.png
(limited to 'Programming_in_C_using_ANSI_C')
diff --git a/Programming_in_C_using_ANSI_C/KamthaneChapter10.ipynb b/Programming_in_C_using_ANSI_C/KamthaneChapter10.ipynb
new file mode 100755
index 00000000..68503d70
--- /dev/null
+++ b/Programming_in_C_using_ANSI_C/KamthaneChapter10.ipynb
@@ -0,0 +1,2940 @@
+{
+ "metadata": {
+ "name": ""
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "
Chapter 10: Functions"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 10.1, Page number: 320"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#user defined function\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Variable Initialization\n",
+ "x = 1\n",
+ "y = 2\n",
+ "\n",
+ "#Function definition\n",
+ "def add(a,b):\n",
+ " return a+b\n",
+ "\n",
+ "#Function call\n",
+ "z = add(x,y)\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"z = %d\"%(z))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "z = 3"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 10.2, Page number: 321"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Call user-defined function at different places.\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Function definitions\n",
+ "def y():\n",
+ " sys.stdout.write(\" Y\")\n",
+ " return\n",
+ "\n",
+ "def a():\n",
+ " sys.stdout.write(\" A\")\n",
+ " y()\n",
+ " return\n",
+ "\n",
+ "def b():\n",
+ " sys.stdout.write(\" B\")\n",
+ " a()\n",
+ " return\n",
+ "\n",
+ "def c():\n",
+ " a()\n",
+ " b()\n",
+ " sys.stdout.write(\" C\")\n",
+ " return\n",
+ "\n",
+ "def d():\n",
+ " sys.stdout.write(\" D\")\n",
+ " c()\n",
+ " b()\n",
+ " a()\n",
+ " return\n",
+ "\n",
+ "#Function calls\n",
+ "y()\n",
+ "a()\n",
+ "b()\n",
+ "c()\n",
+ "d()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Y A Y B A Y A Y B A Y C D A Y B A Y C B A Y A Y"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 10.3, Page number: 323"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Using similar variable names in different functions.\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Function definition\n",
+ "def fun():\n",
+ " b = 20\n",
+ " c = 10\n",
+ " sys.stdout.write(\"\\nIn fun() B = %d C = %d\"%(b,c))\n",
+ " return\n",
+ "\n",
+ "#Variable Initialization\n",
+ "b = 10\n",
+ "c = 5\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"\\nIn main() B = %d C = %d\"%(b,c))\n",
+ "fun()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "In main() B = 10 C = 5\n",
+ "In fun() B = 20 C = 10"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 10.4, Page number: 323"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Global variables on different functions.\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Global Variable Initialization\n",
+ "b = 10\n",
+ "c = 5\n",
+ "\n",
+ "def fun():\n",
+ " global b \n",
+ " b += 1\n",
+ " global c \n",
+ " c -= 1\n",
+ " sys.stdout.write(\"\\nIn fun() B = %d C = %d\"%(b,c))\n",
+ " return\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"\\nIn main() B = %d C = %d\"%(b,c))\n",
+ "fun()\n",
+ "b += 1\n",
+ "c -= 1\n",
+ "sys.stdout.write(\"\\nAgain In main() B = %d C = %d\"%(b,c))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "In main() B = 10 C = 5\n",
+ "In fun() B = 11 C = 4\n",
+ "Again In main() B = 12 C = 3"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 10.5, Page number: 325"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Using return statement in different ways\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Function definition\n",
+ "def pass1(a):\n",
+ " if a == 0:\n",
+ " return;\n",
+ " else:\n",
+ " return a*a*a\n",
+ "\n",
+ "#Variable Initialization\n",
+ "x = int(raw_input(\"Enter value of x : \"))\n",
+ "\n",
+ "#Function call & Result\n",
+ "if x != 1 or x > 0:\n",
+ " y = pass1(x)\n",
+ "\n",
+ "#There is no switch statement in python, so if..else statement\n",
+ "if y == 1:\n",
+ " sys.stdout.write(\"The value returned is %d\"%(y))\n",
+ "else:\n",
+ " sys.stdout.write(\"The Cube of %d is : %d\"%(x,y))\n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter value of x : 5\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The Cube of 5 is : 125"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 10.6, Page number: 327"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Display message using user defined function\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Function definition\n",
+ "def message():\n",
+ " sys.stdout.write(\"Have a nice day\")\n",
+ "\n",
+ "#function call\n",
+ "message()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Have a nice day"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 10.7, Page number: 328"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Display Alphabets 'A','B' and 'C' using functions\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Functions definitions\n",
+ "def a():\n",
+ " sys.stdout.write(\"\\nA\")\n",
+ " \n",
+ "def b():\n",
+ " sys.stdout.write(\" B\")\n",
+ " \n",
+ "def c():\n",
+ " sys.stdout.write(\" C\")\n",
+ " \n",
+ "#Function call\n",
+ "a()\n",
+ "b()\n",
+ "c()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "A B C"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 10.8, Page number: 329"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Send value to user defined function and display results\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Function Definition\n",
+ "def dat(x,y,z):\n",
+ " sys.stdout.write(\"Date = %d/%d/%d\"%(x,y,z))\n",
+ " \n",
+ "#Variable Initialization\n",
+ "d = int(raw_input(\"Enter date dd/mm/yy\"))\n",
+ "m = int(raw_input(\"Enter date dd/mm/yy\"))\n",
+ "y = int(raw_input(\"Enter date dd/mm/yy\"))\n",
+ "\n",
+ "#function call & Result\n",
+ "dat(d,m,y)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter date dd/mm/yy12\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter date dd/mm/yy12\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter date dd/mm/yy2001\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Date = 12/12/2001"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 10.9, Page number: 330"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Square of number using user defined function\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#function definition\n",
+ "def sqr(k):\n",
+ " sys.stdout.write(\"\\n%d\"%(k*k))\n",
+ " \n",
+ "#Variable Initialization\n",
+ "j = 0\n",
+ "\n",
+ "#Function call & Result\n",
+ "for j in range(1,5):\n",
+ " sqr(j)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "1\n",
+ "4\n",
+ "9\n",
+ "16"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 10.10, Page number: 330"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Pass the value to main() function\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#There is no main function in python\n",
+ "sys.stdout.write(\"\\nNumber of command line arguments J = %d\"%(len(sys.argv)))\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Number of command line arguments J = 6"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 10.11, Page number: 331"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Pass and return values to user defined function. \n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Function Definitions\n",
+ "def dat(x,y,z):\n",
+ " sys.stdout.write(\"\\nToday = %d/%d/%d\"%(x,y,z))\n",
+ " x += 1\n",
+ " return x\n",
+ "\n",
+ "#Variable Initialization\n",
+ "d = int(raw_input(\"Enter Date dd/mm/yy : \"))\n",
+ "m = int(raw_input(\"Enter Date dd/mm/yy : \"))\n",
+ "y = int(raw_input(\"Enter Date dd/mm/yy : \"))\n",
+ "\n",
+ "#Function call\n",
+ "t = dat(d,m,y)\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"\\nTomorrow = %d/%d/%d\"%(t,m,y))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Date dd/mm/yy : 12\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Date dd/mm/yy : 12\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Date dd/mm/yy : 2001\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Today = 12/12/2001\n",
+ "Tomorrow = 13/12/2001"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 10.12, Page number: 332"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Send and recieve values to user defined function \n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Function definition\n",
+ "def sum1(x,y,z):\n",
+ " return x+y+z\n",
+ "\n",
+ "#Variable Initialization\n",
+ "a = int(raw_input(\"Enter Three Numbers : \"))\n",
+ "b = int(raw_input(\"Enter Three Numbers : \"))\n",
+ "c = int(raw_input(\"Enter Three Numbers : \"))\n",
+ "\n",
+ "#Function call\n",
+ "s = sum1(a,b,c)\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"Sum = %d\"%(s))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Three Numbers : 7\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Three Numbers : 5\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Three Numbers : 4\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Sum = 16"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 10.13, Page number: 333"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Recieve values from user defined function without passing any \n",
+ "#value through main().\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Function definition\n",
+ "def sum1():\n",
+ " x = int(raw_input(\"Enter Three Numbers : \"))\n",
+ " y = int(raw_input(\"Enter Three Numbers : \"))\n",
+ " z = int(raw_input(\"Enter Three Numbers : \"))\n",
+ " return x+y+z\n",
+ "\n",
+ "#Function call\n",
+ "s = sum1()\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"Sum = %d\"%(s))\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Three Numbers : 3\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Three Numbers : 5\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Three Numbers : 4\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Sum = 12"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 10.14, Page number: 333"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Return value in the form of address.\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Function definition\n",
+ "def sum1():\n",
+ " x = int(raw_input(\"Enter Three Values : \"))\n",
+ " y = int(raw_input(\"Enter Three Values : \"))\n",
+ " z = int(raw_input(\"Enter Three Values : \"))\n",
+ " k = x + y + z\n",
+ " return k\n",
+ "\n",
+ "#There is no pointer concept in python\n",
+ "\n",
+ "#Function call\n",
+ "s = sum1()\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"Sum = %d\"%(s))\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Three Values : 3\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Three Values : 5\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Three Values : 4\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Sum = 12"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 10.15, Page number: 334"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Call by value\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Function Definition\n",
+ "def change(a,b):\n",
+ " k = a\n",
+ " a = b\n",
+ " b = k\n",
+ " sys.stdout.write(\"\\nIn Change() X = %d Y = %d\"%(a,b))\n",
+ " \n",
+ "#Variable Initialization\n",
+ "x = int(raw_input(\"Enter Values of X & Y : \"))\n",
+ "y = int(raw_input(\"Enter Values of X & Y : \"))\n",
+ "\n",
+ "#Function call\n",
+ "change(x,y)\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"\\nIn main() X = %d Y = %d\"%(x,y))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Values of X & Y : 5\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Values of X & Y : 4\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "In Change() X = 4 Y = 5\n",
+ "In main() X = 5 Y = 4"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 10.16, Page number: 335"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Call by reference \n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Function Definition\n",
+ "def change(a,b):\n",
+ " k = a\n",
+ " a = b\n",
+ " b = k\n",
+ " sys.stdout.write(\"\\nIn Change() X = %d Y = %d\"%(a,b))\n",
+ " \n",
+ "#There is no pointer concept in python\n",
+ "\n",
+ "#Variable Initialization\n",
+ "x = int(raw_input(\"Enter Values of X & Y : \"))\n",
+ "y = int(raw_input(\"Enter Values of X & Y : \"))\n",
+ "\n",
+ "#Function call\n",
+ "change(x,y)\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"\\nIn main() X = %d Y = %d\"%(y,x))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Values of X & Y : 5\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Values of X & Y : 4\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "In Change() X = 4 Y = 5\n",
+ "In main() X = 4 Y = 5"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 10.17, Page number: 336"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Return by reference\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Function Definition\n",
+ "def change(a,b):\n",
+ " c = a + b\n",
+ " d = a - b\n",
+ " return c,d\n",
+ "\n",
+ "#Variable Initialization\n",
+ "x = int(raw_input(\"Enter Values of X & Y : \"))\n",
+ "y = int(raw_input(\"Enter Values of X & Y : \"))\n",
+ "\n",
+ "#Function call\n",
+ "add,sub = change(x,y)\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"\\nAddition : %d\"%(add))\n",
+ "sys.stdout.write(\"\\nSubtraction : %d\"%(sub))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Values of X & Y : 5\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Values of X & Y : 4\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Addition : 9\n",
+ "Subtraction : 1"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 10.18, Page number: 337"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Call by value and reference\n",
+ "\n",
+ "k = 0\n",
+ "m = 0\n",
+ "\n",
+ "#Function Definition\n",
+ "def other(k,m):\n",
+ " sys.stdout.write(\"\\nAddress of k & m in other() : %u %u\"%(id(k),id(m)))\n",
+ " \n",
+ "#Result\n",
+ "sys.stdout.write(\"\\nAddress of k & m in main() : %u %u\"%(id(k),id(m)))\n",
+ "\n",
+ "#Function call\n",
+ "other(k,m)\n",
+ "\n",
+ "#there is no pointer concept in python and it uses value tagged method in data storage\n",
+ "#instead of addressing the memory location, values of same variables are tagged together"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Address of k & m in main() : 30922996 30922996\n",
+ "Address of k & m in other() : 30922996 30922996"
+ ]
+ }
+ ],
+ "prompt_number": 15
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 10.19, Page number: 338"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#User defined function as an argument to another function.\n",
+ "\n",
+ "#Variable Initialization\n",
+ "y = 2\n",
+ "\n",
+ "#Function Definitions\n",
+ "def double(m):\n",
+ " return m*2\n",
+ "\n",
+ "def square(k):\n",
+ " return k*k\n",
+ "\n",
+ "#Function call\n",
+ "x = double(square(y))\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"x = %d\"%(x))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "x = 8"
+ ]
+ }
+ ],
+ "prompt_number": 17
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 10.20, Page number: 338"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Two functions as arguments for another functions\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Function definitions\n",
+ "def x(a,b):\n",
+ " return abs(a-b)\n",
+ "\n",
+ "def y():\n",
+ " y = int(raw_input(\"Enter First Number : \"))\n",
+ " return y\n",
+ "\n",
+ "def z():\n",
+ " z = int(raw_input(\"Enter Second Number : \"))\n",
+ " return z\n",
+ "\n",
+ "#Function call\n",
+ "d = x(y(),z())\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"\\nz() - y() = %d\"%(d))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter First Number : 25\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Second Number : 50\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "z() - y() = 25"
+ ]
+ }
+ ],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 10.21, Page number: 339"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Return only absolute value like abs() function\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Function Definition\n",
+ "def uabs(y):\n",
+ " if y < 0:\n",
+ " return y * -1\n",
+ " else:\n",
+ " return y\n",
+ " \n",
+ "#Variable Initialization\n",
+ "x = int(raw_input(\"Enter a Negative Value : \"))\n",
+ "\n",
+ "#Function call\n",
+ "x = uabs(x)\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"\\nX = %d\"%(x))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a Negative Value : -5\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "X = 5"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 10.22, Page number: 340"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Square and cube of an entered number. \n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Function Definitions\n",
+ "def input1(): #Since input() is a built in function in python, input1() is used\n",
+ " k = int(raw_input(\"Number : \"))\n",
+ " return k\n",
+ "\n",
+ "def sqr(m):\n",
+ " sys.stdout.write(\"\\nSquare : %d\"%(m*m))\n",
+ " return m\n",
+ "\n",
+ "def cube(m):\n",
+ " return m*m*m\n",
+ "\n",
+ "#Function call and Result\n",
+ "sys.stdout.write(\"\\nCube : %d\"%(cube(sqr(input1()))))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Number : 2\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Square : 4\n",
+ "Cube : 8"
+ ]
+ }
+ ],
+ "prompt_number": 12
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 10.23, Page number: 341"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Assign return value of a function to variable.\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Function Definition\n",
+ "def input1():\n",
+ " k = int(raw_input(\"Enter Value of x = \"))\n",
+ " return k\n",
+ "\n",
+ "#Function call\n",
+ "x = input1()\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"\\nx = %d\"%(x))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Value of x = 5\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "x = 5"
+ ]
+ }
+ ],
+ "prompt_number": 13
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 10.24, Page number: 342"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Addition and subtraction of numbers with return value of function.\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Function Definition\n",
+ "def input1():\n",
+ " k = int(raw_input(\"Enter Value of x = \"))\n",
+ " return k\n",
+ "\n",
+ "def sqr(m):\n",
+ " return pow(m,2)\n",
+ "\n",
+ "#Function call\n",
+ "x = sqr(1 - input1() + 1)\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"\\nSquare = %d\"%(x))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Value of x = 5\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Square = 9"
+ ]
+ }
+ ],
+ "prompt_number": 14
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 10.25, Page number: 343"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Multiplication and division of numbers with return value of function.\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Function Definition\n",
+ "def input1():\n",
+ " k = int(raw_input(\"Enter Value of x = \"))\n",
+ " return k\n",
+ "\n",
+ "def sqr(m):\n",
+ " return pow(m,2)\n",
+ "\n",
+ "#Function call\n",
+ "x = sqr(5 * input1()/2)\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"\\nSquare = %d\"%(x))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Value of x = 5\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Square = 144"
+ ]
+ }
+ ],
+ "prompt_number": 15
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 10.26, Page number: 344"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# ++ operator with return value of function\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Function Definition\n",
+ "def input1():\n",
+ " k = int(raw_input(\"Enter Value of x = \"))\n",
+ " return k\n",
+ "\n",
+ "def sqr(m):\n",
+ " return pow(m,2)\n",
+ "\n",
+ "#Function call\n",
+ "#There is no ++ operator in python. so += operator is used\n",
+ "y = input1()\n",
+ "y += 1\n",
+ "x = sqr(y)\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"\\nSquare = %d\"%(x))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Value of x = 7\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Square = 64"
+ ]
+ }
+ ],
+ "prompt_number": 16
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 10.27, Page number: 345"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Use mod(%) with function\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Function Definition\n",
+ "def j():\n",
+ " x = int(raw_input(\"Enter a Number : \"))\n",
+ " return x\n",
+ " \n",
+ "#Function call & Result\n",
+ "if j() %2 == 0:\n",
+ " sys.stdout.write(\"\\nNumber is Even.\")\n",
+ "else:\n",
+ " sys.stdout.write(\"\\nNumber is Odd.\")"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a Number : 5\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Number is Odd."
+ ]
+ }
+ ],
+ "prompt_number": 17
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 10.28, Page number: 346"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Conditional operator(?) with function.\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Function Definitions\n",
+ "def sqr(x):\n",
+ " sys.stdout.write(\"Square \")\n",
+ " return pow(x,2)\n",
+ "\n",
+ "def cube(x):\n",
+ " sys.stdout.write(\"Cube \")\n",
+ " return pow(x,3)\n",
+ "\n",
+ "def y():\n",
+ " return 10\n",
+ "\n",
+ "#Variable Initialization\n",
+ "x = int(raw_input(\"Enter a Number : \"))\n",
+ "\n",
+ "#Function call\n",
+ "z = sqr(x) if x > y() else cube(x)\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\" = %d\"%(z))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a Number : 5\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Cube = 125"
+ ]
+ }
+ ],
+ "prompt_number": 18
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 10.29, Page number: 346"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#compare two return values of functions\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Function Definitions\n",
+ "def a():\n",
+ " x = int(raw_input(\"Enter a Number a() : \"))\n",
+ " return x\n",
+ "\n",
+ "def b():\n",
+ " x = int(raw_input(\"Enter a Number b() : \"))\n",
+ " return x\n",
+ "\n",
+ "#Function call and Result\n",
+ "if a() == b():\n",
+ " sys.stdout.write(\"\\nValue of a() & b() are equal\")\n",
+ "else:\n",
+ " sys.stdout.write(\"\\nValue of a() & b() are unique\")"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a Number a() : 5\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a Number b() : 5\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Value of a() & b() are equal"
+ ]
+ }
+ ],
+ "prompt_number": 19
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 10.30, Page number: 347"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Evaluate the equation s = sqr(a() + b()) using function\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Function Definitions\n",
+ "def a():\n",
+ " a = int(raw_input(\"Enter value of a : \"))\n",
+ " return a\n",
+ "\n",
+ "def b():\n",
+ " b = int(raw_input(\"Enter value of b : \"))\n",
+ " return b\n",
+ "\n",
+ "def sqr(x):\n",
+ " return x*x\n",
+ "\n",
+ "#Function call\n",
+ "s = sqr(a() + b())\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"\\nSquare of Sum = %d\"%(s))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter value of a : 5\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter value of b : 3\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Square of Sum = 64"
+ ]
+ }
+ ],
+ "prompt_number": 20
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 10.31, Page number: 348"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Evaluate the equation y = x^1+x^2..x^n using function\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Function Definition\n",
+ "def b(m):\n",
+ " m += 1\n",
+ " #sys.stdout.write(\"%d\"%(m))\n",
+ " return m\n",
+ "\n",
+ "#Variable Initialization\n",
+ "x = int(raw_input(\"Values of 'x' and 'n' : \"))\n",
+ "n = int(raw_input(\"Values of 'x' and 'n' : \"))\n",
+ "y = 0\n",
+ "z = 1\n",
+ "m = 0\n",
+ "\n",
+ "while(z <= n):\n",
+ " m = b(m)\n",
+ " y = y + pow(x,m)\n",
+ " sys.stdout.write(\"%d + \"%(y))\n",
+ " z += 1\n",
+ " \n",
+ "if z >= n:\n",
+ " sys.stdout.write(\"\\nValue of y = %d\"%(y))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Values of 'x' and 'n' : 3\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Values of 'x' and 'n' : 3\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "3 + 12 + 39 + \n",
+ "Value of y = 39"
+ ]
+ }
+ ],
+ "prompt_number": 21
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 10.32, Page number: 350"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Call user defined function through if statement\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Function Definition\n",
+ "def a():\n",
+ " a = int(raw_input(\"Enter value of a :\"))\n",
+ " return a\n",
+ "\n",
+ "#Function call and Result\n",
+ "if a()%2 == 0:\n",
+ " sys.stdout.write(\"\\nThe number is even.\")\n",
+ "else:\n",
+ " sys.stdout.write(\"\\nThe number is odd.\")"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter value of a :5\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "The number is odd."
+ ]
+ }
+ ],
+ "prompt_number": 22
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 10.33, Page number: 351"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Call user defined function through switch() statement\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Function Definition\n",
+ "def a():\n",
+ " c = raw_input(\"Enter Your Choice Square(s), Cube(c), Double(d) : \")\n",
+ " c = c.lower()\n",
+ " return c\n",
+ "\n",
+ "#Variable Initialization\n",
+ "x = 5\n",
+ "\n",
+ "#There is no switch() statement in python.\n",
+ "c = a()\n",
+ "if c == 's':\n",
+ " sys.stdout.write(\"\\nSquare of %d is %d\"%(x,pow(x,2)))\n",
+ "else:\n",
+ " if c == 'c':\n",
+ " sys.stdout.write(\"\\nCube of %d is %d\"%(x,pow(x,3)))\n",
+ " else:\n",
+ " if c == 'd':\n",
+ " sys.stdout.write(\"\\nDouble of %d is %d\"%(x,x*2))\n",
+ " else:\n",
+ " sys.stdout.write(\"\\nUnexpected Choice printed as it is : %d\"%(x))\n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Your Choice Square(s), Cube(c), Double(d) : D\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Double of 5 is 10"
+ ]
+ }
+ ],
+ "prompt_number": 23
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 10.34, Page number: 353"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Call function through the for loop\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Function Definition\n",
+ "def plus(k):\n",
+ " if k == 10:\n",
+ " return 0\n",
+ " else:\n",
+ " return k\n",
+ " \n",
+ "#Variable Initialization\n",
+ "m = 1\n",
+ "\n",
+ "#Function call & Result\n",
+ "#in python, for loop iterates through a range of number. so while loop is used instead.\n",
+ "while plus(m) != 0:\n",
+ " sys.stdout.write(\"%3d\"%(m))\n",
+ " m += 1\n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " 1 2 3 4 5 6 7 8 9"
+ ]
+ }
+ ],
+ "prompt_number": 14
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 10.35, Page number: 354"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Call user defined function through while() loop\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Function Definition\n",
+ "def y():\n",
+ " x = int(raw_input(\"Enter a Number : \"))\n",
+ " return x\n",
+ "\n",
+ "#Function call & Result\n",
+ "while y() != 0:\n",
+ " sys.stdout.write(\"Value enter is non-zero\\n\")"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a Number : 5\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Value enter is non-zero\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a Number : 0\n"
+ ]
+ }
+ ],
+ "prompt_number": 25
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 10.36, Page number: 355"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Call user defined function through do while() loop\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Function Definition\n",
+ "def y():\n",
+ " x = int(raw_input(\"Enter a Number : \"))\n",
+ " return x\n",
+ "\n",
+ "#Function call and Result\n",
+ "#There is no do-while loop in python\n",
+ "\n",
+ "while y() != 0:\n",
+ " sys.stdout.write(\"\\nValue entered is non-zero\\n\")"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a Number : 5\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Value entered is non-zero\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a Number : 0\n"
+ ]
+ }
+ ],
+ "prompt_number": 27
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 10.37, Page number: 356"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Initialize an array using functions.\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Function Definition\n",
+ "def c(m):\n",
+ " n = int(raw_input(\"Enter Number d[%d]\"%(m+1)))\n",
+ " return n\n",
+ "\n",
+ "#Variable Initialization\n",
+ "d = [c(i) for i in range(0,5)]\n",
+ "\n",
+ "sys.stdout.write(\"\\nArray d[] elements are : \")\n",
+ "for k in range(0,5):\n",
+ " sys.stdout.write(\"%2d\"%d[k])"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Number d[1]4\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Number d[2]5\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Number d[3]6\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Number d[4]7\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Number d[5]8\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Array d[] elements are : 4 5 6 7 8"
+ ]
+ }
+ ],
+ "prompt_number": 28
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 10.38, Page number: 357"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Pass array element to the function using call by value method.\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Function Definition\n",
+ "def show(m,u):\n",
+ " sys.stdout.write(\"\\nnum[%d] = %d\"%(m+1,u))\n",
+ " \n",
+ "#Variable initialization\n",
+ "num = [12,13,14,15,16,17,18]\n",
+ "\n",
+ "#Function call & Result\n",
+ "for k in range(0,7):\n",
+ " show(k,num[k])"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "num[1] = 12\n",
+ "num[2] = 13\n",
+ "num[3] = 14\n",
+ "num[4] = 15\n",
+ "num[5] = 16\n",
+ "num[6] = 17\n",
+ "num[7] = 18"
+ ]
+ }
+ ],
+ "prompt_number": 20
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 10.39, Page number: 358"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Pass array element to the function using call by reference\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Function Definition\n",
+ "def show(u):\n",
+ " m = 0\n",
+ " sys.stdout.write(\"\\nnum[7] = {\")\n",
+ " while m != 7:\n",
+ " #There is no pointer concept in python\n",
+ " sys.stdout.write(\"%2d,\"%(u[m]))\n",
+ " m += 1\n",
+ " sys.stdout.write(\"\\b}\")\n",
+ " \n",
+ "#Variable Initialization\n",
+ "num = [12,13,14,15,16,17,18]\n",
+ "\n",
+ "#Function call\n",
+ "show(num)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "num[7] = {12,13,14,15,16,17,18,\b}"
+ ]
+ }
+ ],
+ "prompt_number": 22
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 10.40, Page number: 359"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Array elements in reverse order.\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Function Definition\n",
+ "def show(u):\n",
+ " m = 6\n",
+ " while m != -1:\n",
+ " sys.stdout.write(\"\\nnum[%d] = %d\"%(m,u[m]))\n",
+ " m -= 1\n",
+ " \n",
+ "#Variable Initialization\n",
+ "num = [12,13,14,15,16,17,18]\n",
+ "\n",
+ "#Function call\n",
+ "#There is no pointer concept in python\n",
+ "show(num)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "num[6] = 18\n",
+ "num[5] = 17\n",
+ "num[4] = 16\n",
+ "num[3] = 15\n",
+ "num[2] = 14\n",
+ "num[1] = 13\n",
+ "num[0] = 12"
+ ]
+ }
+ ],
+ "prompt_number": 23
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 10.41, Page number: 360"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Copy array elements using user defined function.\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Function Definition\n",
+ "def cpy(p,m):\n",
+ " j = 0\n",
+ " while j != 5:\n",
+ " m[j] = p[j]\n",
+ " j += 1\n",
+ " \n",
+ "#Variable Initialization\n",
+ "a1 = [1,2,3,4,5]\n",
+ "a2 = [0 for i in range(0,5)]\n",
+ "\n",
+ "#Function call\n",
+ "cpy(a1,a2)\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"Source Target\")\n",
+ "for h in range(0,5):\n",
+ " sys.stdout.write(\"\\n%5d\\t%d\"%(a1[h],a2[h]))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Source Target\n",
+ " 1\t1\n",
+ " 2\t2\n",
+ " 3\t3\n",
+ " 4\t4\n",
+ " 5\t5"
+ ]
+ }
+ ],
+ "prompt_number": 24
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 10.42, Page number: 361"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Read array of other function in main()\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Function definition\n",
+ "def arry(k):\n",
+ " b = [1,2,3,4,5]\n",
+ " return b[k]\n",
+ "\n",
+ "#main() function\n",
+ "for k in range(0,5):\n",
+ " sys.stdout.write(\"\\t%d\"%(arry(k)))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\t1\t2\t3\t4\t5"
+ ]
+ }
+ ],
+ "prompt_number": 29
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 10.43, Page number: 361"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Interchange array elements of two arrays using function.\n",
+ "\n",
+ "import sys\n",
+ "global a\n",
+ "global b\n",
+ "\n",
+ "#Function Definitions\n",
+ "def read():\n",
+ " x = int(raw_input(\"\"))\n",
+ " return x\n",
+ "\n",
+ "def change(a,b):\n",
+ "#Since there is no pointer concept in python, exchange is done in the function using global variables.\n",
+ " for x in range(0,5):\n",
+ " a[x] = a[x] + b[x]\n",
+ " b[x] = a[x] - b[x]\n",
+ " a[x] = a[x] - b[x]\n",
+ " \n",
+ "#Variable Initialization\n",
+ "a = [0 for i in range(0,5)]\n",
+ "b = [0 for i in range(0,5)]\n",
+ "\n",
+ "for x in range(0,10):\n",
+ " if x < 5:\n",
+ " a[x] = read()\n",
+ " else:\n",
+ " b[x-5] = read()\n",
+ "\n",
+ "#Swapping and Result\n",
+ "sys.stdout.write(\"\\nArray A & B \")\n",
+ "\n",
+ "for x in range(0,5):\n",
+ " sys.stdout.write(\"\\n%7d%8d\"%(a[x],b[x]))\n",
+ " \n",
+ "#There is no pointer concept in python.\n",
+ "change(a,b)\n",
+ " \n",
+ "sys.stdout.write(\"\\nNow A & B\")\n",
+ "for x in range(0,5):\n",
+ " sys.stdout.write(\"\\n%7d%8d\"%(a[x],b[x]))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "2\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "3\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "4\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "5\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "6\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "7\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "8\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "9\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "0\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Array A & B \n",
+ " 1 6\n",
+ " 2 7\n",
+ " 3 8\n",
+ " 4 9\n",
+ " 5 0\n",
+ "Now A & B\n",
+ " 6 1\n",
+ " 7 2\n",
+ " 8 3\n",
+ " 9 4\n",
+ " 0 5"
+ ]
+ }
+ ],
+ "prompt_number": 30
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 10.44, Page number: 363"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Read array elements declared in different functions using global\n",
+ "#pointer declaration\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Function Definition\n",
+ "def call(j):\n",
+ " m = 0\n",
+ " u = [5,1,6,0,6]\n",
+ " q = u\n",
+ " while m != j:\n",
+ " sys.stdout.write(\"%3d\"%(u[m]))\n",
+ " m += 1\n",
+ " sys.stdout.write(\"\\n\")\n",
+ " \n",
+ "#Variable Initialization\n",
+ "m = 0\n",
+ "k = [3,8,5,2,5]\n",
+ "q = k\n",
+ "\n",
+ "\n",
+ "\n",
+ "#Result\n",
+ "while m != 5:\n",
+ " sys.stdout.write(\"%3d\"%(q[m]))\n",
+ " m += 1\n",
+ "\n",
+ "sys.stdout.write(\"\\n\")\n",
+ "call(5)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " 3 8 5 2 5\n",
+ " 5 1 6 0 6\n"
+ ]
+ }
+ ],
+ "prompt_number": 30
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 10.45, Page number: 364"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Sum of 1 to 5 numbers using recursion\n",
+ "\n",
+ "import sys\n",
+ "global s\n",
+ "\n",
+ "s = 0\n",
+ "#Function definition\n",
+ "def main(x,s):\n",
+ " s = s + x\n",
+ " sys.stdout.write(\"\\nx = %d s = %d\"%(x,s))\n",
+ " if x == 5:\n",
+ " return\n",
+ " x += 1\n",
+ " main(x,s)\n",
+ " \n",
+ "main(1,s)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "x = 1 s = 1\n",
+ "x = 2 s = 3\n",
+ "x = 3 s = 6\n",
+ "x = 4 s = 10\n",
+ "x = 5 s = 15"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 10.46, Page number: 365"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Calculate triangular number of a given number with recursion function method\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Function definition\n",
+ "def tri_num(m):\n",
+ " f = 0\n",
+ " if m == 0:\n",
+ " return f\n",
+ " else:\n",
+ " f = f + m + tri_num(m-1)\n",
+ " return f\n",
+ "\n",
+ "#Variable Initialization\n",
+ "n = int(raw_input(\"Enter a Number : \"))\n",
+ "\n",
+ "#Function call\n",
+ "t = tri_num(n)\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"\\nTriangular number of %d is %d\"%(n,t))\n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a Number : 5\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Triangular number of 5 is 15"
+ ]
+ }
+ ],
+ "prompt_number": 31
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 10.47, Page number: 366"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Display the given string using recursion\n",
+ "\n",
+ "import sys\n",
+ "import os\n",
+ "global x\n",
+ "\n",
+ "#Variable Initialization\n",
+ "x = 0\n",
+ "str1 = \"Have a Good Day\"\n",
+ "\n",
+ "#Function Definition\n",
+ "def main(x):\n",
+ " if x == len(str1): #There is no null terminating character in python string\n",
+ " return\n",
+ " else:\n",
+ " if str1[x] == 'H':\n",
+ " os.system('cls')\n",
+ " sys.stdout.write(\"%c\"%(str1[x]))\n",
+ " else:\n",
+ " sys.stdout.write(\"%c\"%(str1[x]))\n",
+ " x += 1\n",
+ " main(x)\n",
+ " \n",
+ "#Function call\n",
+ "main(x)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Have a Good Day"
+ ]
+ }
+ ],
+ "prompt_number": 47
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 10.48, Page number: 367"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Display the given string 10 times using recursion\n",
+ "\n",
+ "import sys\n",
+ "import os\n",
+ "\n",
+ "#Function definition\n",
+ "def main(x):\n",
+ " sys.stdout.write(\"\\n%.2d] %s\"%(x,str1))\n",
+ " x += 1\n",
+ " if x == 11:\n",
+ " return\n",
+ " else:\n",
+ " if x == 1:\n",
+ " os.system('cls')\n",
+ " main(x)\n",
+ " else:\n",
+ " main(x)\n",
+ " \n",
+ " \n",
+ "#Variable Initialization\n",
+ "x = 0\n",
+ "str1 = \"Have a Good Day\"\n",
+ "\n",
+ "#Function call\n",
+ "main(x)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "00] Have a Good Day\n",
+ "01] Have a Good Day"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "02] Have a Good Day\n",
+ "03] Have a Good Day\n",
+ "04] Have a Good Day\n",
+ "05] Have a Good Day\n",
+ "06] Have a Good Day\n",
+ "07] Have a Good Day\n",
+ "08] Have a Good Day\n",
+ "09] Have a Good Day\n",
+ "10] Have a Good Day"
+ ]
+ }
+ ],
+ "prompt_number": 51
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 10.49, Page number: 368"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Factorial using recursive function.\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Function Definition\n",
+ "def fact(m):\n",
+ " f = 1\n",
+ " if m == 1:\n",
+ " return 1\n",
+ " else:\n",
+ " f = m * fact(m-1)\n",
+ " return f\n",
+ " \n",
+ "#Variable Initialization\n",
+ "x = int(raw_input(\"Enter a Number : \"))\n",
+ "\n",
+ "#Function call\n",
+ "f = fact (x)\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"\\nFactorial of %d is %d\"%(x,f))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a Number : 5\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Factorial of 5 is 120"
+ ]
+ }
+ ],
+ "prompt_number": 32
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 10.50, Page number: 369"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Display address of user defined function\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Function definition\n",
+ "def show():\n",
+ " sys.stdout.write(\"\\nAddress of function show() is : \")\n",
+ " \n",
+ "#Function call\n",
+ "show()\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"%u\"%(id(show)))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Address of function show() is : 95041520"
+ ]
+ }
+ ],
+ "prompt_number": 54
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 10.51, Page number: 369"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Call function using pointer\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Function definition\n",
+ "def show():\n",
+ " sys.stdout.write(\"\\nAddress of function show() is : \")\n",
+ " \n",
+ "#There is no pointer concept in python\n",
+ "p = id(show)\n",
+ "show()\n",
+ "sys.stdout.write(\"%u\"%(id(show)))\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Address of function show() is : 95041200"
+ ]
+ }
+ ],
+ "prompt_number": 55
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 10.52, Page number: 370"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Display the address of library function.\n",
+ "\n",
+ "import sys\n",
+ "import os\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"\\nAddress of printf() is %u\"%(id(sys.stdout.write)))\n",
+ "sys.stdout.write(\"\\nAddress of scanf() is %u\"%(id(sys.stdout.read)))\n",
+ "sys.stdout.write(\"\\nAddress of clrscr() is %u\"%(id(os.system('cls'))))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Address of printf() is 60743848\n",
+ "Address of scanf() is 60743848\n",
+ "Address of clrscr() is 4774132"
+ ]
+ }
+ ],
+ "prompt_number": 57
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 10.53, Page number: 371"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Call main() using pointer to main() function\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Variable Initilization\n",
+ "x = 0\n",
+ "\n",
+ "#Function definition\n",
+ "def main(x):\n",
+ " p = id(main)\n",
+ " x += 1\n",
+ " sys.stdout.write(\"\\nCall %d Address of main() %u\"%(x,id(main)))\n",
+ " if x == 3:\n",
+ " return\n",
+ " main(x)\n",
+ " \n",
+ "#function call\n",
+ "main(x)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Call 1 Address of main() 95040880\n",
+ "Call 2 Address of main() 95040880\n",
+ "Call 3 Address of main() 95040880"
+ ]
+ }
+ ],
+ "prompt_number": 59
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file
diff --git a/Programming_in_C_using_ANSI_C/KamthaneChapter11.ipynb b/Programming_in_C_using_ANSI_C/KamthaneChapter11.ipynb
new file mode 100755
index 00000000..c8fac375
--- /dev/null
+++ b/Programming_in_C_using_ANSI_C/KamthaneChapter11.ipynb
@@ -0,0 +1,446 @@
+{
+ "metadata": {
+ "name": ""
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Chapter 11: Storage Class"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 11.1, Page number: 376"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Working of auto variable\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Functio definitions\n",
+ "def call1():\n",
+ " v = 20\n",
+ " sys.stdout.write(\"\\nV = %d\"%(v))\n",
+ " \n",
+ "def call2():\n",
+ " v = 30\n",
+ " call1()\n",
+ " sys.stdout.write(\"\\nV = %d\"%(v))\n",
+ " \n",
+ "#Variable Initialization\n",
+ "v = 10\n",
+ "\n",
+ "#Function call\n",
+ "call2()\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"\\nV = %d\"%(v))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "V = 20\n",
+ "V = 30\n",
+ "V = 10"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 11.2, Page number: 376"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Working of auto variables in different blocks\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Variable Initialization\n",
+ "x = 10\n",
+ "\n",
+ "sys.stdout.write(\"\\nX = %d\"%(x))\n",
+ "\n",
+ "x = 20\n",
+ "sys.stdout.write(\"\\nX = %d\"%(x))\n",
+ "\n",
+ "x = 10\n",
+ "sys.stdout.write(\"\\nX = %d\"%(x))\n",
+ "\n",
+ "#In python, block of statements are indicated using intendation.\n",
+ "#block of statements can be written for conditional,loop or function statements"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "X = 10\n",
+ "X = 20\n",
+ "X = 10"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 11.3, Page number: 377"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Use same variable in different blocks with different data types\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Variable Initialization & Result\n",
+ "x = 10\n",
+ "sys.stdout.write(\"\\nX = %d\"%(x))\n",
+ "\n",
+ "x = 2.22\n",
+ "sys.stdout.write(\"\\nX = %g\"%(x))\n",
+ "\n",
+ "x = \"Auto Storage Class\"\n",
+ "sys.stdout.write(\"\\nX = %s\"%(x))\n",
+ "\n",
+ "x = 10\n",
+ "sys.stdout.write(\"\\nX = %d\"%(x))\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "X = 10\n",
+ "X = 2.22\n",
+ "X = Auto Storage Class\n",
+ "X = 10"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 11.4, Page number: 378"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Working of external variables\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Function definitions\n",
+ "def call1():\n",
+ " sys.stdout.write(\"\\nIn Call1() V = %d\"%(v))\n",
+ " \n",
+ "def call2():\n",
+ " sys.stdout.write(\"\\nIn call2() V = %d\"%(v))\n",
+ " \n",
+ "#Variable Initialization\n",
+ "global v\n",
+ "v = 10\n",
+ "\n",
+ "call1()\n",
+ "call2()\n",
+ "sys.stdout.write(\"\\nIn main() V = %d\"%(v))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "In Call1() V = 10\n",
+ "In call2() V = 10\n",
+ "In main() V = 10"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 11.5, Page number: 379"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Working of auto and global variables with same name\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Function definitions\n",
+ "def call1():\n",
+ " v = 20\n",
+ " sys.stdout.write(\"\\nIn Call1() V = %d\"%(v))\n",
+ " \n",
+ "def call2():\n",
+ " sys.stdout.write(\"\\nIn call2() V = %d\"%(v))\n",
+ " \n",
+ "#Variable Initialization\n",
+ "global v\n",
+ "v = 10\n",
+ "\n",
+ "#Function call\n",
+ "call1()\n",
+ "call2()\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"\\nIn main() V = %d\"%(v))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "In Call1() V = 20\n",
+ "In call2() V = 10\n",
+ "In main() V = 10"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 11.6, Page number: 380"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Declare external variables using extern keyword\n",
+ "\n",
+ "#Variable Initialization\n",
+ "global m\n",
+ "m = 10\n",
+ "\n",
+ "#There is no extern keyword in python, global is used instead\n",
+ "sys.stdout.write(\"\\nM = %d\"%(m))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "M = 10"
+ ]
+ }
+ ],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 11.7, Page number: 380"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Use of static variable\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Function definition\n",
+ "def print1(m):\n",
+ " sys.stdout.write(\"\\nm = %d\"%(m))\n",
+ " if m == 3:\n",
+ " return\n",
+ " \n",
+ "m = 0\n",
+ "\n",
+ "for i in range(0,3):\n",
+ " m += 1\n",
+ " print1(m)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "m = 1\n",
+ "m = 2\n",
+ "m = 3"
+ ]
+ }
+ ],
+ "prompt_number": 15
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 11.8, Page number: 381"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Difference between variables of auto and static class\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Result\n",
+ "y = 0\n",
+ "sys.stdout.write(\"\\nx = %d & y = %d\"%(x,y))\n",
+ "\n",
+ "#Since x display the junk value which stored already, it differs in different systems"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "x = 10 & y = 0"
+ ]
+ }
+ ],
+ "prompt_number": 16
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 11.9, Page number: 382"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Register class variable\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#There is no register keyword in python\n",
+ "m = 1\n",
+ "\n",
+ "#Result\n",
+ "for m in range(1,5+1):\n",
+ " sys.stdout.write(\"\\t%d\"%(m))\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\t1\t2\t3\t4\t5"
+ ]
+ }
+ ],
+ "prompt_number": 18
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 11.10, Page number: 382"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Register class variable\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#There is no register class variable in python\n",
+ "m = 1\n",
+ "\n",
+ "for m in range(1,6):\n",
+ " sys.stdout.write(\"\\t%d\"%(m))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\t1\t2\t3\t4\t5"
+ ]
+ }
+ ],
+ "prompt_number": 19
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file
diff --git a/Programming_in_C_using_ANSI_C/KamthaneChapter12.ipynb b/Programming_in_C_using_ANSI_C/KamthaneChapter12.ipynb
new file mode 100755
index 00000000..e1acaa2c
--- /dev/null
+++ b/Programming_in_C_using_ANSI_C/KamthaneChapter12.ipynb
@@ -0,0 +1,849 @@
+{
+ "metadata": {
+ "name": ""
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Chapter 12: Preprocessor Directives"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 12.1, Page number: 386"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Area of circle\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Defining the macro\n",
+ "#There is no macro definitions/preprocessors in python\n",
+ "PI = 3.14\n",
+ "\n",
+ "#Variable Initialization\n",
+ "r = float(raw_input(\"Enter radius of circle in cms.\"))\n",
+ "\n",
+ "#Calculation\n",
+ "area = PI * r * r\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"Area of a Circle = %.2f cm2\"%(area))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter radius of circle in cms.7\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Area of a Circle = 153.86 cm2"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 12.2, Page number: 387"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Identifers for 'c' statements and variables\n",
+ "\n",
+ "import sys\n",
+ "import os\n",
+ "\n",
+ "#Macro definitions\n",
+ "#in python, function definitions are used instead of macros\n",
+ "def N():\n",
+ " return 10\n",
+ "\n",
+ "def cls():\n",
+ " os.system('cls')\n",
+ " \n",
+ "def wait():\n",
+ " t = raw_input(\"\")\n",
+ " \n",
+ "def display(s):\n",
+ " sys.stdout.write(s)\n",
+ "\n",
+ "for k in range(1,N()+1):\n",
+ " display(\"%d\\t\"%(k))\n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1\t2\t3\t4\t5\t6\t7\t8\t9\t10\t"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 12.3, Page number: 387"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Macros for logical operators\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Macro definitions\n",
+ "#in python, function definitions are used as macros\n",
+ "\n",
+ "#There is no preprocessors in python\n",
+ "\n",
+ "#Variable Initialization\n",
+ "a = int(raw_input(\"Enter Three Numbers : \"))\n",
+ "b = int(raw_input(\"Enter Three Numbers : \"))\n",
+ "c = int(raw_input(\"Enter Three Numbers : \"))\n",
+ "\n",
+ "if a > b and a > c:\n",
+ " sys.stdout.write(\"%d is the larger number\"%(a))\n",
+ "else:\n",
+ " if b > a and b > c:\n",
+ " sys.stdout.write(\"%d is the larger number\"%(b))\n",
+ " else:\n",
+ " if c > a and c > b:\n",
+ " sys.stdout.write(\"%d is the larger number\"%(c))\n",
+ " else:\n",
+ " if a == b and b == c:\n",
+ " sys.stdout.write(\"\\nNumbers are same.\")"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Three Numbers : 5\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Three Numbers : 4\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Three Numbers : 7\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "7 is the larger number"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 12.4, Page number: 388"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Identifier for displaying double and triple of a number.\n",
+ "\n",
+ "#Macro definitions\n",
+ "#Function definitions are used as macros in python\n",
+ "\n",
+ "def DOUBLE(a):\n",
+ " return a*2\n",
+ "\n",
+ "def TRIPLE(a):\n",
+ " return a*3\n",
+ "\n",
+ "#Variable initialization\n",
+ "a = 1\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"\\nSINGLE\\tDOUBLE\\tTRIPLE\")\n",
+ "\n",
+ "for a in range(1,5+1):\n",
+ " sys.stdout.write(\"\\n%d\\t%d\\t%d\"%(a,DOUBLE(a),TRIPLE(a)))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "SINGLE\tDOUBLE\tTRIPLE\n",
+ "1\t2\t3\n",
+ "2\t4\t6\n",
+ "3\t6\t9\n",
+ "4\t8\t12\n",
+ "5\t10\t15"
+ ]
+ }
+ ],
+ "prompt_number": 13
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 12.5, Page number: 389"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Undefine a macro\n",
+ "\n",
+ "#There is no preprocessor directives in python\n",
+ "\n",
+ "def wait():\n",
+ " return (raw_input(\"\"))\n",
+ "\n",
+ "#Result\n",
+ "for k in range(1,5+1):\n",
+ " sys.stdout.write(\"%d\\t\"%(k))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1\t2\t3\t4\t5\t"
+ ]
+ }
+ ],
+ "prompt_number": 14
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 12.6, Page number: 389"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Stringizing operation\n",
+ "\n",
+ "#Macro definition\n",
+ "#in python, function definition can be used as macro\n",
+ "\n",
+ "def say(m):\n",
+ " sys.stdout.write(m)\n",
+ " \n",
+ "#Function call\n",
+ "say(\"Hello\")"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Hello"
+ ]
+ }
+ ],
+ "prompt_number": 16
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 12.7, Page number: 390"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Stringizing operation and macro arguments\n",
+ "\n",
+ "#There is no macro in python, function definitions can be used as macros\n",
+ "\n",
+ "def DOUBLE(x):\n",
+ " sys.stdout.write(\"Double of m = %d\\n\"%(x*2))\n",
+ " \n",
+ "def TRIPLE(x):\n",
+ " sys.stdout.write(\"Triple of m = %d\\n\"%(x*3))\n",
+ " \n",
+ "#Variable Initialization\n",
+ "m = int(raw_input(\"Enter a number : \"))\n",
+ "\n",
+ "#Function call & Result\n",
+ "DOUBLE(m)\n",
+ "TRIPLE(m)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a number : 5\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Double of m = 10\n",
+ "Triple of m = 15\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 12.8, Page number: 390"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Larger of two numbers using macro with arguments.\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Macro definition //function definition itself is used as macro\n",
+ "def MAX(x,y):\n",
+ " if x > y:\n",
+ " c = x\n",
+ " else:\n",
+ " c = y\n",
+ " return c\n",
+ "\n",
+ "#Variable Initialization\n",
+ "x = 5\n",
+ "y = 8\n",
+ "\n",
+ "#Function call\n",
+ "c = MAX(x,y)\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"\\nLarger of two numbers = %d\"%(c))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Larger of two numbers = 8"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 12.9, Page number: 391"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Function defined in \"udf.c\" file.\n",
+ "\n",
+ "import sys\n",
+ "#since ipython notebook does not support this function, function is used\n",
+ "#in python, the udf.c file can be written and saved in udf.py and import command\n",
+ "#can be used to include that file in this program\n",
+ "\n",
+ "def display():\n",
+ " sys.stdout.write(\"\\nFunction Called\")\n",
+ " \n",
+ "#Function call\n",
+ "display()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Function Called"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 12.10, Page number: 392"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Conditional compilation \n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#There is no preprocessor directives in python\n",
+ "\n",
+ "LINE = 1\n",
+ "\n",
+ "if LINE:\n",
+ " sys.stdout.write(\"This is line number one\")\n",
+ "else:\n",
+ " sys.stdout.write(\"This is line number two\")\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "This is line number one"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 12.11, Page number: 393"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Conditional compilation \n",
+ "import sys\n",
+ "\n",
+ "#There is no preprocessor directives in python\n",
+ "def E():\n",
+ " return 1\n",
+ "\n",
+ "#Function call\n",
+ "if E():\n",
+ " a = 2\n",
+ " b = 3\n",
+ " sys.stdout.write(\"A = %d & B = %d\"%(a,b))\n",
+ "else:\n",
+ " c = 2\n",
+ " d = 3\n",
+ " sys.stdout.write(\"C = %d & D = %d\"%(c,d))\n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "A = 2 & B = 3"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 12.12, Page number: 394"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Conditional compilation directives \n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#There is no preprocessor directives in python\n",
+ "T = 8\n",
+ "\n",
+ "#Result\n",
+ "if T == 0:\n",
+ " sys.stdout.write(\"\\nMacro is not defined.\")\n",
+ "else:\n",
+ " sys.stdout.write(\"\\nMacro is defined\")"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Macro is defined"
+ ]
+ }
+ ],
+ "prompt_number": 12
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 12.13, Page number: 394"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#User defined error message \n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#There is no preprocessor directives in python\n",
+ "B = 1\n",
+ "A = 0\n",
+ "\n",
+ "#Result\n",
+ "if A == 0:\n",
+ " sys.stdout.write(\"MACRO A IS NOT DEFINED.\")\n",
+ "else:\n",
+ " sys.stdout.write(\"Macro found.\")"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "MACRO A IS NOT DEFINED."
+ ]
+ }
+ ],
+ "prompt_number": 14
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 12.14, Page number: 397"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Set off certain errors \n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#There is no preprocessor directives in python\n",
+ "x = 2\n",
+ "y = 1\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"\\ny = %d\"%(y))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "y = 1"
+ ]
+ }
+ ],
+ "prompt_number": 15
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 12.15, Page number: 398"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Predefined macros \n",
+ "\n",
+ "import sys\n",
+ "import datetime\n",
+ "\n",
+ "#Variable Initialization\n",
+ "FILE = 'PRE_MA~1.C'\n",
+ "LINE = 10\n",
+ "STDC = 1\n",
+ "\n",
+ "#Date/time can be set using datetime.date(2001,5,12)\n",
+ "#Result\n",
+ "sys.stdout.write(\"\\nDATE : %s\"%('Dec 05 2001')) \n",
+ "sys.stdout.write(\"\\nTIME : %s\"%(datetime.time(21,7,39)))\n",
+ "sys.stdout.write(\"\\nFILE NAME : %s\"%(FILE))\n",
+ "sys.stdout.write(\"\\nLINE NO : %d\"%(LINE))\n",
+ "sys.stdout.write(\"\\n_STDC_ : %x\"%(STDC))\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "DATE : Dec 05 2001\n",
+ "TIME : 21:07:39\n",
+ "FILE NAME : PRE_MA~1.C\n",
+ "LINE NO : 10\n",
+ "_STDC_ : 1"
+ ]
+ }
+ ],
+ "prompt_number": 31
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 12.16, Page number: 399"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Display the name of memory model that is currently in use.\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#There is no formal memory model and preprocessor directives in python\n",
+ "\n",
+ "TINY = 0\n",
+ "SMALL = 0\n",
+ "MEDIUM = 0\n",
+ "COMPACT = 0\n",
+ "LARGE = 1\n",
+ "HUGE = 0\n",
+ "\n",
+ "#Result\n",
+ "if TINY:\n",
+ " sys.stdout.write(\"\\nTINY %d\"%(TINY))\n",
+ "else:\n",
+ " if SMALL:\n",
+ " sys.stdout.write(\"\\nSMALL %d\"%(SMALL))\n",
+ " else:\n",
+ " if MEDIUM:\n",
+ " sys.stdout.write(\"\\nMEDIUM %d\"%(MEDIUM))\n",
+ " else:\n",
+ " if COMPACT:\n",
+ " sys.stdout.write(\"\\nCOMPACT %d\"%(COMPACT))\n",
+ " else:\n",
+ " if LARGE:\n",
+ " sys.stdout.write(\"\\nLARGE %d\"%(LARGE))\n",
+ " else:\n",
+ " sys.stdout.write(\"\\nHUGE %d\"%(HUGE))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "LARGE 1"
+ ]
+ }
+ ],
+ "prompt_number": 32
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 12.17, Page number: 400"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Macro expansions\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#There is no preprocessor directives in python\n",
+ "ch = raw_input(\"Input a Text : \")\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"Text Input : %s\"%(ch))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Input a Text : Programming\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Text Input : Programming"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 12.18, Page number: 401"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Predefined macros.\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Variable Initialization\n",
+ "d = raw_input(\"Enter any character : \")\n",
+ "\n",
+ "#Result\n",
+ "f = d.isalpha()\n",
+ "\n",
+ "if f != 0:\n",
+ " sys.stdout.write(\"\\n%c is a letter in\"%(d))\n",
+ " f = d.isupper()\n",
+ " if f != 0:\n",
+ " sys.stdout.write(\" Capital case\")\n",
+ " else:\n",
+ " sys.stdout.write(\" Small case\")\n",
+ "else:\n",
+ " f = d.isdigit()\n",
+ " if f != 0:\n",
+ " sys.stdout.write(\"\\n%c is a digit\"%(d))\n",
+ " else:\n",
+ " f = d.ispunct()\n",
+ " if f != 0:\n",
+ " sys.stdout.write(\"\\n%c is a punctuation symbol\"%(d))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any character : C\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "C is a letter in Capital case"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file
diff --git a/Programming_in_C_using_ANSI_C/KamthaneChapter13.ipynb b/Programming_in_C_using_ANSI_C/KamthaneChapter13.ipynb
new file mode 100755
index 00000000..f9222003
--- /dev/null
+++ b/Programming_in_C_using_ANSI_C/KamthaneChapter13.ipynb
@@ -0,0 +1,2367 @@
+{
+ "metadata": {
+ "name": ""
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Chapter 13: Structure and Union"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 13.1, Page number: 409"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Display size of structure elements\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Class is used instead of structure in python\n",
+ "class book1:\n",
+ " book = ''\n",
+ " pages = 0\n",
+ " price = 0.00\n",
+ "\n",
+ "#Python variables uses value tagged methods for storing the values\n",
+ "\n",
+ "#Class variable declaration\n",
+ "bk1 = book1()\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"\\nSize of Structure Elements\")\n",
+ "sys.stdout.write(\"\\nBook : %d\"%(sys.getsizeof(bk1.book)))\n",
+ "sys.stdout.write(\"\\nPages : %d\"%(sys.getsizeof(bk1.pages)))\n",
+ "sys.stdout.write(\"\\nPrice : %d\"%(sys.getsizeof(bk1.price)))\n",
+ "sys.stdout.write(\"\\nTotal Bytes : %d\"%(sys.getsizeof(bk1)))\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Size of Structure Elements\n",
+ "Book : 21\n",
+ "Pages : 12\n",
+ "Price : 16\n",
+ "Total Bytes : 36"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 13.2, Page number: 410"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Define a structure and initialize its member variables\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Class declaration\n",
+ "class book1:\n",
+ " book = ''\n",
+ " pages = 0\n",
+ " price = 0.0\n",
+ " \n",
+ "#Class variable declaration and Initialization\n",
+ "bk1 = book1()\n",
+ "bk1.book = \"C++\"\n",
+ "bk1.pages = 300\n",
+ "bk1.price = 285\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"\\nBook Name : %s\"%(bk1.book))\n",
+ "sys.stdout.write(\"\\nNo. of Pages : %d\"%(bk1.pages))\n",
+ "sys.stdout.write(\"\\nBook Price : %d\"%(bk1.price))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Book Name : C++\n",
+ "No. of Pages : 300\n",
+ "Book Price : 285"
+ ]
+ }
+ ],
+ "prompt_number": 16
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 13.3, Page number: 411"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Copy structure elements from one object to another object\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Class declaration\n",
+ "class disk:\n",
+ " co = ''\n",
+ " type1 = 0.0\n",
+ " price = 0\n",
+ " \n",
+ "#Class variable declaration\n",
+ "d1 = disk()\n",
+ "d2 = disk()\n",
+ "d3 = disk()\n",
+ "\n",
+ "#Class variable initialization\n",
+ "d1.co = \"SONY\"\n",
+ "d1.type1 = 1.44\n",
+ "d1.price = 20\n",
+ "\n",
+ "#Copying\n",
+ "d2.co = d1.co\n",
+ "d2.type1 = d1.type1\n",
+ "d2.price = d1.price\n",
+ "\n",
+ "d3 = d2\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"\\n%s %g %d\"%(d1.co,d1.type1,d1.price))\n",
+ "sys.stdout.write(\"\\n%s %g %d\"%(d2.co,d2.type1,d2.price))\n",
+ "sys.stdout.write(\"\\n%s %g %d\"%(d3.co,d3.type1,d3.price))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "SONY 1.44 20\n",
+ "SONY 1.44 20\n",
+ "SONY 1.44 20"
+ ]
+ }
+ ],
+ "prompt_number": 13
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 13.4, Page number: 412"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Read values and assign them to structure variables\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Class declaration\n",
+ "class book1:\n",
+ " book = ''\n",
+ " pages = 0\n",
+ " price = 0.0\n",
+ " \n",
+ "#Class variable declaration\n",
+ "bk1 = book1()\n",
+ "\n",
+ "#Variable initialization\n",
+ "bk1.book = raw_input(\"Enter Book name,pages,price : \")\n",
+ "bk1.pages = int(raw_input(\"Enter Book name,pages,price : \"))\n",
+ "bk1.price = float(raw_input(\"Enter Book name, pages,price : \"))\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"\\nBook Name : %s\"%(bk1.book))\n",
+ "sys.stdout.write(\"\\nNo. of Pages : %d\"%(bk1.pages))\n",
+ "sys.stdout.write(\"\\nBook Price : %d\"%(bk1.price))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Book name,pages,price : C\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Book name,pages,price : 500\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Book name, pages,price : 450\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Book Name : C\n",
+ "No. of Pages : 500\n",
+ "Book Price : 450"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 13.5, Page number: 414"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Read and display car details\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Class declaration\n",
+ "class time:\n",
+ " second = 0\n",
+ " minute = 0\n",
+ " hour = 0\n",
+ " \n",
+ "class t:\n",
+ " carno = 0\n",
+ " st = time()\n",
+ " rt = time()\n",
+ " \n",
+ "#Class variable declaration\n",
+ "r1 = t()\n",
+ "\n",
+ "#Variable Initialization\n",
+ "r1.carno = int(raw_input(\"Car No., Starting Time, Reaching Time :\"))\n",
+ "r1.st.hour = int(raw_input(\"Car No., Starting Time, Reaching Time :\"))\n",
+ "r1.st.minute = int(raw_input(\"Car No., Starting Time, Reaching Time :\"))\n",
+ "r1.st.second = int(raw_input(\"Car No., Starting Time, Reaching Time :\"))\n",
+ "r1.rt.hour = int(raw_input(\"Car No., Starting Time, Reaching Time :\"))\n",
+ "r1.rt.minute = int(raw_input(\"Car No., Starting Time, Reaching Time :\"))\n",
+ "r1.rt.second = int(raw_input(\"Car No., Starting Time, Reaching Time :\"))\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"\\nCar No. \\tStarting Time \\tReaching Time\\n\")\n",
+ "sys.stdout.write(\"%d\\t\"%(r1.carno))\n",
+ "sys.stdout.write(\"\\t%d:%d:%d\\t\\t\"%(r1.st.hour,r1.st.minute,r1.st.second))\n",
+ "sys.stdout.write(\"%d:%d:%d\"%(r1.rt.hour,r1.rt.minute,r1.rt.second))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Car No., Starting Time, Reaching Time :125\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Car No., Starting Time, Reaching Time :2\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Car No., Starting Time, Reaching Time :50\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Car No., Starting Time, Reaching Time :30\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Car No., Starting Time, Reaching Time :3\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Car No., Starting Time, Reaching Time :50\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Car No., Starting Time, Reaching Time :25\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Car No. \tStarting Time \tReaching Time\n",
+ "125\t\t2:50:30\t\t3:50:25"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 13.6, Page number: 415"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Read and display the details of a person\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Class declaration\n",
+ "class name:\n",
+ " first = ''\n",
+ " second = ''\n",
+ " last = ''\n",
+ " \n",
+ "class b_date:\n",
+ " day = 0\n",
+ " month = 0\n",
+ " year = 0\n",
+ " \n",
+ "class data:\n",
+ " nm = name()\n",
+ " bt = b_date()\n",
+ " \n",
+ "#Class variable declaration\n",
+ "r1 = data()\n",
+ "\n",
+ "#Variable Initialization\n",
+ "r1.nm.first = raw_input(\"Enter Name (First/Second/Last)\")\n",
+ "r1.nm.second = raw_input(\"Enter Name (First/Second/Last)\")\n",
+ "r1.nm.last = raw_input(\"Enter Name (First/Second/Last)\")\n",
+ "r1.bt.day = int(raw_input(\"Enter Birth Date Day/Month/Year\"))\n",
+ "r1.bt.month = int(raw_input(\"Enter Birth Date Day/Month/Year\"))\n",
+ "r1.bt.year = int(raw_input(\"Enter Birth Date Day/Month/Year\"))\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"Name : %s %s %s\\n\"%(r1.nm.first,r1.nm.second,r1.nm.last))\n",
+ "sys.stdout.write(\"Birth Date : %d.%d.%d\"%(r1.bt.day,r1.bt.month,r1.bt.year))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Name (First/Second/Last)Ram\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Name (First/Second/Last)Sham\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Name (First/Second/Last)Pande\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Birth Date Day/Month/Year12\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Birth Date Day/Month/Year12\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Birth Date Day/Month/Year1980\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Name : Ram Sham Pande\n",
+ "Birth Date : 12.12.1980"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 13.7, Page number: 416"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Create array of structure objects\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Class declaration\n",
+ "class time:\n",
+ " second = 0\n",
+ " minute = 0\n",
+ " hour = 0\n",
+ " \n",
+ "class t:\n",
+ " carno = 0\n",
+ " st = time()\n",
+ " rt = time()\n",
+ " \n",
+ "#Class variable declaration\n",
+ "r1 = [t() for i in range(0,3)]\n",
+ "\n",
+ "#Variable Initialization\n",
+ "for k in range(0,3):\n",
+ " r1[k].carno = int(raw_input(\"Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)\"))\n",
+ " r1[k].st.hour = int(raw_input(\"Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)\"))\n",
+ " r1[k].st.minute = int(raw_input(\"Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)\"))\n",
+ " r1[k].st.second = int(raw_input(\"Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)\"))\n",
+ " r1[k].rt.hour = int(raw_input(\"Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)\"))\n",
+ " r1[k].rt.minute = int(raw_input(\"Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)\"))\n",
+ " r1[k].rt.second = int(raw_input(\"Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)\"))\n",
+ " \n",
+ "#Result\n",
+ "sys.stdout.write(\"\\nCar No.\\t\\tStarting Time\\tReaching Time\")\n",
+ "for k in range(0,3):\n",
+ " sys.stdout.write(\"\\n%d\\t\"%(r1[k].carno))\n",
+ " sys.stdout.write(\"\\t%d:%d:%d\\t\"%(r1[k].st.hour,r1[k].st.minute,r1[k].st.second))\n",
+ " sys.stdout.write(\"\\t%d:%d:%d\\t\"%(r1[k].rt.hour,r1[k].rt.minute,r1[k].rt.second))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)120\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)2\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)20\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)25\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)3\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)25\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)58\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)121\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)3\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)25\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)40\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)4\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)40\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)25\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)122\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)4\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)30\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)52\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)5\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)40\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)10\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Car No.\t\tStarting Time\tReaching Time\n",
+ "120\t\t4:30:52\t\t5:40:10\t\n",
+ "121\t\t4:30:52\t\t5:40:10\t\n",
+ "122\t\t4:30:52\t\t5:40:10\t"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 13.8, Page number: 417"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Read and display student details\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Class declaration\n",
+ "class stud:\n",
+ " name = ''\n",
+ " rollno = 0\n",
+ " grade = ''\n",
+ " \n",
+ "#Class variable declaration\n",
+ "st = [stud() for i in range(0,3)]\n",
+ "\n",
+ "#Variable Initialization\n",
+ "k = 0 \n",
+ "while k < 3:\n",
+ " st[k].name = raw_input(\"Name :\")\n",
+ " st[k].rollno = int(raw_input(\"Roll No. :\"))\n",
+ " st[k].grade = raw_input(\"Grade : \")\n",
+ " k += 1\n",
+ " \n",
+ "#Result\n",
+ "k = 0\n",
+ "sys.stdout.write(\"\\nName \\tRollno \\tGrade\\n\")\n",
+ "while k < 3:\n",
+ " sys.stdout.write(\"\\n%s\\t%d\\t%s\"%(st[k].name,st[k].rollno,st[k].grade))\n",
+ " k += 1\n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Name :Suresh\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Roll No. :125\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Grade : A\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Name :Mahesh\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Roll No. :126\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Grade : B\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Name :Rajesh\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Roll No. :127\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Grade : A\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Name \tRollno \tGrade\n",
+ "\n",
+ "Suresh\t125\tA\n",
+ "Mahesh\t126\tB\n",
+ "Rajesh\t127\tA"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 13.9, Page number: 419"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Pointer to structure \n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Class declaration\n",
+ "class book:\n",
+ " name = ''\n",
+ " author = ''\n",
+ " pages = 0\n",
+ " \n",
+ "#Class variable declaration\n",
+ "b1 = book()\n",
+ "\n",
+ "#Variable Initialization\n",
+ "b1.name = \"JAVA COMPLETE REFERENCE\"\n",
+ "b1.author = \"P.NAUGHTON\"\n",
+ "b1.pages = 886\n",
+ "\n",
+ "#There is no pointer concept in python\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"\\n%s by %s of %d pages\"%(b1.name,b1.author,b1.pages))\n",
+ "sys.stdout.write(\"\\n%s by %s of %d pages\"%(b1.name,b1.author,b1.pages))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "JAVA COMPLETE REFERENCE by P.NAUGHTON of 886 pages\n",
+ "JAVA COMPLETE REFERENCE by P.NAUGHTON of 886 pages"
+ ]
+ }
+ ],
+ "prompt_number": 27
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 13.10, Page number: 420"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Pointer as members of structure \n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Class declaration\n",
+ "class boy:\n",
+ " name = ''\n",
+ " age = 0\n",
+ " height = 0.0\n",
+ " \n",
+ "#Class variable declaration\n",
+ "sp = boy()\n",
+ "\n",
+ "#Variable Initialization\n",
+ "sp.name = \"Mahesh\"\n",
+ "sp.age = 20\n",
+ "sp.height = 5.40\n",
+ "\n",
+ "#There is no pointer concept in python\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"\\nName = %s\"%(sp.name))\n",
+ "sys.stdout.write(\"\\nAge = %s\"%(sp.age))\n",
+ "sys.stdout.write(\"\\nHeight = %s\"%(sp.height))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Name = Mahesh\n",
+ "Age = 20\n",
+ "Height = 5.4"
+ ]
+ }
+ ],
+ "prompt_number": 28
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 13.11, Page number: 421"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Pointer as members of structure \n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Class declaration\n",
+ "class boy:\n",
+ " name = ''\n",
+ " age = 0\n",
+ " height = 0.0\n",
+ " \n",
+ "#Class variable declaration\n",
+ "b = boy()\n",
+ "\n",
+ "#Variable Initialization\n",
+ "nm = \"Somesh\"\n",
+ "ag = 20\n",
+ "ht = 5.40\n",
+ "\n",
+ "#There is no pointer concept in python\n",
+ "b.name = nm\n",
+ "b.age = ag\n",
+ "b.height = ht\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"\\nName : %s\"%(b.name))\n",
+ "sys.stdout.write(\"\\nAge = %d\"%(b.age))\n",
+ "sys.stdout.write(\"\\nHeight = %.2g\"%(b.height))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Name : Somesh\n",
+ "Age = 20\n",
+ "Height = 5.4"
+ ]
+ }
+ ],
+ "prompt_number": 30
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 13.12, Page number: 422"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Display the contents of the structure using ordinary pointer\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Class declaration\n",
+ "class num:\n",
+ " a = 0\n",
+ " b = 0\n",
+ " c = 0\n",
+ "\n",
+ "#Class variable declaration\n",
+ "d = num()\n",
+ "\n",
+ "#Variable Initialization\n",
+ "d.a = 2\n",
+ "d.b = 3\n",
+ "d.c = 4\n",
+ "\n",
+ "#There is no pointer concept in python\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"\\na = %d\"%(d.a))\n",
+ "sys.stdout.write(\"\\nb = %d\"%(d.b))\n",
+ "sys.stdout.write(\"\\nc = %d\"%(d.c))\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "a = 2\n",
+ "b = 3\n",
+ "c = 4"
+ ]
+ }
+ ],
+ "prompt_number": 32
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 13.13, Page number: 423"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Passing address of structure variable\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Class declaration\n",
+ "class book:\n",
+ " name = ''\n",
+ " author = ''\n",
+ " pages = 0\n",
+ " \n",
+ "#Class variable declaration and initialization\n",
+ "b1 = book()\n",
+ "b1.name = \"JAVA COMPLETE REFERENCE\"\n",
+ "b1.author = \"P.NAUGHTON\"\n",
+ "b1.pages = 886\n",
+ "\n",
+ "#Function definition\n",
+ "def show(b1):\n",
+ " sys.stdout.write(\"\\n%s by %s of %d pages\"%(b1.name,b1.author,b1.pages))\n",
+ "\n",
+ "#Function call\n",
+ "show(b1)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "JAVA COMPLETE REFERENCE by P.NAUGHTON of 886 pages"
+ ]
+ }
+ ],
+ "prompt_number": 34
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 13.14, Page number: 424"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Passing structure elements to function\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Class declaration\n",
+ "class boy:\n",
+ " name = ''\n",
+ " age = 0\n",
+ " wt = 0\n",
+ " \n",
+ "#Class variable declaration and initialization\n",
+ "b1 = boy()\n",
+ "b1.name = \"Amit\"\n",
+ "b1.age = 20\n",
+ "b1.wt = 25\n",
+ "\n",
+ "#Function definition\n",
+ "def print1(s,t,n):\n",
+ " sys.stdout.write(\"\\n%s %d %d\"%(s,t,n))\n",
+ " \n",
+ "#Function call\n",
+ "print1(b1.name,b1.age,b1.wt)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Amit 20 25"
+ ]
+ }
+ ],
+ "prompt_number": 36
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 13.15, Page number: 425"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Pass entire structure to user defined function\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Class declaration\n",
+ "class boy:\n",
+ " name = ''\n",
+ " age = 0\n",
+ " wt = 0\n",
+ " \n",
+ "#Class variable declaration and initialization\n",
+ "b1 = boy()\n",
+ "b1.name = \"Amit\"\n",
+ "b1.age = 20\n",
+ "b1.wt = 25\n",
+ "\n",
+ "#Function definition\n",
+ "def print1(b):\n",
+ " sys.stdout.write(\"\\n%s %d %d\"%(b.name,b.age,b1.wt))\n",
+ " \n",
+ "#Function call\n",
+ "print1(b1)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Amit 20 25"
+ ]
+ }
+ ],
+ "prompt_number": 37
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 13.16, Page number: 426"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#User defined data type \n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#There is no preprocessor directive in python\n",
+ "H = 60\n",
+ "\n",
+ "#There is no typedef function in python and no separate variable declaration is needed\n",
+ "hrs = int(raw_input(\"Enter Hours :\"))\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"\\nMinutes = %d\"%(hrs*H))\n",
+ "sys.stdout.write(\"\\nSeconds = %d\"%(hrs*H*H))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Hours :2\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Minutes = 120\n",
+ "Seconds = 7200"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 13.17, Page number: 426"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#String data type\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#There is no typedef function in python\n",
+ "a = \" Hello \"\n",
+ "b = raw_input(\"Enter Your Name : \")\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"%s %s\"%(a,b))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Your Name : KAMAL\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Hello KAMAL"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 13.18, Page number: 427"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Create user defined data type from structure\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Class declaration\n",
+ "#There is no typedef function in python\n",
+ "class struct:\n",
+ " name = ''\n",
+ " sex = ''\n",
+ " acno = 0\n",
+ " \n",
+ "#class variable declaration and initialization\n",
+ "employee = struct()\n",
+ "employee.name = \"Sanjay\"\n",
+ "employee.sex = \"M\"\n",
+ "employee.acno = 125\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"\\nName\\tSex\\tA/c No.\\n\")\n",
+ "sys.stdout.write(\"%s\\t\"%(employee.name))\n",
+ "sys.stdout.write(\"%s\\t\"%(employee.sex))\n",
+ "sys.stdout.write(\"%s\\n\"%(employee.acno))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Name\tSex\tA/c No.\n",
+ "Sanjay\tM\t125\n"
+ ]
+ }
+ ],
+ "prompt_number": 44
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 13.19, Page number: 427"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Create user defined data type from structure\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Class declaration\n",
+ "class struct:\n",
+ " name = ''\n",
+ " sex = ''\n",
+ " acno = 0\n",
+ " \n",
+ "#class Variable declaration and initialization\n",
+ "employee = [struct() for i in range(0,2)]\n",
+ "\n",
+ "for k in range(0,2):\n",
+ " employee[k].name = raw_input(\"Name of the Employee :\")\n",
+ " employee[k].sex = raw_input(\"Sex\")\n",
+ " employee[k].acno = raw_input(\"A/c No.\")\n",
+ " \n",
+ "#Result\n",
+ "sys.stdout.write(\"\\nName\\tSex\\tA/c No.\\n\")\n",
+ "for k in range(0,2):\n",
+ " sys.stdout.write(\"%s\\t\"%(employee[k].name))\n",
+ " sys.stdout.write(\"%s\\t\"%(employee[k].sex))\n",
+ " sys.stdout.write(\"%s\\n\"%(employee[k].acno))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Name of the Employee :AJAY\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "SexM\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "A/c No.122\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Name of the Employee :ANITA\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "SexF\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "A/c No.124\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Name\tSex\tA/c No.\n",
+ "AJAY\tM\t122\n",
+ "ANITA\tF\t124\n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 13.20, Page number: 429"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Structure containing the details of the employees\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#There is no typedef function in python\n",
+ "\n",
+ "#Class definition\n",
+ "class struct:\n",
+ " first = ''\n",
+ " middle = ''\n",
+ " last = ''\n",
+ " city = ''\n",
+ " pincode = 0\n",
+ " \n",
+ "#Class variable declaration and initialization\n",
+ "person = [struct() for i in range(0,2)]\n",
+ "\n",
+ "for j in range(0,2):\n",
+ " person[j].first = raw_input(\"First Name :\")\n",
+ " person[j].middle = raw_input(\"Middle Name : \")\n",
+ " person[j].last = raw_input(\"Last Name : \")\n",
+ " person[j].city = raw_input(\"City & Pincode\")\n",
+ " person[j].pincode = int(raw_input(\"City & Pincode\"))\n",
+ " \n",
+ "#Result\n",
+ "for j in range(0,2):\n",
+ " sys.stdout.write(\"\\nRecord No : %d\"%(j+1))\n",
+ " sys.stdout.write(\"\\nFirst Name : %s\"%(person[j].first))\n",
+ " sys.stdout.write(\"\\nMiddle Name : %s\"%(person[j].middle))\n",
+ " sys.stdout.write(\"\\nLast Name : %s\"%(person[j].last))\n",
+ " sys.stdout.write(\"\\nCity & Pincode : %s - %d\\n\"%(person[j].city,person[j].pincode))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "First Name :Jay\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Middle Name : Mohan\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Last Name : Deshmukh\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "City & PincodeNanded\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "City & Pincode431602\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "First Name :Vijay\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Middle Name : Kamal\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Last Name : Nandedkar\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "City & PincodeNanded\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "City & Pincode431602\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Record No : 1\n",
+ "First Name : Jay\n",
+ "Middle Name : Mohan\n",
+ "Last Name : Deshmukh\n",
+ "City & Pincode : Nanded - 431602\n",
+ "\n",
+ "Record No : 2\n",
+ "First Name : Vijay\n",
+ "Middle Name : Kamal\n",
+ "Last Name : Nandedkar\n",
+ "City & Pincode : Nanded - 431602\n"
+ ]
+ }
+ ],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 13.21, Page number: 431"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Information of vehicles\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Variable Initialization\n",
+ "PETROL = 1\n",
+ "DISEL = 2\n",
+ "TWO_WH = 3\n",
+ "FOUR_WH = 4\n",
+ "OLD = 5\n",
+ "NEW = 6\n",
+ "\n",
+ "#Class declaration\n",
+ "class vehicle:\n",
+ " type1 = 3\n",
+ " fuel = 2\n",
+ " model = 3\n",
+ " \n",
+ "#Class variable declaration and initialization\n",
+ "v = vehicle()\n",
+ "v.type1 = FOUR_WH\n",
+ "v.fuel = DISEL\n",
+ "v.model = OLD\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"\\nType of Vehicle : %d\"%(v.type1))\n",
+ "sys.stdout.write(\"\\nFuel : %d\"%(v.fuel))\n",
+ "sys.stdout.write(\"\\nModel : %d\"%(v.model))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Type of Vehicle : 4\n",
+ "Fuel : 2\n",
+ "Model : 5"
+ ]
+ }
+ ],
+ "prompt_number": 52
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 13.22, Page number: 432"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Display the examination result of the student \n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Variable Initialization\n",
+ "#There is no preprocessor directives in python\n",
+ "PASS = 1\n",
+ "FAIL = 0\n",
+ "A = 0\n",
+ "B = 1\n",
+ "C = 2\n",
+ "\n",
+ "#Class declaration\n",
+ "class student:\n",
+ " name = ''\n",
+ " result = 1\n",
+ " grade = 2\n",
+ " \n",
+ "#Class variable declaration and initialization\n",
+ "v = student()\n",
+ "\n",
+ "v.name = \"Sachin\"\n",
+ "v.result = PASS\n",
+ "v.grade = C\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"\\nName : %s\"%(v.name))\n",
+ "sys.stdout.write(\"\\nResult : %d\"%(v.result))\n",
+ "sys.stdout.write(\"\\nGrade : %d\"%(v.grade))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Name : Sachin\n",
+ "Result : 1\n",
+ "Grade : 2"
+ ]
+ }
+ ],
+ "prompt_number": 53
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 13.23, Page number: 433"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Enumerated data type for 12 months\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#There is no enumerated data type in python and an alternate is to use class\n",
+ "#Class declaration\n",
+ "class month:\n",
+ " Jan = 0\n",
+ " Feb = 1\n",
+ " Mar = 2\n",
+ " Apr = 3\n",
+ " May = 4\n",
+ " June = 5\n",
+ " July = 6\n",
+ " Aug = 7\n",
+ " Sep = 8\n",
+ " Oct = 9\n",
+ " Nov = 10\n",
+ " Dec = 11\n",
+ " \n",
+ "#Class variable declaration\n",
+ "c = month()\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"\\nJan = %d\"%(c.Jan))\n",
+ "sys.stdout.write(\"\\nFeb = %d\"%(c.Feb))\n",
+ "sys.stdout.write(\"\\nJune = %d\"%(c.June))\n",
+ "sys.stdout.write(\"\\nDec = %d\"%(c.Dec))\n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Jan = 0\n",
+ "Feb = 1\n",
+ "June = 5\n",
+ "Dec = 11"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 13.24, Page number: 434"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Enumerated data type \n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#There is no enumerated data type in python and an alternate is to use class\n",
+ "#Class declaration\n",
+ "class month:\n",
+ " Jan = 1\n",
+ " Feb = 2\n",
+ " Mar = 3\n",
+ " Apr = 4\n",
+ " May = 5\n",
+ " June = 6\n",
+ " July = 7\n",
+ " Aug = 8\n",
+ " Sep = 9\n",
+ " Oct = 10\n",
+ " Nov = 11\n",
+ " Dec = 12\n",
+ " \n",
+ "#Class variable declaration\n",
+ "c = month()\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"\\nJan = %d\"%(c.Jan))\n",
+ "sys.stdout.write(\"\\nFeb = %d\"%(c.Feb))\n",
+ "sys.stdout.write(\"\\nJune = %d\"%(c.June))\n",
+ "sys.stdout.write(\"\\nDec = %d\"%(c.Dec))\n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Jan = 1\n",
+ "Feb = 2\n",
+ "June = 6\n",
+ "Dec = 12"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 13.25, Page number: 434"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Display name of month using enumerated data type\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#There is no enumerated data type in python and an alternate is to use class\n",
+ "#Class declaration\n",
+ "class month:\n",
+ " Jan = 1\n",
+ " Feb = 2\n",
+ " Mar = 3\n",
+ " Apr = 4\n",
+ " May = 5\n",
+ " June = 6\n",
+ " July = 7\n",
+ " Aug = 8\n",
+ " Sep = 9\n",
+ " Oct = 10\n",
+ " Nov = 11\n",
+ " Dec = 12\n",
+ " \n",
+ "#Class variable declaration\n",
+ "c = month()\n",
+ "\n",
+ "#Result\n",
+ "for f in range(c.Jan,c.Dec+1):\n",
+ " #There is no switch case statement in python\n",
+ " if f == c.Jan:\n",
+ " sys.stdout.write(\"\\nJanuary\")\n",
+ " else:\n",
+ " if f == c.Feb:\n",
+ " sys.stdout.write(\"\\nFebruary\")\n",
+ " else:\n",
+ " if f == c.Mar:\n",
+ " sys.stdout.write(\"\\nMarch\")\n",
+ " else:\n",
+ " if f == c.Apr:\n",
+ " sys.stdout.write(\"\\nApril\")\n",
+ " else:\n",
+ " if f == c.May:\n",
+ " sys.stdout.write(\"\\nMay\")\n",
+ " else:\n",
+ " if f == c.June:\n",
+ " sys.stdout.write(\"\\nJune\")\n",
+ " else:\n",
+ " if f == c.July:\n",
+ " sys.stdout.write(\"\\nJuly\")\n",
+ " else:\n",
+ " if f == c.Aug:\n",
+ " sys.stdout.write(\"\\nAugust\")\n",
+ " else:\n",
+ " if f == c.Sep:\n",
+ " sys.stdout.write(\"\\nSeptember\")\n",
+ " else:\n",
+ " if f == c.Oct:\n",
+ " sys.stdout.write(\"\\nOctober\")\n",
+ " else:\n",
+ " if f == c.Nov:\n",
+ " sys.stdout.write(\"\\nNovember\")\n",
+ " else:\n",
+ " sys.stdout.write(\"\\nDecember\")"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "January\n",
+ "February\n",
+ "March\n",
+ "April\n",
+ "May\n",
+ "June\n",
+ "July\n",
+ "August\n",
+ "September\n",
+ "October\n",
+ "November\n",
+ "December"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 13.26, Page number: 436"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Enumerated data type\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#There is no enumerated data type in python. class is used instead\n",
+ "class capital:\n",
+ " Mumbai = 0\n",
+ " Hyderabad = 1\n",
+ " Bangalore = 2\n",
+ "\n",
+ "class state:\n",
+ " name = ''\n",
+ " c = capital()\n",
+ " \n",
+ "#Class variable declaration\n",
+ "s = state()\n",
+ "c1 = capital()\n",
+ "\n",
+ "#Class variable initialization\n",
+ "s.name = \"Andhra Pradesh\"\n",
+ "s.c = s.c.Hyderabad\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"\\nState : %s\"%(s.name))\n",
+ "sys.stdout.write(\"\\nCapital : %d\"%(s.c))\n",
+ "\n",
+ "if s.c == c1.Hyderabad:\n",
+ " sys.stdout.write(\"\\nHyderabad is Capital of %s\"%(s.name))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "State : Andhra Pradesh\n",
+ "Capital : 1\n",
+ "Hyderabad is Capital of Andhra Pradesh"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 13.27, Page number: 437"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Identify the type of entered character using enumerated data type.\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Class declaration instead of enumerated data type\n",
+ "class ctype:\n",
+ " Letter = 0\n",
+ " Digit = 1\n",
+ " Other = 2\n",
+ " \n",
+ "#Variable Initialization\n",
+ "ch = raw_input(\"Enter any character\")\n",
+ "c = ctype()\n",
+ "f = ch.isalpha()\n",
+ "\n",
+ "#Result\n",
+ "if f != 0:\n",
+ " sys.stdout.write(\"\\n%c is %d type of symbol\"%(ch,c.Letter))\n",
+ "else:\n",
+ " f = ch.isdigit()\n",
+ " if f != 0:\n",
+ " sys.stdout.write(\"\\n%c is %d type of symbol\"%(ch,c.Digit))\n",
+ " else:\n",
+ " sys.stdout.write(\"\\n%c is %d type of symbol\"%(ch,c.Other))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any character=\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "= is 2 type of symbol"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 13.28, Page number: 438"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Size of union and number of bytes reserved for it\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#There is no union/structure in python. class is used instead\n",
+ "#Class declaration\n",
+ "class result:\n",
+ " marks = 0\n",
+ " grade = ''\n",
+ " \n",
+ "class res:\n",
+ " name = ''\n",
+ " age = 0\n",
+ " perf = result()\n",
+ " \n",
+ "#Class variable declaration\n",
+ "data = res()\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"Size of Union : %d\\n\"%(sys.getsizeof(data.perf)))\n",
+ "sys.stdout.write(\"Size of Structure : %d\\n\"%(sys.getsizeof(data)))\n",
+ "\n",
+ "#in python, value tagged method is used for data storage and can represent large numbers"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Size of Union : 36\n",
+ "Size of Structure : 36\n"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 13.29, Page number: 440"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Memory size of the computer\n",
+ "\n",
+ "import psutil\n",
+ "\n",
+ "psutil.phymem_usage()\n",
+ "\n",
+ "#Displays current status of the memory\n",
+ "#different systems will have different memory status"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "pyout",
+ "prompt_number": 4,
+ "text": [
+ "usage(total=1600512000L, used=1496383488L, free=104128512L, percent=93.5)"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 13.30, Page number: 440"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Display the system time at specified cursor position\n",
+ "\n",
+ "import sys\n",
+ "import datetime\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"%s\"%(datetime.datetime.now()))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "2013-09-20 11:04:15.649000"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 13.31, Page number: 441"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Change the cursor in different sizes\n",
+ "\n",
+ "import turtle\n",
+ "#Ipython supports graphics through turtle module\n",
+ "\n",
+ "from Tkinter import *\n",
+ "import Tkinter\n",
+ "\n",
+ "top = Tkinter.Tk()\n",
+ "\n",
+ "B1 = Tkinter.Button(top, text =\"circle\", relief=RAISED,\\\n",
+ " cursor=\"circle\")\n",
+ "B2 = Tkinter.Button(top, text =\"plus\", relief=RAISED,\\\n",
+ " cursor=\"plus\")\n",
+ "B1.pack()\n",
+ "B2.pack()\n",
+ "#top.mainloop()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 13.32, Page number: 441"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Create a directory using dos interrupt\n",
+ "\n",
+ "import os\n",
+ "import sys\n",
+ "\n",
+ "#Variable Initialization\n",
+ "dir1 = raw_input(\"Enter Directory Name : \")\n",
+ "\n",
+ "#Result\n",
+ "if not os.path.exists(dir1):\n",
+ " os.makedirs(dir1)\n",
+ " sys.stdout.write(\"Directory %s created\"%(dir1))\n",
+ "else:\n",
+ " sys.stdout.write(\"Directory %s not created\"%(dir1))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Directory XYZ created"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 13.33, Page number: 442"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Display the given character on the screen\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Variable Initialization\n",
+ "dl = 67\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"%c\"%(chr(dl)))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "C"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 13.34, Page number: 442"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Display the attributes of a file using DOS interrupt\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "import os\n",
+ "\n",
+ "print os.stat(\"IO.SYS\")"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "nt.stat_result(st_mode=33206, st_ino=0L, st_dev=0, st_nlink=0, st_uid=0, st_gid=0, st_size=0L, st_atime=1383454467L, st_mtime=1383454467L, st_ctime=1383454467L)\n"
+ ]
+ }
+ ],
+ "prompt_number": 12
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 13.35, Page number: 444"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Delete a file using dos interrupt\n",
+ "\n",
+ "import os\n",
+ "import sys\n",
+ "\n",
+ "#Variable Initialization\n",
+ "file1 = raw_input(\"Enter a file name : \")\n",
+ "\n",
+ "try:\n",
+ " os.remove(file1)\n",
+ " sys.stdout.write(\"File successfully deleted\")\n",
+ "except:\n",
+ " sys.stdout.write(\"File could not be deleted\")"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "File successfully deleted"
+ ]
+ }
+ ],
+ "prompt_number": 13
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 13.36, Page number: 445"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Structuret within union\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#there is no union/structure in python. class is used instead\n",
+ "#Class declaration\n",
+ "class x:\n",
+ " f = 0.0\n",
+ " p = ['' for i in range(0,2)]\n",
+ " \n",
+ "class z:\n",
+ " set1 = x()\n",
+ " \n",
+ "#Class variable declaration and initialization\n",
+ "st = z()\n",
+ "\n",
+ "st.set1.f = 5.5\n",
+ "st.set1.p[0] = 65\n",
+ "st.set1.p[1] = 66\n",
+ "\n",
+ "#Result\n",
+ "sys.stdout.write(\"\\n%g\"%(st.set1.f))\n",
+ "sys.stdout.write(\"\\n%c\"%(st.set1.p[0]))\n",
+ "sys.stdout.write(\"\\n%c\"%(st.set1.p[1]))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "5.5\n",
+ "A\n",
+ "B"
+ ]
+ }
+ ],
+ "prompt_number": 12
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file
diff --git a/Programming_in_C_using_ANSI_C/KamthaneChapter14.ipynb b/Programming_in_C_using_ANSI_C/KamthaneChapter14.ipynb
new file mode 100755
index 00000000..409585bd
--- /dev/null
+++ b/Programming_in_C_using_ANSI_C/KamthaneChapter14.ipynb
@@ -0,0 +1,2311 @@
+{
+ "metadata": {
+ "name": ""
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Chapter 14: Files"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 14.1, Page number: 454"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Write data to text file and read it\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Open file\n",
+ "fp = open('C:\\Users\\Aathira\\Documents\\IPython Notebooks\\data.txt','a')\n",
+ "\n",
+ "#Write data to file if successfully created the file\n",
+ "if fp == 0:\n",
+ " sys.stdout.write(\"Cannot open file\")\n",
+ "else:\n",
+ " sys.stdout.write(\"Write data & to stop press '.'\")\n",
+ " c = ''\n",
+ " while c != '.':\n",
+ " c = raw_input(\"Write data & to stop press '.'\")\n",
+ " fp.write(c)\n",
+ " fp.close()\n",
+ "\n",
+ " #Read and display the contents of file\n",
+ " sys.stdout.write(\"\\nContents read : \")\n",
+ " fp = open('C:\\Users\\Aathira\\Documents\\IPython Notebooks\\data.txt','r')\n",
+ " f = fp.readlines()\n",
+ " for f1 in f:\n",
+ " print f1\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Write data & to stop press '.'"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Write data & to stop press '.'ABCDEFGHIJK\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Write data & to stop press '.'.\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Contents read : ABCDEFGHIJK.\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 14.2, Page number: 455"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Display the contents of the file before and after appending.\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Open file for read data\n",
+ "fp = open('C:\\Users\\Aathira\\Documents\\IPython Notebooks\\data.txt','r')\n",
+ "\n",
+ "#Read and display the contents of file\n",
+ "sys.stdout.write(\"\\nContents of file before appending :\\n\")\n",
+ "fp = open('C:\\Users\\Aathira\\Documents\\IPython Notebooks\\data.txt','r')\n",
+ "f = fp.readlines()\n",
+ "for f1 in f:\n",
+ " print f1\n",
+ "\n",
+ "#Open file for appending\n",
+ "fp = open('C:\\Users\\Aathira\\Documents\\IPython Notebooks\\data.txt','a')\n",
+ "\n",
+ "if f == 0:\n",
+ " sys.stdout.write(\"File can not appended\")\n",
+ "else:\n",
+ " c = ''\n",
+ " #fp.write('\\n')\n",
+ " while c != '.':\n",
+ " c = raw_input(\"Enter string to append\")\n",
+ " fp.write(c)\n",
+ " \n",
+ " fp.close()\n",
+ " \n",
+ " #Read and display the contents of file\n",
+ " sys.stdout.write(\"\\nContents of file After appending\\n\")\n",
+ " fp = open('C:\\Users\\Aathira\\Documents\\IPython Notebooks\\data.txt','r')\n",
+ " f = fp.readlines()\n",
+ " for f1 in f:\n",
+ " print f1\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Contents of file before appending :\n",
+ "String is terminated with '\\0'.\n",
+ "\n",
+ "Contents of file After appending\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "String is terminated with '\\0'.This character is called as NULL character.\n"
+ ]
+ }
+ ],
+ "prompt_number": 16
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 14.3, Page number: 457"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Writing and reading of a file.\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Open file\n",
+ "fp = open('C:\\Users\\Aathira\\Documents\\IPython Notebooks\\data.txt','w+')\n",
+ "\n",
+ "#Write data to file if successfully created the file\n",
+ "if fp == 0:\n",
+ " sys.stdout.write(\"Cannot open file\")\n",
+ "else:\n",
+ " sys.stdout.write(\"Write data & to stop press '.'\")\n",
+ " c = ''\n",
+ " while c != '.':\n",
+ " c = raw_input(\"Write data & to stop press '.'\")\n",
+ " fp.write(c)\n",
+ " fp.close()\n",
+ "\n",
+ " #Read and display the contents of file\n",
+ " sys.stdout.write(\"\\nContents read : \")\n",
+ " fp = open('C:\\Users\\Aathira\\Documents\\IPython Notebooks\\data.txt','r')\n",
+ " f = fp.readlines()\n",
+ " for f1 in f:\n",
+ " print f1\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Write data & to stop press '.'\n",
+ "Contents read : "
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "ABCDEFGHIJK.\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 14.4, Page number: 458"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Open a file in append mode and add new records in it.\n",
+ "\n",
+ "import sys\n",
+ "\n",
+ "#Open file\n",
+ "fp = open('C:\\Users\\Aathira\\Documents\\IPython Notebooks\\data.txt','a+')\n",
+ "\n",
+ "#Write data to file if successfully created the file\n",
+ "if fp == 0:\n",
+ " sys.stdout.write(\"Cannot open file\")\n",
+ "else:\n",
+ " sys.stdout.write(\"Write data & to stop press '.'\")\n",
+ " c = ''\n",
+ " while c != '.':\n",
+ " c = raw_input(\"Write data & to stop press '.'\")\n",
+ " fp.write(c)\n",
+ " fp.close()\n",
+ "\n",
+ " #Read and display the contents of file\n",
+ " sys.stdout.write(\"\\nContents read : \")\n",
+ " fp = open('C:\\Users\\Aathira\\Documents\\IPython Notebooks\\data.txt','a+')\n",
+ " f = fp.readlines()\n",
+ " for f1 in f:\n",
+ " print f1\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Write data & to stop press '.'\n",
+ "Contents read : "
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "This is append and read mode.\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Example 14.5, Page number: 459