diff options
author | Hardik Ghaghada | 2014-06-20 15:52:25 +0530 |
---|---|---|
committer | Hardik Ghaghada | 2014-06-20 15:52:25 +0530 |
commit | e1e59ca3a50d9f93e8b7bc0693b8081d5db77771 (patch) | |
tree | f54eab21dd3d725d64a495fcd47c00d37abed004 /ANSI_C_Programming | |
parent | a78126bbe4443e9526a64df9d8245c4af8843044 (diff) | |
parent | 83c1bfceb1b681b4bb7253b47491be2d8b2014a1 (diff) | |
download | Python-Textbook-Companions-e1e59ca3a50d9f93e8b7bc0693b8081d5db77771.tar.gz Python-Textbook-Companions-e1e59ca3a50d9f93e8b7bc0693b8081d5db77771.tar.bz2 Python-Textbook-Companions-e1e59ca3a50d9f93e8b7bc0693b8081d5db77771.zip |
Merge pull request #1 from debashisdeb/master
removing problem statements from all the chapters to avoid copyright violations
Diffstat (limited to 'ANSI_C_Programming')
-rw-r--r-- | ANSI_C_Programming/chapter1.ipynb | 398 | ||||
-rw-r--r-- | ANSI_C_Programming/chapter10.ipynb | 1534 | ||||
-rw-r--r-- | ANSI_C_Programming/chapter11.ipynb | 1065 | ||||
-rw-r--r-- | ANSI_C_Programming/chapter12.ipynb | 2909 | ||||
-rw-r--r-- | ANSI_C_Programming/chapter13.ipynb | 1421 | ||||
-rw-r--r-- | ANSI_C_Programming/chapter14.ipynb | 812 | ||||
-rw-r--r-- | ANSI_C_Programming/chapter15.ipynb | 1381 | ||||
-rw-r--r-- | ANSI_C_Programming/chapter2.ipynb | 1563 | ||||
-rw-r--r-- | ANSI_C_Programming/chapter3.ipynb | 3178 | ||||
-rw-r--r-- | ANSI_C_Programming/chapter4.ipynb | 777 | ||||
-rw-r--r-- | ANSI_C_Programming/chapter5.ipynb | 1939 | ||||
-rw-r--r-- | ANSI_C_Programming/chapter6.ipynb | 3460 | ||||
-rw-r--r-- | ANSI_C_Programming/chapter7.ipynb | 1531 | ||||
-rw-r--r-- | ANSI_C_Programming/chapter8.ipynb | 2798 | ||||
-rw-r--r-- | ANSI_C_Programming/chapter9.ipynb | 1914 |
15 files changed, 13341 insertions, 13339 deletions
diff --git a/ANSI_C_Programming/chapter1.ipynb b/ANSI_C_Programming/chapter1.ipynb index d5485e0e..1c3108fa 100644 --- a/ANSI_C_Programming/chapter1.ipynb +++ b/ANSI_C_Programming/chapter1.ipynb @@ -1,200 +1,200 @@ -{
- "metadata": {
- "name": "chapter1.ipynb"
- },
- "nbformat": 3,
- "nbformat_minor": 0,
- "worksheets": [
- {
- "cells": [
- {
- "cell_type": "heading",
- "level": 1,
- "metadata": {},
- "source": [
- "CHAPTER 1: GETTING STARTED"
- ]
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:12"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Calculation of simple interest\n",
- "#Author gekay Date:25/08/2009\n",
- "p=1000\n",
- "n=3\n",
- "r=8.5\n",
- "si=p*n*r/100 #formula for simple interest\n",
- "print \"%f\\n\" % (si)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "255.000000\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 1
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:17"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Calculation of simple interest\n",
- "#Author gekay Date 25/07/2008\n",
- "print \"Enter values of p,n,r\"\n",
- "p=eval(raw_input())\n",
- "n=eval(raw_input())\n",
- "r=eval(raw_input())\n",
- "si=p*n*r/100\n",
- "print \"%f\\n\" % (si)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter values of p,n,r\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "1000\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "3\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "8.5\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "255.000000\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 2
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "Example on page:18"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Just for fun. Author.Bozo\n",
- "print \"Enter a number\"\n",
- "num=eval(raw_input()) #to take user input\n",
- "print \"Now I am letting you on a secret...\\n\"\n",
- "print \"You have just entered the number %d\\n\" % (num)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter a number\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "30\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Now I am letting you on a secret...\n",
- "\n",
- "You have just entered the number 30\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 3
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:23"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#to find exponentiation of numbers\n",
- "a=pow(3,2) #(3**2) will also do the same operation\n",
- "print \"%d\" % (a)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "9\n"
- ]
- }
- ],
- "prompt_number": 4
- }
- ],
- "metadata": {}
- }
- ]
+{ + "metadata": { + "name": "", + "signature": "sha256:a5caa773def394b4fd51c60fb4856df2fdb5f2105f126537a15f6520ea337bff" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "CHAPTER 1: GETTING STARTED" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:12" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + " \n", + "p=1000\n", + "n=3\n", + "r=8.5\n", + "si=p*n*r/100 #formula for simple interest\n", + "print \"%f\\n\" % (si)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "255.000000\n", + "\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:17" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "print \"Enter values of p,n,r\"\n", + "p=eval(raw_input())\n", + "n=eval(raw_input())\n", + "r=eval(raw_input())\n", + "si=p*n*r/100\n", + "print \"%f\\n\" % (si)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter values of p,n,r\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1000\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "8.5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "255.000000\n", + "\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example on page:18" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "print \"Enter a number\"\n", + "num=eval(raw_input()) #to take user input\n", + "print \"Now I am letting you on a secret...\\n\"\n", + "print \"You have just entered the number %d\\n\" % (num)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a number\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "30\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Now I am letting you on a secret...\n", + "\n", + "You have just entered the number 30\n", + "\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:23" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "a=pow(3,2) #(3**2) will also do the same operation\n", + "print \"%d\" % (a)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "9\n" + ] + } + ], + "prompt_number": 4 + } + ], + "metadata": {} + } + ] }
\ No newline at end of file diff --git a/ANSI_C_Programming/chapter10.ipynb b/ANSI_C_Programming/chapter10.ipynb index 51080026..370cbcea 100644 --- a/ANSI_C_Programming/chapter10.ipynb +++ b/ANSI_C_Programming/chapter10.ipynb @@ -1,768 +1,768 @@ -{
- "metadata": {
- "name": "chapter10.ipynb"
- },
- "nbformat": 3,
- "nbformat_minor": 0,
- "worksheets": [
- {
- "cells": [
- {
- "cell_type": "heading",
- "level": 1,
- "metadata": {},
- "source": [
- "CHAPTER 10: STRUCTURES"
- ]
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:342-343"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#storing & printing names,prices & pages of books\n",
- "name=[]\n",
- "price=[]\n",
- "pages=[]\n",
- "for i in range(3):\n",
- " name.append(\"\\0\")\n",
- " price.append(0)\n",
- " pages.append(0)\n",
- "print \"Enter names,prices and no. of pages of 3 books\\n\"\n",
- "for i in range(0,3,1):\n",
- " name[i]=raw_input()\n",
- " price[i]=eval(raw_input())\n",
- " pages[i]=eval(raw_input())\n",
- "print \"\\nAnd this is what you entered\\n\"\n",
- "for i in range(0,3,1):\n",
- " print \"%c %f %d\\n\" % (name[i],price[i],pages[i])"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter names,prices and no. of pages of 3 books\n",
- "\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "A\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "100.00\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "354\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "C\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "256.50\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "682\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "F\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "233.70\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "512\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "\n",
- "And this is what you entered\n",
- "\n",
- "A 100.000000 354\n",
- "\n",
- "C 256.500000 682\n",
- "\n",
- "F 233.700000 512\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 1
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:343-344"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#storing & printing names,prices & pages of books using structure\n",
- "class book: #YOU CAN ALSO USE CTYPES FOR IMPLEMENTING STRUCTURES\n",
- " def __init__(self, **kwds):\n",
- " self.__dict__.update(kwds)\n",
- "b1=book()\n",
- "b2=book()\n",
- "b3=book()\n",
- "print \"Enter names,prices & no. of pages of 3 books\\n\"\n",
- "b1.name=raw_input()\n",
- "b1.price=eval(raw_input())\n",
- "b1.pages=eval(raw_input())\n",
- "b2.name=raw_input()\n",
- "b2.price=eval(raw_input())\n",
- "b2.pages=eval(raw_input())\n",
- "b3.name=raw_input()\n",
- "b3.price=eval(raw_input())\n",
- "b3.pages=eval(raw_input())\n",
- "print \"And this is what you entered\\n\"\n",
- "print \"%c %f %d\\n\" % (b1.name,b1.price,b1.pages)\n",
- "print \"%c %f %d\\n\" % (b2.name,b2.price,b2.pages)\n",
- "print \"%c %f %d\\n\" % (b3.name,b3.price,b3.pages)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter names,prices & no. of pages of 3 books\n",
- "\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "A\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "100.00\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "354\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "C\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "256.50\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "682\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "F\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "233.70\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "512\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "And this is what you entered\n",
- "\n",
- "A 100.000000 354\n",
- "\n",
- "C 256.500000 682\n",
- "\n",
- "F 233.700000 512\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 2
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:347"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Memory map of structure elements\n",
- "class book():\n",
- " def __init__(self,**kwds):\n",
- " self.__dict__.update(kwds)\n",
- "b1=book(name='B',price=130.00,pages=550)\n",
- "print \"Address of name=%u\\n\" % (id(b1.name))\n",
- "print \"Address of price=%u\\n\" % (id(b1.price))\n",
- "print \"Address of pages=%u\\n\" % (id(b1.pages))"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Address of name=20788864\n",
- "\n",
- "Address of price=88371144\n",
- "\n",
- "Address of pages=88859008\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 3
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:348-349"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Usage of an array of stuctures\n",
- "def linkfloat():\n",
- " a=0\n",
- " b=id(a) #cause emulator to be linked\n",
- "class book():\n",
- " def __init__(self,**kwds):\n",
- " self.__dict__.update(kwds)\n",
- "b=[]\n",
- "for i in range(3):\n",
- " b.append(0) #setting initial value as zero\n",
- "for i in range(0,3,1):\n",
- " b[i]=book()\n",
- "for i in range(0,3,1):\n",
- " print \"Enter name price and pages\"\n",
- " b[i].name=raw_input()\n",
- " b[i].price=eval(raw_input())\n",
- " b[i].pages=eval(raw_input())\n",
- "for i in range(0,3,1):\n",
- " print \"%c %f %d\\n\" % (b[i].name,b[i].price,b[i].pages)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter name price and pages\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "A\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "100.00\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "354\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter name price and pages\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "C\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "256.50\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "682\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter name price and pages\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "F\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "233.70\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "512\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "A 100.000000 354\n",
- "\n",
- "C 256.500000 682\n",
- "\n",
- "F 233.700000 512\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 4
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:350-351"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Copying elements of an structure into another structure\n",
- "class employee():\n",
- " def __init__(self,**kwds):\n",
- " self.__dict__.update(kwds)\n",
- "e1=employee(name=\"Sanjay\",age=30,salary=5500.50)\n",
- "e2=employee()\n",
- "e3=employee()\n",
- "#piece-meal copying\n",
- "import copy\n",
- "e2.name=copy.copy(e1.name)\n",
- "e2.age=e1.age\n",
- "e2.salary=e1.salary\n",
- "#copying all elements at one go\n",
- "e3=e2\n",
- "print \"%s %d %f\\n\" % (e1.name,e1.age,e1.salary)\n",
- "print \"%s %d %f\\n\" % (e2.name,e2.age,e2.salary)\n",
- "print \"%s %d %f\\n\" % (e3.name,e3.age,e3.salary)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Sanjay 30 5500.500000\n",
- "\n",
- "Sanjay 30 5500.500000\n",
- "\n",
- "Sanjay 30 5500.500000\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 5
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:351-352"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#declaring a structure inside another structure\n",
- "class address():\n",
- " def __init__(self, **kwds):\n",
- " self.__dict__.update(kwds)\n",
- "class emp():\n",
- " def __init__(self, **kwds):\n",
- " self.__dict__.update(kwds)\n",
- "a = address(phone=\"531046\", city=\"nagpur\", pin=10)\n",
- "e = emp(name=\"jeru\", address=a)\n",
- "print \"name=%s phone=%s\\n\" % (e.name,e.address.phone)\n",
- "print \"city=%s pin=%d\\n\" % (e.address.city,e.address.pin)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "name=jeru phone=531046\n",
- "\n",
- "city=nagpur pin=10\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 6
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:353"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#passing individual structure elements to a function\n",
- "class book():\n",
- " def __init__(self, **kwds):\n",
- " self.__dict__.update(kwds)\n",
- "def display(s,t,n):\n",
- " print \"%s %s %d\\n\" % (s,t,n)\n",
- "b1=book(name=\"Let us C\",author=\"YPK\",callno=101)\n",
- "display(b1.name,b1.author,b1.callno)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Let us C YPK 101\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 7
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:353-354"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#passing whole structure to a function\n",
- "class book():\n",
- " def __init__(self, **kwds):\n",
- " self.__dict__.update(kwds)\n",
- "def display(b):\n",
- " print \"%s %s %d\\n\" % (b.name,b.author,b.callno)\n",
- "b1=book(name=\"Let us C\",author=\"YPK\",callno=101)\n",
- "display(b1)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Let us C YPK 101\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 8
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:354-355"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#printing structure elements to a function\n",
- "class book():\n",
- " def __init__(self, **kwds):\n",
- " self.__dict__.update(kwds)\n",
- "b1=book(name=\"Let us C\",author=\"YPK\",callno=101)\n",
- "ptr=book()\n",
- "ptr=b1\n",
- "print \"%s %s %d\\n\" % (b1.name,b1.author,b1.callno)\n",
- "print \"%s %s %d\\n\" % (ptr.name,ptr.author,ptr.callno)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Let us C YPK 101\n",
- "\n",
- "Let us C YPK 101\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 9
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:355-356"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#passing address of a structure variable\n",
- "class book():\n",
- " def __init__(self, **kwds):\n",
- " self.__dict__.update(kwds)\n",
- "def display(b):\n",
- " print \"%s %s %d\\n\" % (b.name,b.author,b.callno)\n",
- "b1=book(name=\"Let us C\",author=\"YPK\",callno=101)\n",
- "display(b1)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Let us C YPK 101\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 10
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:356"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#printing address of elements of structure\n",
- "class emp():\n",
- " def __init__(self, **kwds):\n",
- " self.__dict__.update(kwds)\n",
- "e=emp(a=0,ch='\\0',s=0.0)\n",
- "print \"%u %u %u\\n\" % (id(e.a),id(e.ch),id(e.s))"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "20623060 20204312 88374024\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 11
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:357-358"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Implementation of structure\n",
- "#pragma directive is not present in python\n",
- "class emp():\n",
- " def __init__(self,a,ch,s): #Structure without using dictionary\n",
- " self.a=a\n",
- " self.ch=ch\n",
- " self.s=s\n",
- "e=emp(a=0,ch='\\0',s=0.0)\n",
- "print \"%u %u %u\\n\" % (id(e.a),id(e.ch),id(e.s))"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "20360916 19942168 88177544\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 10
- }
- ],
- "metadata": {}
- }
- ]
+{ + "metadata": { + "name": "", + "signature": "sha256:2551d4c0f797f2bbef63a528dde1d2dd1067208b3f54db92e7a20ac687ed39db" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "CHAPTER 10: STRUCTURES" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:342-343" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "name=[]\n", + "price=[]\n", + "pages=[]\n", + "for i in range(3):\n", + " name.append(\"\\0\")\n", + " price.append(0)\n", + " pages.append(0)\n", + "print \"Enter names,prices and no. of pages of 3 books\\n\"\n", + "for i in range(0,3,1):\n", + " name[i]=raw_input()\n", + " price[i]=eval(raw_input())\n", + " pages[i]=eval(raw_input())\n", + "print \"\\nAnd this is what you entered\\n\"\n", + "for i in range(0,3,1):\n", + " print \"%c %f %d\\n\" % (name[i],price[i],pages[i])" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter names,prices and no. of pages of 3 books\n", + "\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "A\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "100.00\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "354\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "C\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "256.50\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "682\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "F\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "233.70\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "512\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "And this is what you entered\n", + "\n", + "A 100.000000 354\n", + "\n", + "C 256.500000 682\n", + "\n", + "F 233.700000 512\n", + "\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:343-344" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "class book: #YOU CAN ALSO USE CTYPES FOR IMPLEMENTING STRUCTURES\n", + " def __init__(self, **kwds):\n", + " self.__dict__.update(kwds)\n", + "b1=book()\n", + "b2=book()\n", + "b3=book()\n", + "print \"Enter names,prices & no. of pages of 3 books\\n\"\n", + "b1.name=raw_input()\n", + "b1.price=eval(raw_input())\n", + "b1.pages=eval(raw_input())\n", + "b2.name=raw_input()\n", + "b2.price=eval(raw_input())\n", + "b2.pages=eval(raw_input())\n", + "b3.name=raw_input()\n", + "b3.price=eval(raw_input())\n", + "b3.pages=eval(raw_input())\n", + "print \"And this is what you entered\\n\"\n", + "print \"%c %f %d\\n\" % (b1.name,b1.price,b1.pages)\n", + "print \"%c %f %d\\n\" % (b2.name,b2.price,b2.pages)\n", + "print \"%c %f %d\\n\" % (b3.name,b3.price,b3.pages)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter names,prices & no. of pages of 3 books\n", + "\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "A\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "100.00\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "354\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "C\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "256.50\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "682\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "F\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "233.70\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "512\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "And this is what you entered\n", + "\n", + "A 100.000000 354\n", + "\n", + "C 256.500000 682\n", + "\n", + "F 233.700000 512\n", + "\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:347" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "class book():\n", + " def __init__(self,**kwds):\n", + " self.__dict__.update(kwds)\n", + "b1=book(name='B',price=130.00,pages=550)\n", + "print \"Address of name=%u\\n\" % (id(b1.name))\n", + "print \"Address of price=%u\\n\" % (id(b1.price))\n", + "print \"Address of pages=%u\\n\" % (id(b1.pages))" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Address of name=20788864\n", + "\n", + "Address of price=88371144\n", + "\n", + "Address of pages=88859008\n", + "\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:348-349" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def linkfloat():\n", + " a=0\n", + " b=id(a) #cause emulator to be linked\n", + "class book():\n", + " def __init__(self,**kwds):\n", + " self.__dict__.update(kwds)\n", + "b=[]\n", + "for i in range(3):\n", + " b.append(0) #setting initial value as zero\n", + "for i in range(0,3,1):\n", + " b[i]=book()\n", + "for i in range(0,3,1):\n", + " print \"Enter name price and pages\"\n", + " b[i].name=raw_input()\n", + " b[i].price=eval(raw_input())\n", + " b[i].pages=eval(raw_input())\n", + "for i in range(0,3,1):\n", + " print \"%c %f %d\\n\" % (b[i].name,b[i].price,b[i].pages)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter name price and pages\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "A\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "100.00\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "354\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter name price and pages\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "C\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "256.50\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "682\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter name price and pages\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "F\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "233.70\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "512\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "A 100.000000 354\n", + "\n", + "C 256.500000 682\n", + "\n", + "F 233.700000 512\n", + "\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:350-351" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "class employee():\n", + " def __init__(self,**kwds):\n", + " self.__dict__.update(kwds)\n", + "e1=employee(name=\"Sanjay\",age=30,salary=5500.50)\n", + "e2=employee()\n", + "e3=employee()\n", + "#piece-meal copying\n", + "import copy\n", + "e2.name=copy.copy(e1.name)\n", + "e2.age=e1.age\n", + "e2.salary=e1.salary\n", + "#copying all elements at one go\n", + "e3=e2\n", + "print \"%s %d %f\\n\" % (e1.name,e1.age,e1.salary)\n", + "print \"%s %d %f\\n\" % (e2.name,e2.age,e2.salary)\n", + "print \"%s %d %f\\n\" % (e3.name,e3.age,e3.salary)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sanjay 30 5500.500000\n", + "\n", + "Sanjay 30 5500.500000\n", + "\n", + "Sanjay 30 5500.500000\n", + "\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:351-352" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "class address():\n", + " def __init__(self, **kwds):\n", + " self.__dict__.update(kwds)\n", + "class emp():\n", + " def __init__(self, **kwds):\n", + " self.__dict__.update(kwds)\n", + "a = address(phone=\"531046\", city=\"nagpur\", pin=10)\n", + "e = emp(name=\"jeru\", address=a)\n", + "print \"name=%s phone=%s\\n\" % (e.name,e.address.phone)\n", + "print \"city=%s pin=%d\\n\" % (e.address.city,e.address.pin)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "name=jeru phone=531046\n", + "\n", + "city=nagpur pin=10\n", + "\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:353" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "class book():\n", + " def __init__(self, **kwds):\n", + " self.__dict__.update(kwds)\n", + "def display(s,t,n):\n", + " print \"%s %s %d\\n\" % (s,t,n)\n", + "b1=book(name=\"Let us C\",author=\"YPK\",callno=101)\n", + "display(b1.name,b1.author,b1.callno)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Let us C YPK 101\n", + "\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:353-354" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "class book():\n", + " def __init__(self, **kwds):\n", + " self.__dict__.update(kwds)\n", + "def display(b):\n", + " print \"%s %s %d\\n\" % (b.name,b.author,b.callno)\n", + "b1=book(name=\"Let us C\",author=\"YPK\",callno=101)\n", + "display(b1)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Let us C YPK 101\n", + "\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:354-355" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "class book():\n", + " def __init__(self, **kwds):\n", + " self.__dict__.update(kwds)\n", + "b1=book(name=\"Let us C\",author=\"YPK\",callno=101)\n", + "ptr=book()\n", + "ptr=b1\n", + "print \"%s %s %d\\n\" % (b1.name,b1.author,b1.callno)\n", + "print \"%s %s %d\\n\" % (ptr.name,ptr.author,ptr.callno)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Let us C YPK 101\n", + "\n", + "Let us C YPK 101\n", + "\n" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:355-356" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "class book():\n", + " def __init__(self, **kwds):\n", + " self.__dict__.update(kwds)\n", + "def display(b):\n", + " print \"%s %s %d\\n\" % (b.name,b.author,b.callno)\n", + "b1=book(name=\"Let us C\",author=\"YPK\",callno=101)\n", + "display(b1)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Let us C YPK 101\n", + "\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:356" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "class emp():\n", + " def __init__(self, **kwds):\n", + " self.__dict__.update(kwds)\n", + "e=emp(a=0,ch='\\0',s=0.0)\n", + "print \"%u %u %u\\n\" % (id(e.a),id(e.ch),id(e.s))" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "20623060 20204312 88374024\n", + "\n" + ] + } + ], + "prompt_number": 11 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:357-358" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "class emp():\n", + " def __init__(self,a,ch,s): #Structure without using dictionary\n", + " self.a=a\n", + " self.ch=ch\n", + " self.s=s\n", + "e=emp(a=0,ch='\\0',s=0.0)\n", + "print \"%u %u %u\\n\" % (id(e.a),id(e.ch),id(e.s))" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "20360916 19942168 88177544\n", + "\n" + ] + } + ], + "prompt_number": 10 + } + ], + "metadata": {} + } + ] }
\ No newline at end of file diff --git a/ANSI_C_Programming/chapter11.ipynb b/ANSI_C_Programming/chapter11.ipynb index 4d9a0fc1..f0dba284 100644 --- a/ANSI_C_Programming/chapter11.ipynb +++ b/ANSI_C_Programming/chapter11.ipynb @@ -1,533 +1,534 @@ -{
- "metadata": {
- "name": "chapter11.ipynb"
- },
- "nbformat": 3,
- "nbformat_minor": 0,
- "worksheets": [
- {
- "cells": [
- {
- "cell_type": "heading",
- "level": 1,
- "metadata": {},
- "source": [
- "CHAPTER 11: CONSOLE INPUT/OUTPUT"
- ]
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:372"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#program explaining the interpretation of contents of format string\n",
- "avg=346\n",
- "per=69.2\n",
- "print \"Average=%d\\nPercentage=%f\\n\" % (avg,per)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Average=346\n",
- "Percentage=69.200000\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 1
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:374"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#program explaining field-width of format specifiers\n",
- "weight=63\n",
- "print \"weight is %d kg\\n\" % (weight)\n",
- "print \"weight is %2d kg\\n\" % (weight)\n",
- "print \"weight is %4d kg\\n\" % (weight)\n",
- "print \"weight is %6d kg\\n\" % (weight)\n",
- "print \"weight is %-6d kg\\n\" % (weight)\n",
- "print \"weight is %1d kg\\n\" % (weight)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "weight is 63 kg\n",
- "\n",
- "weight is 63 kg\n",
- "\n",
- "weight is 63 kg\n",
- "\n",
- "weight is 63 kg\n",
- "\n",
- "weight is 63 kg\n",
- "\n",
- "weight is 63 kg\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 2
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:374-375"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#program without using field-width\n",
- "print \"%f%f%f\\n\" % (5.0,13.5,133.9)\n",
- "print \"%f%f%f\\n\" % (305.0,1200.9,3005.3)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "5.00000013.500000133.900000\n",
- "\n",
- "305.0000001200.9000003005.300000\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 3
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:375"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#program using field-width to line-up properly\n",
- "print \"%10.1f%10.1f%10.1f\\n\" % (5.0,13.5,133.9)\n",
- "print \"%10.1f%10.1f%10.1f\\n\" % (305.0,1200.9,3005.3)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- " 5.0 13.5 133.9\n",
- "\n",
- " 305.0 1200.9 3005.3\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 4
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:375"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Formatting strings with printf()\n",
- "firstname1=\"Sandy\"\n",
- "surname1=\"Malya\"\n",
- "firstname2=\"AjayKumar\"\n",
- "surname2=\"Gurubaxani\"\n",
- "print \"%20s%20s\\n\" % (firstname1,surname1)\n",
- "print \"%20s%20s\\n\" % (firstname2,surname2)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- " Sandy Malya\n",
- "\n",
- " AjayKumar Gurubaxani\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 5
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:376"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Use of escape sequences like tab and newlines\n",
- "print \"You\\tmust\\tbe\\tcrazy\\nto\\thate\\tthis\\tbook\\n\""
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "You\tmust\tbe\tcrazy\n",
- "to\thate\tthis\tbook\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 6
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:377-378"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#using different format specifiers in print function\n",
- "ch='z'\n",
- "i=125\n",
- "a=12.55\n",
- "s=\"hello there!\"\n",
- "print \"%c %d %f\\n\" % (ch,ord(ch),ord(ch))\n",
- "print \"%s\\n\" % (s) #here conversation not possible\n",
- "print \"%c %d %f\\n\" % (i,i,i)\n",
- "print \"%f %d\\n\" % (a,a)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "z 122 122.000000\n",
- "\n",
- "hello there!\n",
- "\n",
- "} 125 125.000000\n",
- "\n",
- "12.550000 12\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 9
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:379"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#assigning elements to an array\n",
- "i=10\n",
- "ch='A'\n",
- "a=3.14\n",
- "print \"%d %c %f\\n\" % (i,ch,a)\n",
- "str=[i,ch,a]\n",
- "print \"%s\\n\" % (str)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "10 A 3.140000\n",
- "\n",
- "[10, 'A', 3.14]\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 10
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:380"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#use of input function\n",
- "print \"Press any key to continue\"\n",
- "raw_input() #will echo the character\n",
- "print \"\\nType any character\"\n",
- "ch=raw_input() #will echo the character typed\n",
- "print \"\\nType any character\"\n",
- "raw_input() #will echo character\n",
- "print \"\\nContinue Y/N\"\n",
- "raw_input() #will echo character"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Press any key to continue\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- " \n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "\n",
- "Type any character\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "B\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "\n",
- "Type any character\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "W\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "\n",
- "Continue Y/N\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Y\n"
- ]
- },
- {
- "metadata": {},
- "output_type": "pyout",
- "prompt_number": 11,
- "text": [
- "'Y'"
- ]
- }
- ],
- "prompt_number": 11
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:381"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Use of print function\n",
- "ch='A'\n",
- "print ch,\n",
- "print(ch),\n",
- "print ch,\n",
- "print 'Z',\n",
- "print('Z'),\n",
- "print'Z',"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "A A A Z Z Z\n"
- ]
- }
- ],
- "prompt_number": 14
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:381"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#program to input a string of characters\n",
- "print \"Enter name\"\n",
- "name=raw_input()\n",
- "print \"%s\\n\" % (name)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter name\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Jonty Rhodes\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Jonty Rhodes\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 15
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:382"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#program to input a string of characters\n",
- "print \"Enter name\"\n",
- "footballer=raw_input() #sends base address of array\n",
- "print \"Happy footballing!\"\n",
- "print footballer"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter name\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Jonty Rhodes\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Happy footballing!\n",
- "Jonty Rhodes\n"
- ]
- }
- ],
- "prompt_number": 16
- }
- ],
- "metadata": {}
- }
- ]
+{ + "metadata": { + "name": "", + "signature": "sha256:b6a9263efd333c836685db4fb9578682414d12b2c46fafab1217faab10d2f61b" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "CHAPTER 11: CONSOLE INPUT/OUTPUT" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:372" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "avg=346\n", + "per=69.2\n", + "print \"Average=%d\\nPercentage=%f\\n\" % (avg,per)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Average=346\n", + "Percentage=69.200000\n", + "\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:374" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "weight=63\n", + "print \"weight is %d kg\\n\" % (weight)\n", + "print \"weight is %2d kg\\n\" % (weight)\n", + "print \"weight is %4d kg\\n\" % (weight)\n", + "print \"weight is %6d kg\\n\" % (weight)\n", + "print \"weight is %-6d kg\\n\" % (weight)\n", + "print \"weight is %1d kg\\n\" % (weight)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "weight is 63 kg\n", + "\n", + "weight is 63 kg\n", + "\n", + "weight is 63 kg\n", + "\n", + "weight is 63 kg\n", + "\n", + "weight is 63 kg\n", + "\n", + "weight is 63 kg\n", + "\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:374-375" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "print \"%f%f%f\\n\" % (5.0,13.5,133.9)\n", + "print \"%f%f%f\\n\" % (305.0,1200.9,3005.3)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "5.00000013.500000133.900000\n", + "\n", + "305.0000001200.9000003005.300000\n", + "\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:375" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "print \"%10.1f%10.1f%10.1f\\n\" % (5.0,13.5,133.9)\n", + "print \"%10.1f%10.1f%10.1f\\n\" % (305.0,1200.9,3005.3)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " 5.0 13.5 133.9\n", + "\n", + " 305.0 1200.9 3005.3\n", + "\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:375" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "firstname1=\"Sandy\"\n", + "surname1=\"Malya\"\n", + "firstname2=\"AjayKumar\"\n", + "surname2=\"Gurubaxani\"\n", + "print \"%20s%20s\\n\" % (firstname1,surname1)\n", + "print \"%20s%20s\\n\" % (firstname2,surname2)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Sandy Malya\n", + "\n", + " AjayKumar Gurubaxani\n", + "\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:376" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "print \"You\\tmust\\tbe\\tcrazy\\nto\\thate\\tthis\\tbook\\n\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "You\tmust\tbe\tcrazy\n", + "to\thate\tthis\tbook\n", + "\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:377-378" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "ch='z'\n", + "i=125\n", + "a=12.55\n", + "s=\"hello there!\"\n", + "print \"%c %d %f\\n\" % (ch,ord(ch),ord(ch))\n", + "print \"%s\\n\" % (s) #here conversation not possible\n", + "print \"%c %d %f\\n\" % (i,i,i)\n", + "print \"%f %d\\n\" % (a,a)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "z 122 122.000000\n", + "\n", + "hello there!\n", + "\n", + "} 125 125.000000\n", + "\n", + "12.550000 12\n", + "\n" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:379" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "i=10\n", + "ch='A'\n", + "a=3.14\n", + "print \"%d %c %f\\n\" % (i,ch,a)\n", + "str=[i,ch,a]\n", + "print \"%s\\n\" % (str)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "10 A 3.140000\n", + "\n", + "[10, 'A', 3.14]\n", + "\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:380" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "print \"Press any key to continue\"\n", + "raw_input() #will echo the character\n", + "print \"\\nType any character\"\n", + "ch=raw_input() #will echo the character typed\n", + "print \"\\nType any character\"\n", + "raw_input() #will echo character\n", + "print \"\\nContinue Y/N\"\n", + "raw_input() #will echo character" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Press any key to continue\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + " \n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Type any character\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "B\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Type any character\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "W\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Continue Y/N\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Y\n" + ] + }, + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 11, + "text": [ + "'Y'" + ] + } + ], + "prompt_number": 11 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:381" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "ch='A'\n", + "print ch,\n", + "print(ch),\n", + "print ch,\n", + "print 'Z',\n", + "print('Z'),\n", + "print'Z'," + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "A A A Z Z Z\n" + ] + } + ], + "prompt_number": 14 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:381" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "print \"Enter name\"\n", + "name=raw_input()\n", + "print \"%s\\n\" % (name)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter name\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Jonty Rhodes\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Jonty Rhodes\n", + "\n" + ] + } + ], + "prompt_number": 15 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:382" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "print \"Enter name\"\n", + "footballer=raw_input() #sends base address of array\n", + "print \"Happy footballing!\"\n", + "print footballer" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter name\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Jonty Rhodes\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Happy footballing!\n", + "Jonty Rhodes\n" + ] + } + ], + "prompt_number": 16 + } + ], + "metadata": {} + } + ] }
\ No newline at end of file diff --git a/ANSI_C_Programming/chapter12.ipynb b/ANSI_C_Programming/chapter12.ipynb index 1c8b157f..8bab1a13 100644 --- a/ANSI_C_Programming/chapter12.ipynb +++ b/ANSI_C_Programming/chapter12.ipynb @@ -1,1455 +1,1456 @@ -{
- "metadata": {
- "name": "chapter12.ipynb"
- },
- "nbformat": 3,
- "nbformat_minor": 0,
- "worksheets": [
- {
- "cells": [
- {
- "cell_type": "heading",
- "level": 1,
- "metadata": {},
- "source": [
- "CHAPTER 12:FILE INPUT/OUTPUT"
- ]
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:391"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Reads strings from the file and displays them on screen\n",
- "fp=open('PR1.txt','r') #open the file for reading\n",
- "ch=fp.readlines() #ch will store all content of file\n",
- "print \"%s\" % (' '.join(ch)) #prints content of file\n",
- "print \"\\n\"\n",
- "fp.close()"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "site:http://www.bzu.edu.pk/\n",
- " user:siteuser@localhost\n",
- " version:5.5.32-0ubuntu0.12.04.1\n",
- " db: bzu\n",
- " tables:\n",
- " username:web,webadmin,webadministrator23\n",
- " pass:71dc9f7af599450b23a3b5d54bc665c7,\t49fa1a081c8d5c8dcfa05d730803251a,\t*E8665C4049F515D836A3C8704D0543C6DA0FE96D\n",
- "\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 8
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:394"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Showing error in opening a file\n",
- "try:\n",
- " fp=open('yoyo.txt','r')\n",
- " for line in fin:\n",
- " print line\n",
- " fp.close()\n",
- "except:\n",
- " print 'cannot open file'"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "cannot open file\n"
- ]
- }
- ],
- "prompt_number": 9
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:395"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Count chars, spaces, tabs and newlines in a file\n",
- "nol=0\n",
- "nom=0\n",
- "nob=0\n",
- "noc=0\n",
- "fp=open('H4CK3R.txt','r')\n",
- "while True:\n",
- " ch=fp.read(1)\n",
- " if not ch:\n",
- " break\n",
- " noc+=1\n",
- " if ch==' ':\n",
- " nob+=1\n",
- " if ch=='\\n':\n",
- " nol+=1\n",
- " if ch=='\\t':\n",
- " nom+=1\n",
- "fp.close()\n",
- "print \"\\n\"\n",
- "fp.close()\n",
- "print \"Number of characters=%d\\n\" % (noc)\n",
- "print \"Number of blanks=%d\\n\" % (nob)\n",
- "print \"Number of tabs=%d\\n\" % (nom)\n",
- "print \"Number of lines=%d\\n\" % (nol)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "\n",
- "\n",
- "Number of characters=162\n",
- "\n",
- "Number of blanks=21\n",
- "\n",
- "Number of tabs=3\n",
- "\n",
- "Number of lines=4\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 10
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:396-397"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Program that copies the content of one file into another\n",
- "import sys\n",
- "try:\n",
- " fs=open('H4CK3R.txt','r')\n",
- "except:\n",
- " print \"Cannot open file\"\n",
- " sys.exit(1)\n",
- "try:\n",
- " ft=open('python.txt','w')\n",
- "except:\n",
- " print \"Cannot open file\"\n",
- " fs.close()\n",
- " sys.exit(2)\n",
- "ch=fs.readlines()\n",
- "ft.writelines(ch)\n",
- "fs.close()\n",
- "ft.close()"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [],
- "prompt_number": 13
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:399"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Receives strings from keyboard and writes them to file\n",
- "try:\n",
- " fp=open('POEM.txt','w')\n",
- "except:\n",
- " print 'cannot open file'\n",
- "print \"\\nEnter a few lines of text:\\n\"\n",
- "s=' '\n",
- "while (len(s)>0):\n",
- " s=raw_input()\n",
- " fp.writelines(s)\n",
- " fp.writelines(\"\\n\")\n",
- "fp.close()"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "\n",
- "Enter a few lines of text:\n",
- "\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Shining and bright,they are forever,\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "so true about diamonds,\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "more so of memories,\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "especially yours!\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "\n"
- ]
- }
- ],
- "prompt_number": 15
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:400"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Reads strings from the file and displays them on screen\n",
- "try:\n",
- " fp=open('POEM.txt','r')\n",
- "except:\n",
- " print 'cannot open file'\n",
- "s=fp.read(99)\n",
- "print \"%s\" % (s)\n",
- "print \"\\n\"\n",
- "fp.close()"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Shining and bright,they are forever,\n",
- "so true about diamonds,\n",
- "more so of memories,\n",
- "especially yours!\n",
- "\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 16
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:401-402"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Writes records to a file using structure\n",
- "class emp():\n",
- " def __init__(self,**kwds):\n",
- " self.__dict__.update(kwds)\n",
- "another='Y'\n",
- "e=emp()\n",
- "try:\n",
- " fp=open('EMPLOYEE.txt','w')\n",
- "except:\n",
- " print 'cannot open file'\n",
- "while another=='Y':\n",
- " print \"\\nEnter name,age and basic salary:\"\n",
- " e.name=raw_input()\n",
- " e.age=eval(raw_input())\n",
- " e.bs=eval(raw_input())\n",
- " ch=\"%s %d %f\" % (e.name,e.age,e.bs)\n",
- " fp.writelines(ch)\n",
- " fp.writelines(\"\\n\")\n",
- " print \"Add another record(Y/N)\"\n",
- " another=raw_input()\n",
- "fp.close()"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "\n",
- "Enter name,age and basic salary:\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Sunil\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "34\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "1250.50\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Add another record(Y/N)\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Y\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "\n",
- "Enter name,age and basic salary:\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Sameer\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "21\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "1300.50\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Add another record(Y/N)\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Y\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "\n",
- "Enter name,age and basic salary:\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Rahul\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "34\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "1400.55\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Add another record(Y/N)\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "n\n"
- ]
- }
- ],
- "prompt_number": 17
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:403-404"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Read records from a file using structure\n",
- "class emp():\n",
- " def __init__(self,**kwds):\n",
- " self.__dict__.update(kwds)\n",
- "e=emp()\n",
- "try:\n",
- " fp=open('EMPLOYEE.txt','r') # open the file for reading\n",
- "except:\n",
- " print 'cannot open file'\n",
- "for line in fp: # iterate over each line\n",
- " e.name, e.age, e.bs = line.split() # split it by whitespace\n",
- " e.age = int(e.age) # convert age from string to int\n",
- " e.bs = float(e.bs) # convert bs from string to float\n",
- " print \"%s %d %f\\n\" %(e.name, e.age, e.bs)\n",
- "fp.close()"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Sunil 34 1250.500000\n",
- "\n",
- "Sameer 21 1300.500000\n",
- "\n",
- "Rahul 34 1400.550000\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 18
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:404-405"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#program to copy text as well as binary files\n",
- "import sys #for exit()\n",
- "try:\n",
- " fs=open('H4CK3R.txt','rb') # open the file for reading\n",
- "except:\n",
- " print 'cannot open file'\n",
- " sys.exit(1)\n",
- "try:\n",
- " ft=open('python.txt','wb') # open the file for writing\n",
- "except:\n",
- " print 'cannot open file'\n",
- " fs.close()\n",
- " sys.exit(2)\n",
- "ch=fs.readlines()\n",
- "ft.writelines(ch)\n",
- "fs.close()\n",
- "ft.close()"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [],
- "prompt_number": 20
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:407-408"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Receives records from keyboard and writes them to a file in binary mode\n",
- "class emp():\n",
- " def __init__(self,**kwds):\n",
- " self.__dict__.update(kwds)\n",
- "another='Y'\n",
- "e=emp()\n",
- "try:\n",
- " fp=open('EMP.txt','wb') # open the file for reading\n",
- "except:\n",
- " print 'cannot open file' \n",
- "while another=='Y':\n",
- " print \"\\nEnter name,age and basic salary:\"\n",
- " e.name=raw_input()\n",
- " e.age=eval(raw_input())\n",
- " e.bs=eval(raw_input())\n",
- " ch=\"%s %d %f\" % (e.name,e.age,e.bs)\n",
- " fp.writelines(ch)\n",
- " print \"Add another record (Y/N)\"\n",
- " another=raw_input()\n",
- " if another=='Y':\n",
- " fp.writelines(\"\\n\")\n",
- "fp.close()"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "\n",
- "Enter name,age and basic salary:\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Suresh\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "24\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "1250.50\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Add another record (Y/N)\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Y\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "\n",
- "Enter name,age and basic salary:\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Ranjan\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "21\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "1300.60\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Add another record (Y/N)\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Y\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "\n",
- "Enter name,age and basic salary:\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Harish\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "28\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "1400.70\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Add another record (Y/N)\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "N\n"
- ]
- }
- ],
- "prompt_number": 21
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:409"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Read records from binary file and displays them on VDU\n",
- "class emp():\n",
- " def __init__(self,**kwds):\n",
- " self.__dict__.update(kwds)\n",
- "e=emp()\n",
- "try:\n",
- " fp=open('EMP.txt','rb')\n",
- "except:\n",
- " print \"Cannot open file\"\n",
- "for line in fp:\n",
- " e.name, e.age, e.bs = line.split() # split it by whitespace\n",
- " e.age = int(e.age) # convert age from string to int\n",
- " e.bs = float(e.bs) # convert bs from string to float\n",
- " print \"%s %d %f\\n\" %(e.name, e.age, e.bs)\n",
- "fp.close()"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Suresh 24 1250.500000\n",
- "\n",
- "Ranjan 21 1300.600000\n",
- "\n",
- "Harish 28 1400.700000\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 22
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:411-414"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#A menu-driven program for elementary database management\n",
- "class emp():\n",
- " def __init__(self,**kwds):\n",
- " self.__dict__.update(kwds)\n",
- "e=emp()\n",
- "try:\n",
- " fp=open('EMP.txt','rb+') # open the file for reading\n",
- "except:\n",
- " try:\n",
- " fp=open('EMP.txt','wb+') # open the file for writing\n",
- " except:\n",
- " print 'cannot open file'\n",
- "while 1:\n",
- " print \"1.Add Records\"\n",
- " print \"2.List Records\"\n",
- " print \"3.Modify Records\"\n",
- " print \"4.Delete Records\"\n",
- " print \"0.Exit\"\n",
- " print \"Your choice\"\n",
- " choice=eval(raw_input())\n",
- " def add():\n",
- " import os\n",
- " fp.seek(0,os.SEEK_END)\n",
- " another='Y'\n",
- " while another=='Y':\n",
- " print \"\\nEnter name,age and basic salary:\"\n",
- " e.name=raw_input()\n",
- " e.age=eval(raw_input())\n",
- " e.bs=eval(raw_input())\n",
- " ch=\"%s %d %f\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\" % (e.name,e.age,e.bs)\n",
- " fp.writelines(\"\\n\")\n",
- " fp.writelines(ch)\n",
- " print \"Add another record(Y/N)\"\n",
- " another=raw_input()\n",
- " def list():\n",
- " import os\n",
- " fp.seek(0,os.SEEK_SET)\n",
- " for line in fp: # iterate over each line\n",
- " if len(line)>10:\n",
- " e.name, e.age, e.bs = line.split() # split it by whitespace\n",
- " e.age = int(e.age) # convert age from string to int\n",
- " e.bs = float(e.bs) # convert bs from string to float\n",
- " print \"%s %d %f\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\" %(e.name, e.age, e.bs)\n",
- " def modify():\n",
- " another='Y'\n",
- " while another=='Y':\n",
- " print \"\\nEnter name of employee to modify\"\n",
- " empname=raw_input()\n",
- " import os\n",
- " fp.seek(0,os.SEEK_SET)\n",
- " for line in iter(fp.readline, ''):\n",
- " if len(line)>10:\n",
- " e.name, e.age, e.bs=line.split()\n",
- " e.age = int(e.age) # convert age from string to int\n",
- " e.bs = float(e.bs)\n",
- " if(cmp(e.name,empname)==0):\n",
- " c=len(line)\n",
- " print \"\\nEnter new name,age & bs\"\n",
- " e.name=raw_input()\n",
- " e.age=eval(raw_input())\n",
- " e.bs=eval(raw_input())\n",
- " import os\n",
- " fp.seek(-c,os.SEEK_CUR)\n",
- " ch=\"%s %d %f\" % (e.name,e.age,e.bs)\n",
- " fp.writelines(\"\\n\")\n",
- " fp.writelines(ch)\n",
- " fp.writelines(\"\\n\")\n",
- " break\n",
- " print \"\\nModify another Record(Y/N)\"\n",
- " another=raw_input()\n",
- " def delete():\n",
- " another='Y'\n",
- " global fp\n",
- " while another=='Y':\n",
- " print \"\\nEnter name of employee to delete\"\n",
- " empname=raw_input()\n",
- " ft=open('TEMP.txt','wb')\n",
- " import os\n",
- " fp.seek(0,os.SEEK_SET)\n",
- " for line in fp:\n",
- " if len(line)>10:\n",
- " e.name, e.age, e.bs=line.split()\n",
- " e.age = int(e.age) # convert age from string to int\n",
- " e.bs = float(e.bs)\n",
- " if(cmp(e.name,empname)!=0):\n",
- " ch=\"%s %d %f\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\" % (e.name,e.age,e.bs)\n",
- " ft.writelines(ch)\n",
- " ft.writelines(\"\\n\")\n",
- " fp.close()\n",
- " ft.close()\n",
- " import os\n",
- " os.remove(\"EMP.txt\") # Delete file EMP.txt\n",
- " os.rename( \"TEMP.txt\", \"D:/EMP.txt\" ) # Rename a file from TEMP.txt to EMP.txt\n",
- " fp=open('EMP.txt','rb+')\n",
- " print \"Delete another record(Y/N)\"\n",
- " another=raw_input()\n",
- " def exit():\n",
- " import sys\n",
- " fp.close()\n",
- " sys.exit(0)\n",
- " def switch(c):\n",
- " return {1: add,\n",
- " 2: list,\n",
- " 3: modify,\n",
- " 4: delete,\n",
- " 0: exit,\n",
- " }[c]()\n",
- " switch(choice)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "1.Add Records\n",
- "2.List Records\n",
- "3.Modify Records\n",
- "4.Delete Records\n",
- "0.Exit\n",
- "Your choice\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "2\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Suresh 24 1250.500000\t\t\t\t\t\t\t\t\t\t\n",
- "Ranjan 21 1300.600000\t\t\t\t\t\t\t\t\t\t\n",
- "Harish 28 1400.700000\t\t\t\t\t\t\t\t\t\t\n",
- "1.Add Records\n",
- "2.List Records\n",
- "3.Modify Records\n",
- "4.Delete Records\n",
- "0.Exit\n",
- "Your choice\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "1\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "\n",
- "Enter name,age and basic salary:\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Ram\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "20\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "5000\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Add another record(Y/N)\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Y\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "\n",
- "Enter name,age and basic salary:\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "GOPAL\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "19\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "6755.5\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Add another record(Y/N)\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "N\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "1.Add Records\n",
- "2.List Records\n",
- "3.Modify Records\n",
- "4.Delete Records\n",
- "0.Exit\n",
- "Your choice\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "2\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Suresh 24 1250.500000\t\t\t\t\t\t\t\t\t\t\n",
- "Ranjan 21 1300.600000\t\t\t\t\t\t\t\t\t\t\n",
- "Harish 28 1400.700000\t\t\t\t\t\t\t\t\t\t\n",
- "Ram 20 5000.000000\t\t\t\t\t\t\t\t\t\t\n",
- "GOPAL 19 6755.500000\t\t\t\t\t\t\t\t\t\t\n",
- "1.Add Records\n",
- "2.List Records\n",
- "3.Modify Records\n",
- "4.Delete Records\n",
- "0.Exit\n",
- "Your choice\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "3\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "\n",
- "Enter name of employee to modify\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Ram\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "\n",
- "Enter new name,age & bs\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Radhey\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "15\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "4687.66\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "\n",
- "Modify another Record(Y/N)\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "N\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "1.Add Records\n",
- "2.List Records\n",
- "3.Modify Records\n",
- "4.Delete Records\n",
- "0.Exit\n",
- "Your choice\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "2\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Suresh 24 1250.500000\t\t\t\t\t\t\t\t\t\t\n",
- "Ranjan 21 1300.600000\t\t\t\t\t\t\t\t\t\t\n",
- "Harish 28 1400.700000\t\t\t\t\t\t\t\t\t\t\n",
- "Radhey 15 4687.660000\t\t\t\t\t\t\t\t\t\t\n",
- "GOPAL 19 6755.500000\t\t\t\t\t\t\t\t\t\t\n",
- "1.Add Records\n",
- "2.List Records\n",
- "3.Modify Records\n",
- "4.Delete Records\n",
- "0.Exit\n",
- "Your choice\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "4\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "\n",
- "Enter name of employee to delete\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Radhey\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Delete another record(Y/N)\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "N\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "1.Add Records\n",
- "2.List Records\n",
- "3.Modify Records\n",
- "4.Delete Records\n",
- "0.Exit\n",
- "Your choice\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "2\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Suresh 24 1250.500000\t\t\t\t\t\t\t\t\t\t\n",
- "Ranjan 21 1300.600000\t\t\t\t\t\t\t\t\t\t\n",
- "Harish 28 1400.700000\t\t\t\t\t\t\t\t\t\t\n",
- "GOPAL 19 6755.500000\t\t\t\t\t\t\t\t\t\t\n",
- "1.Add Records\n",
- "2.List Records\n",
- "3.Modify Records\n",
- "4.Delete Records\n",
- "0.Exit\n",
- "Your choice\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "4\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "\n",
- "Enter name of employee to delete\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "GOPAL\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Delete another record(Y/N)\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "N\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "1.Add Records\n",
- "2.List Records\n",
- "3.Modify Records\n",
- "4.Delete Records\n",
- "0.Exit\n",
- "Your choice\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "2\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Suresh 24 1250.500000\t\t\t\t\t\t\t\t\t\t\n",
- "Ranjan 21 1300.600000\t\t\t\t\t\t\t\t\t\t\n",
- "Harish 28 1400.700000\t\t\t\t\t\t\t\t\t\t\n",
- "1.Add Records\n",
- "2.List Records\n",
- "3.Modify Records\n",
- "4.Delete Records\n",
- "0.Exit\n",
- "Your choice\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "0\n"
- ]
- },
- {
- "ename": "SystemExit",
- "evalue": "0",
- "output_type": "pyerr",
- "traceback": [
- "An exception has occurred, use %tb to see the full traceback.\n",
- "\u001b[1;31mSystemExit\u001b[0m\u001b[1;31m:\u001b[0m 0\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stderr",
- "text": [
- "To exit: use 'exit', 'quit', or Ctrl-D.\n"
- ]
- }
- ],
- "prompt_number": 23
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:416-417"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#File-copy program which copies text,.com and .exe files\n",
- "from StringIO import StringIO\n",
- "buffer = StringIO()\n",
- "print \"\\nEnter source file name\"\n",
- "source=raw_input()\n",
- "try:\n",
- " inhandle=open(source,'rb')\n",
- "except:\n",
- " print \"Cannot open file\"\n",
- "print \"Enter target file name\"\n",
- "target=raw_input()\n",
- "try:\n",
- " outhandle=open(target,'wb')\n",
- "except:\n",
- " print \"Cannot open file\"\n",
- " inhandle.close()\n",
- "bytes=inhandle.readlines()\n",
- "buffer.write(bytes)\n",
- "outhandle.writelines(buffer.getvalue())\n",
- "buffer.close()\n",
- "inhandle.close()\n",
- "outhandle.close()"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "\n",
- "Enter source file name\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "H4CK3R.txt\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter target file name\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "python.txt\n"
- ]
- }
- ],
- "prompt_number": 24
- }
- ],
- "metadata": {}
- }
- ]
+{ + "metadata": { + "name": "", + "signature": "sha256:a886cf212382fa732d2b3df088bbcba9a65ba4a31cbe899c80e912ac04c2747f" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "CHAPTER 12:FILE INPUT/OUTPUT" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:391" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "fp=open('PR1.txt','r') #open the file for reading\n", + "ch=fp.readlines() #ch will store all content of file\n", + "print \"%s\" % (' '.join(ch)) #prints content of file\n", + "print \"\\n\"\n", + "fp.close()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "site:http://www.bzu.edu.pk/\n", + " user:siteuser@localhost\n", + " version:5.5.32-0ubuntu0.12.04.1\n", + " db: bzu\n", + " tables:\n", + " username:web,webadmin,webadministrator23\n", + " pass:71dc9f7af599450b23a3b5d54bc665c7,\t49fa1a081c8d5c8dcfa05d730803251a,\t*E8665C4049F515D836A3C8704D0543C6DA0FE96D\n", + "\n", + "\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:394" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "try:\n", + " fp=open('yoyo.txt','r')\n", + " for line in fin:\n", + " print line\n", + " fp.close()\n", + "except:\n", + " print 'cannot open file'" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "cannot open file\n" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:395" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "nol=0\n", + "nom=0\n", + "nob=0\n", + "noc=0\n", + "fp=open('H4CK3R.txt','r')\n", + "while True:\n", + " ch=fp.read(1)\n", + " if not ch:\n", + " break\n", + " noc+=1\n", + " if ch==' ':\n", + " nob+=1\n", + " if ch=='\\n':\n", + " nol+=1\n", + " if ch=='\\t':\n", + " nom+=1\n", + "fp.close()\n", + "print \"\\n\"\n", + "fp.close()\n", + "print \"Number of characters=%d\\n\" % (noc)\n", + "print \"Number of blanks=%d\\n\" % (nob)\n", + "print \"Number of tabs=%d\\n\" % (nom)\n", + "print \"Number of lines=%d\\n\" % (nol)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "\n", + "Number of characters=162\n", + "\n", + "Number of blanks=21\n", + "\n", + "Number of tabs=3\n", + "\n", + "Number of lines=4\n", + "\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:396-397" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "import sys\n", + "try:\n", + " fs=open('H4CK3R.txt','r')\n", + "except:\n", + " print \"Cannot open file\"\n", + " sys.exit(1)\n", + "try:\n", + " ft=open('python.txt','w')\n", + "except:\n", + " print \"Cannot open file\"\n", + " fs.close()\n", + " sys.exit(2)\n", + "ch=fs.readlines()\n", + "ft.writelines(ch)\n", + "fs.close()\n", + "ft.close()" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 13 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:399" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "try:\n", + " fp=open('POEM.txt','w')\n", + "except:\n", + " print 'cannot open file'\n", + "print \"\\nEnter a few lines of text:\\n\"\n", + "s=' '\n", + "while (len(s)>0):\n", + " s=raw_input()\n", + " fp.writelines(s)\n", + " fp.writelines(\"\\n\")\n", + "fp.close()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Enter a few lines of text:\n", + "\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Shining and bright,they are forever,\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "so true about diamonds,\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "more so of memories,\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "especially yours!\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n" + ] + } + ], + "prompt_number": 15 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:400" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "try:\n", + " fp=open('POEM.txt','r')\n", + "except:\n", + " print 'cannot open file'\n", + "s=fp.read(99)\n", + "print \"%s\" % (s)\n", + "print \"\\n\"\n", + "fp.close()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Shining and bright,they are forever,\n", + "so true about diamonds,\n", + "more so of memories,\n", + "especially yours!\n", + "\n", + "\n" + ] + } + ], + "prompt_number": 16 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:401-402" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "class emp():\n", + " def __init__(self,**kwds):\n", + " self.__dict__.update(kwds)\n", + "another='Y'\n", + "e=emp()\n", + "try:\n", + " fp=open('EMPLOYEE.txt','w')\n", + "except:\n", + " print 'cannot open file'\n", + "while another=='Y':\n", + " print \"\\nEnter name,age and basic salary:\"\n", + " e.name=raw_input()\n", + " e.age=eval(raw_input())\n", + " e.bs=eval(raw_input())\n", + " ch=\"%s %d %f\" % (e.name,e.age,e.bs)\n", + " fp.writelines(ch)\n", + " fp.writelines(\"\\n\")\n", + " print \"Add another record(Y/N)\"\n", + " another=raw_input()\n", + "fp.close()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Enter name,age and basic salary:\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sunil\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "34\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1250.50\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Add another record(Y/N)\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Y\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Enter name,age and basic salary:\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sameer\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "21\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1300.50\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Add another record(Y/N)\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Y\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Enter name,age and basic salary:\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Rahul\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "34\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1400.55\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Add another record(Y/N)\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "n\n" + ] + } + ], + "prompt_number": 17 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:403-404" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "class emp():\n", + " def __init__(self,**kwds):\n", + " self.__dict__.update(kwds)\n", + "e=emp()\n", + "try:\n", + " fp=open('EMPLOYEE.txt','r') # open the file for reading\n", + "except:\n", + " print 'cannot open file'\n", + "for line in fp: # iterate over each line\n", + " e.name, e.age, e.bs = line.split() # split it by whitespace\n", + " e.age = int(e.age) # convert age from string to int\n", + " e.bs = float(e.bs) # convert bs from string to float\n", + " print \"%s %d %f\\n\" %(e.name, e.age, e.bs)\n", + "fp.close()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sunil 34 1250.500000\n", + "\n", + "Sameer 21 1300.500000\n", + "\n", + "Rahul 34 1400.550000\n", + "\n" + ] + } + ], + "prompt_number": 18 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:404-405" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "import sys #for exit()\n", + "try:\n", + " fs=open('H4CK3R.txt','rb') # open the file for reading\n", + "except:\n", + " print 'cannot open file'\n", + " sys.exit(1)\n", + "try:\n", + " ft=open('python.txt','wb') # open the file for writing\n", + "except:\n", + " print 'cannot open file'\n", + " fs.close()\n", + " sys.exit(2)\n", + "ch=fs.readlines()\n", + "ft.writelines(ch)\n", + "fs.close()\n", + "ft.close()" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 20 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:407-408" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "class emp():\n", + " def __init__(self,**kwds):\n", + " self.__dict__.update(kwds)\n", + "another='Y'\n", + "e=emp()\n", + "try:\n", + " fp=open('EMP.txt','wb') # open the file for reading\n", + "except:\n", + " print 'cannot open file' \n", + "while another=='Y':\n", + " print \"\\nEnter name,age and basic salary:\"\n", + " e.name=raw_input()\n", + " e.age=eval(raw_input())\n", + " e.bs=eval(raw_input())\n", + " ch=\"%s %d %f\" % (e.name,e.age,e.bs)\n", + " fp.writelines(ch)\n", + " print \"Add another record (Y/N)\"\n", + " another=raw_input()\n", + " if another=='Y':\n", + " fp.writelines(\"\\n\")\n", + "fp.close()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Enter name,age and basic salary:\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Suresh\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "24\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1250.50\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Add another record (Y/N)\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Y\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Enter name,age and basic salary:\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Ranjan\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "21\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1300.60\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Add another record (Y/N)\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Y\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Enter name,age and basic salary:\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Harish\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "28\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1400.70\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Add another record (Y/N)\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "N\n" + ] + } + ], + "prompt_number": 21 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:409" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "class emp():\n", + " def __init__(self,**kwds):\n", + " self.__dict__.update(kwds)\n", + "e=emp()\n", + "try:\n", + " fp=open('EMP.txt','rb')\n", + "except:\n", + " print \"Cannot open file\"\n", + "for line in fp:\n", + " e.name, e.age, e.bs = line.split() # split it by whitespace\n", + " e.age = int(e.age) # convert age from string to int\n", + " e.bs = float(e.bs) # convert bs from string to float\n", + " print \"%s %d %f\\n\" %(e.name, e.age, e.bs)\n", + "fp.close()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Suresh 24 1250.500000\n", + "\n", + "Ranjan 21 1300.600000\n", + "\n", + "Harish 28 1400.700000\n", + "\n" + ] + } + ], + "prompt_number": 22 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:411-414" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "class emp():\n", + " def __init__(self,**kwds):\n", + " self.__dict__.update(kwds)\n", + "e=emp()\n", + "try:\n", + " fp=open('EMP.txt','rb+') # open the file for reading\n", + "except:\n", + " try:\n", + " fp=open('EMP.txt','wb+') # open the file for writing\n", + " except:\n", + " print 'cannot open file'\n", + "while 1:\n", + " print \"1.Add Records\"\n", + " print \"2.List Records\"\n", + " print \"3.Modify Records\"\n", + " print \"4.Delete Records\"\n", + " print \"0.Exit\"\n", + " print \"Your choice\"\n", + " choice=eval(raw_input())\n", + " def add():\n", + " import os\n", + " fp.seek(0,os.SEEK_END)\n", + " another='Y'\n", + " while another=='Y':\n", + " print \"\\nEnter name,age and basic salary:\"\n", + " e.name=raw_input()\n", + " e.age=eval(raw_input())\n", + " e.bs=eval(raw_input())\n", + " ch=\"%s %d %f\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\" % (e.name,e.age,e.bs)\n", + " fp.writelines(\"\\n\")\n", + " fp.writelines(ch)\n", + " print \"Add another record(Y/N)\"\n", + " another=raw_input()\n", + " def list():\n", + " import os\n", + " fp.seek(0,os.SEEK_SET)\n", + " for line in fp: # iterate over each line\n", + " if len(line)>10:\n", + " e.name, e.age, e.bs = line.split() # split it by whitespace\n", + " e.age = int(e.age) # convert age from string to int\n", + " e.bs = float(e.bs) # convert bs from string to float\n", + " print \"%s %d %f\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\" %(e.name, e.age, e.bs)\n", + " def modify():\n", + " another='Y'\n", + " while another=='Y':\n", + " print \"\\nEnter name of employee to modify\"\n", + " empname=raw_input()\n", + " import os\n", + " fp.seek(0,os.SEEK_SET)\n", + " for line in iter(fp.readline, ''):\n", + " if len(line)>10:\n", + " e.name, e.age, e.bs=line.split()\n", + " e.age = int(e.age) # convert age from string to int\n", + " e.bs = float(e.bs)\n", + " if(cmp(e.name,empname)==0):\n", + " c=len(line)\n", + " print \"\\nEnter new name,age & bs\"\n", + " e.name=raw_input()\n", + " e.age=eval(raw_input())\n", + " e.bs=eval(raw_input())\n", + " import os\n", + " fp.seek(-c,os.SEEK_CUR)\n", + " ch=\"%s %d %f\" % (e.name,e.age,e.bs)\n", + " fp.writelines(\"\\n\")\n", + " fp.writelines(ch)\n", + " fp.writelines(\"\\n\")\n", + " break\n", + " print \"\\nModify another Record(Y/N)\"\n", + " another=raw_input()\n", + " def delete():\n", + " another='Y'\n", + " global fp\n", + " while another=='Y':\n", + " print \"\\nEnter name of employee to delete\"\n", + " empname=raw_input()\n", + " ft=open('TEMP.txt','wb')\n", + " import os\n", + " fp.seek(0,os.SEEK_SET)\n", + " for line in fp:\n", + " if len(line)>10:\n", + " e.name, e.age, e.bs=line.split()\n", + " e.age = int(e.age) # convert age from string to int\n", + " e.bs = float(e.bs)\n", + " if(cmp(e.name,empname)!=0):\n", + " ch=\"%s %d %f\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\" % (e.name,e.age,e.bs)\n", + " ft.writelines(ch)\n", + " ft.writelines(\"\\n\")\n", + " fp.close()\n", + " ft.close()\n", + " import os\n", + " os.remove(\"EMP.txt\") # Delete file EMP.txt\n", + " os.rename( \"TEMP.txt\", \"D:/EMP.txt\" ) # Rename a file from TEMP.txt to EMP.txt\n", + " fp=open('EMP.txt','rb+')\n", + " print \"Delete another record(Y/N)\"\n", + " another=raw_input()\n", + " def exit():\n", + " import sys\n", + " fp.close()\n", + " sys.exit(0)\n", + " def switch(c):\n", + " return {1: add,\n", + " 2: list,\n", + " 3: modify,\n", + " 4: delete,\n", + " 0: exit,\n", + " }[c]()\n", + " switch(choice)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "1.Add Records\n", + "2.List Records\n", + "3.Modify Records\n", + "4.Delete Records\n", + "0.Exit\n", + "Your choice\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Suresh 24 1250.500000\t\t\t\t\t\t\t\t\t\t\n", + "Ranjan 21 1300.600000\t\t\t\t\t\t\t\t\t\t\n", + "Harish 28 1400.700000\t\t\t\t\t\t\t\t\t\t\n", + "1.Add Records\n", + "2.List Records\n", + "3.Modify Records\n", + "4.Delete Records\n", + "0.Exit\n", + "Your choice\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Enter name,age and basic salary:\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Ram\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "20\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5000\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Add another record(Y/N)\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Y\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Enter name,age and basic salary:\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "GOPAL\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "19\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "6755.5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Add another record(Y/N)\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "N\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "1.Add Records\n", + "2.List Records\n", + "3.Modify Records\n", + "4.Delete Records\n", + "0.Exit\n", + "Your choice\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Suresh 24 1250.500000\t\t\t\t\t\t\t\t\t\t\n", + "Ranjan 21 1300.600000\t\t\t\t\t\t\t\t\t\t\n", + "Harish 28 1400.700000\t\t\t\t\t\t\t\t\t\t\n", + "Ram 20 5000.000000\t\t\t\t\t\t\t\t\t\t\n", + "GOPAL 19 6755.500000\t\t\t\t\t\t\t\t\t\t\n", + "1.Add Records\n", + "2.List Records\n", + "3.Modify Records\n", + "4.Delete Records\n", + "0.Exit\n", + "Your choice\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Enter name of employee to modify\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Ram\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Enter new name,age & bs\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Radhey\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "15\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4687.66\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Modify another Record(Y/N)\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "N\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "1.Add Records\n", + "2.List Records\n", + "3.Modify Records\n", + "4.Delete Records\n", + "0.Exit\n", + "Your choice\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Suresh 24 1250.500000\t\t\t\t\t\t\t\t\t\t\n", + "Ranjan 21 1300.600000\t\t\t\t\t\t\t\t\t\t\n", + "Harish 28 1400.700000\t\t\t\t\t\t\t\t\t\t\n", + "Radhey 15 4687.660000\t\t\t\t\t\t\t\t\t\t\n", + "GOPAL 19 6755.500000\t\t\t\t\t\t\t\t\t\t\n", + "1.Add Records\n", + "2.List Records\n", + "3.Modify Records\n", + "4.Delete Records\n", + "0.Exit\n", + "Your choice\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Enter name of employee to delete\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Radhey\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Delete another record(Y/N)\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "N\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "1.Add Records\n", + "2.List Records\n", + "3.Modify Records\n", + "4.Delete Records\n", + "0.Exit\n", + "Your choice\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Suresh 24 1250.500000\t\t\t\t\t\t\t\t\t\t\n", + "Ranjan 21 1300.600000\t\t\t\t\t\t\t\t\t\t\n", + "Harish 28 1400.700000\t\t\t\t\t\t\t\t\t\t\n", + "GOPAL 19 6755.500000\t\t\t\t\t\t\t\t\t\t\n", + "1.Add Records\n", + "2.List Records\n", + "3.Modify Records\n", + "4.Delete Records\n", + "0.Exit\n", + "Your choice\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Enter name of employee to delete\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "GOPAL\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Delete another record(Y/N)\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "N\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "1.Add Records\n", + "2.List Records\n", + "3.Modify Records\n", + "4.Delete Records\n", + "0.Exit\n", + "Your choice\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Suresh 24 1250.500000\t\t\t\t\t\t\t\t\t\t\n", + "Ranjan 21 1300.600000\t\t\t\t\t\t\t\t\t\t\n", + "Harish 28 1400.700000\t\t\t\t\t\t\t\t\t\t\n", + "1.Add Records\n", + "2.List Records\n", + "3.Modify Records\n", + "4.Delete Records\n", + "0.Exit\n", + "Your choice\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "0\n" + ] + }, + { + "ename": "SystemExit", + "evalue": "0", + "output_type": "pyerr", + "traceback": [ + "An exception has occurred, use %tb to see the full traceback.\n", + "\u001b[1;31mSystemExit\u001b[0m\u001b[1;31m:\u001b[0m 0\n" + ] + }, + { + "output_type": "stream", + "stream": "stderr", + "text": [ + "To exit: use 'exit', 'quit', or Ctrl-D.\n" + ] + } + ], + "prompt_number": 23 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:416-417" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "from StringIO import StringIO\n", + "buffer = StringIO()\n", + "print \"\\nEnter source file name\"\n", + "source=raw_input()\n", + "try:\n", + " inhandle=open(source,'rb')\n", + "except:\n", + " print \"Cannot open file\"\n", + "print \"Enter target file name\"\n", + "target=raw_input()\n", + "try:\n", + " outhandle=open(target,'wb')\n", + "except:\n", + " print \"Cannot open file\"\n", + " inhandle.close()\n", + "bytes=inhandle.readlines()\n", + "buffer.write(bytes)\n", + "outhandle.writelines(buffer.getvalue())\n", + "buffer.close()\n", + "inhandle.close()\n", + "outhandle.close()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Enter source file name\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "H4CK3R.txt\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter target file name\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "python.txt\n" + ] + } + ], + "prompt_number": 24 + } + ], + "metadata": {} + } + ] }
\ No newline at end of file diff --git a/ANSI_C_Programming/chapter13.ipynb b/ANSI_C_Programming/chapter13.ipynb index a481e2c4..b37b75e4 100644 --- a/ANSI_C_Programming/chapter13.ipynb +++ b/ANSI_C_Programming/chapter13.ipynb @@ -1,712 +1,711 @@ -{
- "metadata": {
- "name": "chapter13.ipynb"
- },
- "nbformat": 3,
- "nbformat_minor": 0,
- "worksheets": [
- {
- "cells": [
- {
- "cell_type": "heading",
- "level": 1,
- "metadata": {},
- "source": [
- "CHAPTER 13: MORE ISSUES IN INPUT/OUTPUT"
- ]
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:434-435"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#using argc & argv to copy one file into another\n",
- "import sys\n",
- "if(len(sys.argv)!=3):\n",
- " print \"Improper number of arguments\\n\"\n",
- " sys.exit(0)\n",
- "try:\n",
- " fs=open(sys.argv[1], \"r\")\n",
- "except:\n",
- " sys.exit(\"Cannot open source file\\n\")\n",
- "try:\n",
- " ft=open(sys.argv[2], \"w\")\n",
- "except:\n",
- " fs.close()\n",
- " sys.exit(\"Cannot open source file\\n\")\n",
- "ch=fs.readlines()\n",
- "ft.writelines(ch)\n",
- "fs.close()\n",
- "ft.close()"
- ],
- "language": "python",
- "metadata": {},
- "outputs": []
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:437-438"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Detecting errors in reading/writing\n",
- "try:\n",
- " fp=open('python.txt','w')\n",
- " while True:\n",
- " ch=fp.read(1)\n",
- " if not ch:\n",
- " break;\n",
- " print \"%c\" % (ch)\n",
- "except:\n",
- " print \"Error in reading file\\n\" "
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Error in reading file\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 15
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:439"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Prints file contents on printer\n",
- "#there is no standard way to print using Python on all platforms\n",
- "#It will work on windows platform\n",
- "import tempfile\n",
- "import win32api\n",
- "import win32print\n",
- "import sys\n",
- "\n",
- "try:\n",
- " fp=open('poem.txt','r') #No need to open it\n",
- "except:\n",
- " sys.exit(\"Cannot open file\\n\")\n",
- "win32api.ShellExecute (\n",
- " 0,\n",
- " \"print\",\n",
- " \"poem.txt\",\n",
- " '/d:\"%s\"' % win32print.GetDefaultPrinter (),\n",
- " \".\",\n",
- " 0\n",
- ")\n",
- "fp.close()"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "metadata": {},
- "output_type": "pyout",
- "prompt_number": 3,
- "text": [
- "42"
- ]
- }
- ],
- "prompt_number": 3
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:440-441"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#redirecting the output from the screen to a file\n",
- "import sys\n",
- "ch = sys.stdin.read(1)\n",
- "while (len(ch)>0):\n",
- " sys.stdout.write(ch)\n",
- " ch = sys.stdin.read(1)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [],
- "prompt_number": 11
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:442"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#program for generating the ASCII table on screen\n",
- "for ch in range(0,256,1):\n",
- " print \"%d %c\\n\" % (ch,ch)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "0 \u0000\n",
- "\n",
- "1 \u0001\n",
- "\n",
- "2 \u0002\n",
- "\n",
- "3 \u0003\n",
- "\n",
- "4 \u0004\n",
- "\n",
- "5 \u0005\n",
- "\n",
- "6 \u0006\n",
- "\n",
- "7 \u0007\n",
- "\n",
- "8 \b\n",
- "\n",
- "9 \t\n",
- "\n",
- "10 \n",
- "\n",
- "\n",
- "11 \u000b",
- "\n",
- "\n",
- "12 \f",
- "\n",
- "\n",
- "13 \r\n",
- "\n",
- "14 \u000e\n",
- "\n",
- "15 \u000f\n",
- "\n",
- "16 \u0010\n",
- "\n",
- "17 \u0011\n",
- "\n",
- "18 \u0012\n",
- "\n",
- "19 \u0013\n",
- "\n",
- "20 \u0014\n",
- "\n",
- "21 \u0015\n",
- "\n",
- "22 \u0016\n",
- "\n",
- "23 \u0017\n",
- "\n",
- "24 \u0018\n",
- "\n",
- "25 \u0019\n",
- "\n",
- "26 \u001a\n",
- "\n",
- "27 \u001b\n",
- "\n",
- "28 \u001c",
- "\n",
- "\n",
- "29 \u001d",
- "\n",
- "\n",
- "30 \u001e",
- "\n",
- "\n",
- "31 \u001f\n",
- "\n",
- "32 \n",
- "\n",
- "33 !\n",
- "\n",
- "34 \"\n",
- "\n",
- "35 #\n",
- "\n",
- "36 $\n",
- "\n",
- "37 %\n",
- "\n",
- "38 &\n",
- "\n",
- "39 '\n",
- "\n",
- "40 (\n",
- "\n",
- "41 )\n",
- "\n",
- "42 *\n",
- "\n",
- "43 +\n",
- "\n",
- "44 ,\n",
- "\n",
- "45 -\n",
- "\n",
- "46 .\n",
- "\n",
- "47 /\n",
- "\n",
- "48 0\n",
- "\n",
- "49 1\n",
- "\n",
- "50 2\n",
- "\n",
- "51 3\n",
- "\n",
- "52 4\n",
- "\n",
- "53 5\n",
- "\n",
- "54 6\n",
- "\n",
- "55 7\n",
- "\n",
- "56 8\n",
- "\n",
- "57 9\n",
- "\n",
- "58 :\n",
- "\n",
- "59 ;\n",
- "\n",
- "60 <\n",
- "\n",
- "61 =\n",
- "\n",
- "62 >\n",
- "\n",
- "63 ?\n",
- "\n",
- "64 @\n",
- "\n",
- "65 A\n",
- "\n",
- "66 B\n",
- "\n",
- "67 C\n",
- "\n",
- "68 D\n",
- "\n",
- "69 E\n",
- "\n",
- "70 F\n",
- "\n",
- "71 G\n",
- "\n",
- "72 H\n",
- "\n",
- "73 I\n",
- "\n",
- "74 J\n",
- "\n",
- "75 K\n",
- "\n",
- "76 L\n",
- "\n",
- "77 M\n",
- "\n",
- "78 N\n",
- "\n",
- "79 O\n",
- "\n",
- "80 P\n",
- "\n",
- "81 Q\n",
- "\n",
- "82 R\n",
- "\n",
- "83 S\n",
- "\n",
- "84 T\n",
- "\n",
- "85 U\n",
- "\n",
- "86 V\n",
- "\n",
- "87 W\n",
- "\n",
- "88 X\n",
- "\n",
- "89 Y\n",
- "\n",
- "90 Z\n",
- "\n",
- "91 [\n",
- "\n",
- "92 \\\n",
- "\n",
- "93 ]\n",
- "\n",
- "94 ^\n",
- "\n",
- "95 _\n",
- "\n",
- "96 `\n",
- "\n",
- "97 a\n",
- "\n",
- "98 b\n",
- "\n",
- "99 c\n",
- "\n",
- "100 d\n",
- "\n",
- "101 e\n",
- "\n",
- "102 f\n",
- "\n",
- "103 g\n",
- "\n",
- "104 h\n",
- "\n",
- "105 i\n",
- "\n",
- "106 j\n",
- "\n",
- "107 k\n",
- "\n",
- "108 l\n",
- "\n",
- "109 m\n",
- "\n",
- "110 n\n",
- "\n",
- "111 o\n",
- "\n",
- "112 p\n",
- "\n",
- "113 q\n",
- "\n",
- "114 r\n",
- "\n",
- "115 s\n",
- "\n",
- "116 t\n",
- "\n",
- "117 u\n",
- "\n",
- "118 v\n",
- "\n",
- "119 w\n",
- "\n",
- "120 x\n",
- "\n",
- "121 y\n",
- "\n",
- "122 z\n",
- "\n",
- "123 {\n",
- "\n",
- "124 |\n",
- "\n",
- "125 }\n",
- "\n",
- "126 ~\n",
- "\n",
- "127 \u007f\n",
- "\n",
- "128 \ufffd\n",
- "\n",
- "129 \ufffd\n",
- "\n",
- "130 \ufffd\n",
- "\n",
- "131 \ufffd\n",
- "\n",
- "132 \ufffd\n",
- "\n",
- "133 \ufffd\n",
- "\n",
- "134 \ufffd\n",
- "\n",
- "135 \ufffd\n",
- "\n",
- "136 \ufffd\n",
- "\n",
- "137 \ufffd\n",
- "\n",
- "138 \ufffd\n",
- "\n",
- "139 \ufffd\n",
- "\n",
- "140 \ufffd\n",
- "\n",
- "141 \ufffd\n",
- "\n",
- "142 \ufffd\n",
- "\n",
- "143 \ufffd\n",
- "\n",
- "144 \ufffd\n",
- "\n",
- "145 \ufffd\n",
- "\n",
- "146 \ufffd\n",
- "\n",
- "147 \ufffd\n",
- "\n",
- "148 \ufffd\n",
- "\n",
- "149 \ufffd\n",
- "\n",
- "150 \ufffd\n",
- "\n",
- "151 \ufffd\n",
- "\n",
- "152 \ufffd\n",
- "\n",
- "153 \ufffd\n",
- "\n",
- "154 \ufffd\n",
- "\n",
- "155 \ufffd\n",
- "\n",
- "156 \ufffd\n",
- "\n",
- "157 \ufffd\n",
- "\n",
- "158 \ufffd\n",
- "\n",
- "159 \ufffd\n",
- "\n",
- "160 \ufffd\n",
- "\n",
- "161 \ufffd\n",
- "\n",
- "162 \ufffd\n",
- "\n",
- "163 \ufffd\n",
- "\n",
- "164 \ufffd\n",
- "\n",
- "165 \ufffd\n",
- "\n",
- "166 \ufffd\n",
- "\n",
- "167 \ufffd\n",
- "\n",
- "168 \ufffd\n",
- "\n",
- "169 \ufffd\n",
- "\n",
- "170 \ufffd\n",
- "\n",
- "171 \ufffd\n",
- "\n",
- "172 \ufffd\n",
- "\n",
- "173 \ufffd\n",
- "\n",
- "174 \ufffd\n",
- "\n",
- "175 \ufffd\n",
- "\n",
- "176 \ufffd\n",
- "\n",
- "177 \ufffd\n",
- "\n",
- "178 \ufffd\n",
- "\n",
- "179 \ufffd\n",
- "\n",
- "180 \ufffd\n",
- "\n",
- "181 \ufffd\n",
- "\n",
- "182 \ufffd\n",
- "\n",
- "183 \ufffd\n",
- "\n",
- "184 \ufffd\n",
- "\n",
- "185 \ufffd\n",
- "\n",
- "186 \ufffd\n",
- "\n",
- "187 \ufffd\n",
- "\n",
- "188 \ufffd\n",
- "\n",
- "189 \ufffd\n",
- "\n",
- "190 \ufffd\n",
- "\n",
- "191 \ufffd\n",
- "\n",
- "192 \ufffd\n",
- "\n",
- "193 \ufffd\n",
- "\n",
- "194 \ufffd\n",
- "\n",
- "195 \ufffd\n",
- "\n",
- "196 \ufffd\n",
- "\n",
- "197 \ufffd\n",
- "\n",
- "198 \ufffd\n",
- "\n",
- "199 \ufffd\n",
- "\n",
- "200 \ufffd\n",
- "\n",
- "201 \ufffd\n",
- "\n",
- "202 \ufffd\n",
- "\n",
- "203 \ufffd\n",
- "\n",
- "204 \ufffd\n",
- "\n",
- "205 \ufffd\n",
- "\n",
- "206 \ufffd\n",
- "\n",
- "207 \ufffd\n",
- "\n",
- "208 \ufffd\n",
- "\n",
- "209 \ufffd\n",
- "\n",
- "210 \ufffd\n",
- "\n",
- "211 \ufffd\n",
- "\n",
- "212 \ufffd\n",
- "\n",
- "213 \ufffd\n",
- "\n",
- "214 \ufffd\n",
- "\n",
- "215 \ufffd\n",
- "\n",
- "216 \ufffd\n",
- "\n",
- "217 \ufffd\n",
- "\n",
- "218 \ufffd\n",
- "\n",
- "219 \ufffd\n",
- "\n",
- "220 \ufffd\n",
- "\n",
- "221 \ufffd\n",
- "\n",
- "222 \ufffd\n",
- "\n",
- "223 \ufffd\n",
- "\n",
- "224 \ufffd\n",
- "\n",
- "225 \ufffd\n",
- "\n",
- "226 \ufffd\n",
- "\n",
- "227 \ufffd\n",
- "\n",
- "228 \ufffd\n",
- "\n",
- "229 \ufffd\n",
- "\n",
- "230 \ufffd\n",
- "\n",
- "231 \ufffd\n",
- "\n",
- "232 \ufffd\n",
- "\n",
- "233 \ufffd\n",
- "\n",
- "234 \ufffd\n",
- "\n",
- "235 \ufffd\n",
- "\n",
- "236 \ufffd\n",
- "\n",
- "237 \ufffd\n",
- "\n",
- "238 \ufffd\n",
- "\n",
- "239 \ufffd\n",
- "\n",
- "240 \ufffd\n",
- "\n",
- "241 \ufffd\n",
- "\n",
- "242 \ufffd\n",
- "\n",
- "243 \ufffd\n",
- "\n",
- "244 \ufffd\n",
- "\n",
- "245 \ufffd\n",
- "\n",
- "246 \ufffd\n",
- "\n",
- "247 \ufffd\n",
- "\n",
- "248 \ufffd\n",
- "\n",
- "249 \ufffd\n",
- "\n",
- "250 \ufffd\n",
- "\n",
- "251 \ufffd\n",
- "\n",
- "252 \ufffd\n",
- "\n",
- "253 \ufffd\n",
- "\n",
- "254 \ufffd\n",
- "\n",
- "255 \ufffd\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 1
- }
- ],
- "metadata": {}
- }
- ]
+{ + "metadata": { + "name": "", + "signature": "sha256:af78c3ae74c9d65bb5213be5077524875e7de56ebf2616c80d19baf13507c5b4" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "CHAPTER 13: MORE ISSUES IN INPUT/OUTPUT" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:434-435" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "import sys\n", + "if(len(sys.argv)!=3):\n", + " print \"Improper number of arguments\\n\"\n", + " sys.exit(0)\n", + "try:\n", + " fs=open(sys.argv[1], \"r\")\n", + "except:\n", + " sys.exit(\"Cannot open source file\\n\")\n", + "try:\n", + " ft=open(sys.argv[2], \"w\")\n", + "except:\n", + " fs.close()\n", + " sys.exit(\"Cannot open source file\\n\")\n", + "ch=fs.readlines()\n", + "ft.writelines(ch)\n", + "fs.close()\n", + "ft.close()" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:437-438" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "try:\n", + " fp=open('python.txt','w')\n", + " while True:\n", + " ch=fp.read(1)\n", + " if not ch:\n", + " break;\n", + " print \"%c\" % (ch)\n", + "except:\n", + " print \"Error in reading file\\n\" " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Error in reading file\n", + "\n" + ] + } + ], + "prompt_number": 15 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:439" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "import tempfile\n", + "import win32api\n", + "import win32print\n", + "import sys\n", + "\n", + "try:\n", + " fp=open('poem.txt','r') #No need to open it\n", + "except:\n", + " sys.exit(\"Cannot open file\\n\")\n", + "win32api.ShellExecute (\n", + " 0,\n", + " \"print\",\n", + " \"poem.txt\",\n", + " '/d:\"%s\"' % win32print.GetDefaultPrinter (),\n", + " \".\",\n", + " 0\n", + ")\n", + "fp.close()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 3, + "text": [ + "42" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:440-441" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "import sys\n", + "ch = sys.stdin.read(1)\n", + "while (len(ch)>0):\n", + " sys.stdout.write(ch)\n", + " ch = sys.stdin.read(1)" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 11 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:442" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "for ch in range(0,256,1):\n", + " print \"%d %c\\n\" % (ch,ch)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "0 \u0000\n", + "\n", + "1 \u0001\n", + "\n", + "2 \u0002\n", + "\n", + "3 \u0003\n", + "\n", + "4 \u0004\n", + "\n", + "5 \u0005\n", + "\n", + "6 \u0006\n", + "\n", + "7 \u0007\n", + "\n", + "8 \b\n", + "\n", + "9 \t\n", + "\n", + "10 \n", + "\n", + "\n", + "11 \u000b", + "\n", + "\n", + "12 \f", + "\n", + "\n", + "13 \r\n", + "\n", + "14 \u000e\n", + "\n", + "15 \u000f\n", + "\n", + "16 \u0010\n", + "\n", + "17 \u0011\n", + "\n", + "18 \u0012\n", + "\n", + "19 \u0013\n", + "\n", + "20 \u0014\n", + "\n", + "21 \u0015\n", + "\n", + "22 \u0016\n", + "\n", + "23 \u0017\n", + "\n", + "24 \u0018\n", + "\n", + "25 \u0019\n", + "\n", + "26 \u001a\n", + "\n", + "27 \u001b\n", + "\n", + "28 \u001c", + "\n", + "\n", + "29 \u001d", + "\n", + "\n", + "30 \u001e", + "\n", + "\n", + "31 \u001f\n", + "\n", + "32 \n", + "\n", + "33 !\n", + "\n", + "34 \"\n", + "\n", + "35 #\n", + "\n", + "36 $\n", + "\n", + "37 %\n", + "\n", + "38 &\n", + "\n", + "39 '\n", + "\n", + "40 (\n", + "\n", + "41 )\n", + "\n", + "42 *\n", + "\n", + "43 +\n", + "\n", + "44 ,\n", + "\n", + "45 -\n", + "\n", + "46 .\n", + "\n", + "47 /\n", + "\n", + "48 0\n", + "\n", + "49 1\n", + "\n", + "50 2\n", + "\n", + "51 3\n", + "\n", + "52 4\n", + "\n", + "53 5\n", + "\n", + "54 6\n", + "\n", + "55 7\n", + "\n", + "56 8\n", + "\n", + "57 9\n", + "\n", + "58 :\n", + "\n", + "59 ;\n", + "\n", + "60 <\n", + "\n", + "61 =\n", + "\n", + "62 >\n", + "\n", + "63 ?\n", + "\n", + "64 @\n", + "\n", + "65 A\n", + "\n", + "66 B\n", + "\n", + "67 C\n", + "\n", + "68 D\n", + "\n", + "69 E\n", + "\n", + "70 F\n", + "\n", + "71 G\n", + "\n", + "72 H\n", + "\n", + "73 I\n", + "\n", + "74 J\n", + "\n", + "75 K\n", + "\n", + "76 L\n", + "\n", + "77 M\n", + "\n", + "78 N\n", + "\n", + "79 O\n", + "\n", + "80 P\n", + "\n", + "81 Q\n", + "\n", + "82 R\n", + "\n", + "83 S\n", + "\n", + "84 T\n", + "\n", + "85 U\n", + "\n", + "86 V\n", + "\n", + "87 W\n", + "\n", + "88 X\n", + "\n", + "89 Y\n", + "\n", + "90 Z\n", + "\n", + "91 [\n", + "\n", + "92 \\\n", + "\n", + "93 ]\n", + "\n", + "94 ^\n", + "\n", + "95 _\n", + "\n", + "96 `\n", + "\n", + "97 a\n", + "\n", + "98 b\n", + "\n", + "99 c\n", + "\n", + "100 d\n", + "\n", + "101 e\n", + "\n", + "102 f\n", + "\n", + "103 g\n", + "\n", + "104 h\n", + "\n", + "105 i\n", + "\n", + "106 j\n", + "\n", + "107 k\n", + "\n", + "108 l\n", + "\n", + "109 m\n", + "\n", + "110 n\n", + "\n", + "111 o\n", + "\n", + "112 p\n", + "\n", + "113 q\n", + "\n", + "114 r\n", + "\n", + "115 s\n", + "\n", + "116 t\n", + "\n", + "117 u\n", + "\n", + "118 v\n", + "\n", + "119 w\n", + "\n", + "120 x\n", + "\n", + "121 y\n", + "\n", + "122 z\n", + "\n", + "123 {\n", + "\n", + "124 |\n", + "\n", + "125 }\n", + "\n", + "126 ~\n", + "\n", + "127 \u007f\n", + "\n", + "128 \ufffd\n", + "\n", + "129 \ufffd\n", + "\n", + "130 \ufffd\n", + "\n", + "131 \ufffd\n", + "\n", + "132 \ufffd\n", + "\n", + "133 \ufffd\n", + "\n", + "134 \ufffd\n", + "\n", + "135 \ufffd\n", + "\n", + "136 \ufffd\n", + "\n", + "137 \ufffd\n", + "\n", + "138 \ufffd\n", + "\n", + "139 \ufffd\n", + "\n", + "140 \ufffd\n", + "\n", + "141 \ufffd\n", + "\n", + "142 \ufffd\n", + "\n", + "143 \ufffd\n", + "\n", + "144 \ufffd\n", + "\n", + "145 \ufffd\n", + "\n", + "146 \ufffd\n", + "\n", + "147 \ufffd\n", + "\n", + "148 \ufffd\n", + "\n", + "149 \ufffd\n", + "\n", + "150 \ufffd\n", + "\n", + "151 \ufffd\n", + "\n", + "152 \ufffd\n", + "\n", + "153 \ufffd\n", + "\n", + "154 \ufffd\n", + "\n", + "155 \ufffd\n", + "\n", + "156 \ufffd\n", + "\n", + "157 \ufffd\n", + "\n", + "158 \ufffd\n", + "\n", + "159 \ufffd\n", + "\n", + "160 \ufffd\n", + "\n", + "161 \ufffd\n", + "\n", + "162 \ufffd\n", + "\n", + "163 \ufffd\n", + "\n", + "164 \ufffd\n", + "\n", + "165 \ufffd\n", + "\n", + "166 \ufffd\n", + "\n", + "167 \ufffd\n", + "\n", + "168 \ufffd\n", + "\n", + "169 \ufffd\n", + "\n", + "170 \ufffd\n", + "\n", + "171 \ufffd\n", + "\n", + "172 \ufffd\n", + "\n", + "173 \ufffd\n", + "\n", + "174 \ufffd\n", + "\n", + "175 \ufffd\n", + "\n", + "176 \ufffd\n", + "\n", + "177 \ufffd\n", + "\n", + "178 \ufffd\n", + "\n", + "179 \ufffd\n", + "\n", + "180 \ufffd\n", + "\n", + "181 \ufffd\n", + "\n", + "182 \ufffd\n", + "\n", + "183 \ufffd\n", + "\n", + "184 \ufffd\n", + "\n", + "185 \ufffd\n", + "\n", + "186 \ufffd\n", + "\n", + "187 \ufffd\n", + "\n", + "188 \ufffd\n", + "\n", + "189 \ufffd\n", + "\n", + "190 \ufffd\n", + "\n", + "191 \ufffd\n", + "\n", + "192 \ufffd\n", + "\n", + "193 \ufffd\n", + "\n", + "194 \ufffd\n", + "\n", + "195 \ufffd\n", + "\n", + "196 \ufffd\n", + "\n", + "197 \ufffd\n", + "\n", + "198 \ufffd\n", + "\n", + "199 \ufffd\n", + "\n", + "200 \ufffd\n", + "\n", + "201 \ufffd\n", + "\n", + "202 \ufffd\n", + "\n", + "203 \ufffd\n", + "\n", + "204 \ufffd\n", + "\n", + "205 \ufffd\n", + "\n", + "206 \ufffd\n", + "\n", + "207 \ufffd\n", + "\n", + "208 \ufffd\n", + "\n", + "209 \ufffd\n", + "\n", + "210 \ufffd\n", + "\n", + "211 \ufffd\n", + "\n", + "212 \ufffd\n", + "\n", + "213 \ufffd\n", + "\n", + "214 \ufffd\n", + "\n", + "215 \ufffd\n", + "\n", + "216 \ufffd\n", + "\n", + "217 \ufffd\n", + "\n", + "218 \ufffd\n", + "\n", + "219 \ufffd\n", + "\n", + "220 \ufffd\n", + "\n", + "221 \ufffd\n", + "\n", + "222 \ufffd\n", + "\n", + "223 \ufffd\n", + "\n", + "224 \ufffd\n", + "\n", + "225 \ufffd\n", + "\n", + "226 \ufffd\n", + "\n", + "227 \ufffd\n", + "\n", + "228 \ufffd\n", + "\n", + "229 \ufffd\n", + "\n", + "230 \ufffd\n", + "\n", + "231 \ufffd\n", + "\n", + "232 \ufffd\n", + "\n", + "233 \ufffd\n", + "\n", + "234 \ufffd\n", + "\n", + "235 \ufffd\n", + "\n", + "236 \ufffd\n", + "\n", + "237 \ufffd\n", + "\n", + "238 \ufffd\n", + "\n", + "239 \ufffd\n", + "\n", + "240 \ufffd\n", + "\n", + "241 \ufffd\n", + "\n", + "242 \ufffd\n", + "\n", + "243 \ufffd\n", + "\n", + "244 \ufffd\n", + "\n", + "245 \ufffd\n", + "\n", + "246 \ufffd\n", + "\n", + "247 \ufffd\n", + "\n", + "248 \ufffd\n", + "\n", + "249 \ufffd\n", + "\n", + "250 \ufffd\n", + "\n", + "251 \ufffd\n", + "\n", + "252 \ufffd\n", + "\n", + "253 \ufffd\n", + "\n", + "254 \ufffd\n", + "\n", + "255 \ufffd\n", + "\n" + ] + } + ], + "prompt_number": 1 + } + ], + "metadata": {} + } + ] }
\ No newline at end of file diff --git a/ANSI_C_Programming/chapter14.ipynb b/ANSI_C_Programming/chapter14.ipynb index c29af0ac..35f6efe1 100644 --- a/ANSI_C_Programming/chapter14.ipynb +++ b/ANSI_C_Programming/chapter14.ipynb @@ -1,407 +1,407 @@ -{
- "metadata": {
- "name": "chapter14.ipynb"
- },
- "nbformat": 3,
- "nbformat_minor": 0,
- "worksheets": [
- {
- "cells": [
- {
- "cell_type": "heading",
- "level": 1,
- "metadata": {},
- "source": [
- "CHAPTER 14: OPERATIONS ON BITS"
- ]
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:449"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Print binary equivalent of integers using showbits() function()\n",
- "def showbits(x):\n",
- " return bin(x)[2:].zfill(16)\n",
- "for j in range(0,6,1):\n",
- " print \"Decimal %d is same as binary\" % (j)\n",
- " print showbits(j)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Decimal 0 is same as binary\n",
- "0000000000000000\n",
- "Decimal 1 is same as binary\n",
- "0000000000000001\n",
- "Decimal 2 is same as binary\n",
- "0000000000000010\n",
- "Decimal 3 is same as binary\n",
- "0000000000000011\n",
- "Decimal 4 is same as binary\n",
- "0000000000000100\n",
- "Decimal 5 is same as binary\n",
- "0000000000000101\n"
- ]
- }
- ],
- "prompt_number": 2
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:450"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Use of One's complement operator\n",
- "def showbits(x):\n",
- " return bin(x)[2:].zfill(16)\n",
- "for j in range(0,4,1):\n",
- " print \"Decimal %d is same as binary\" % (j)\n",
- " print showbits(j)\n",
- " k=j^65535 #using xor for making one's complement\n",
- " print \"One's complement of %d is\" % (j)\n",
- " print showbits(k)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Decimal 0 is same as binary\n",
- "0000000000000000\n",
- "One's complement of 0 is\n",
- "1111111111111111\n",
- "Decimal 1 is same as binary\n",
- "0000000000000001\n",
- "One's complement of 1 is\n",
- "1111111111111110\n",
- "Decimal 2 is same as binary\n",
- "0000000000000010\n",
- "One's complement of 2 is\n",
- "1111111111111101\n",
- "Decimal 3 is same as binary\n",
- "0000000000000011\n",
- "One's complement of 3 is\n",
- "1111111111111100\n"
- ]
- }
- ],
- "prompt_number": 15
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:451"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#file encryption utility\n",
- "import sys\n",
- "def encrypt():\n",
- " try:\n",
- " fs=open('SOURCE.txt','r') #normal file\n",
- " ft=open('TARGET.txt','w') #encrypted file\n",
- " except:\n",
- " print \"File opening error!\"\n",
- " sys.exit(1)\n",
- " while True:\n",
- " ch=fs.read(1)\n",
- " if not ch:\n",
- " break\n",
- " ft.write(bytearray([ord(ch)^0xff]))\n",
- " fs.close()\n",
- " ft.close()\n",
- "encrypt()"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [],
- "prompt_number": 3
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:452"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Use of Right shift operator\n",
- "def showbits(x):\n",
- " return bin(x)[2:].zfill(16)\n",
- "i=5225\n",
- "print \"Decimal %d is same as binary\" % (i)\n",
- "print showbits(i)\n",
- "for j in range(0,6,1):\n",
- " k=i>>j #right shift operator\n",
- " print \"%d right shift %d gives\" % (i,j)\n",
- " print showbits(k)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Decimal 5225 is same as binary\n",
- "0001010001101001\n",
- "5225 right shift 0 gives\n",
- "0001010001101001\n",
- "5225 right shift 1 gives\n",
- "0000101000110100\n",
- "5225 right shift 2 gives\n",
- "0000010100011010\n",
- "5225 right shift 3 gives\n",
- "0000001010001101\n",
- "5225 right shift 4 gives\n",
- "0000000101000110\n",
- "5225 right shift 5 gives\n",
- "0000000010100011\n"
- ]
- }
- ],
- "prompt_number": 3
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:453-454"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Use of left shift operator\n",
- "def showbits(x):\n",
- " return bin(x)[2:].zfill(16)\n",
- "i=5225\n",
- "print \"Decimal %d is same as binary\" % (i)\n",
- "print showbits(i)\n",
- "for j in range(0,5,1):\n",
- " mask = 2 ** 16 - 1\n",
- " k = (i << j) & mask #left shift operator\n",
- " print \"%d right shift %d gives\" % (i,j)\n",
- " print showbits(k)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Decimal 5225 is same as binary\n",
- "0001010001101001\n",
- "5225 right shift 0 gives\n",
- "0001010001101001\n",
- "5225 right shift 1 gives\n",
- "0010100011010010\n",
- "5225 right shift 2 gives\n",
- "0101000110100100\n",
- "5225 right shift 3 gives\n",
- "1010001101001000\n",
- "5225 right shift 4 gives\n",
- "0100011010010000\n"
- ]
- }
- ],
- "prompt_number": 4
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:457-458"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Decoding date field in directory entry using bitwise operators\n",
- "#Note:Answer in the book is wrong\n",
- "d=9\n",
- "m=3\n",
- "y=1990\n",
- "date=(y-1980)*512+m*32+d\n",
- "print \"Date=%u\\n\" % (date)\n",
- "year=1980+(date>>9)\n",
- "month=((date<<7)>>12)\n",
- "day=((date<<11)>>11)\n",
- "print \"Year=%u\" % (year)\n",
- "print \"Month=%u\" % (month)\n",
- "print \"Day=%u\" % (day)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Date=5225\n",
- "\n",
- "Year=1990\n",
- "Month=163\n",
- "Day=5225\n"
- ]
- }
- ],
- "prompt_number": 11
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:460"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#To test whether a bit in a number is ON or OFF\n",
- "i=65\n",
- "print \"Value of i=%d\\n\" % (i)\n",
- "j=i&32\n",
- "if j==0:\n",
- " print \"and its fifth bit is off\\n\"\n",
- "else:\n",
- " print \"and its fifth bit is on\\n\"\n",
- "j=i&64\n",
- "if j==0:\n",
- " print \"whereas its sixth bit is off\\n\"\n",
- "else:\n",
- " print \"whereas its sixth bit is on\\n\""
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Value of i=65\n",
- "\n",
- "and its fifth bit is off\n",
- "\n",
- "whereas its sixth bit is on\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 12
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:463"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Use of XOR operator\n",
- "b=50\n",
- "b=b^12 #XOR operator\n",
- "print \"%d\\n\" % (b) #this will print 62\n",
- "b=b^12\n",
- "print \"%d\\n\" % (b) #this will print 50"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "62\n",
- "\n",
- "50\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 13
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:463-464"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#function for 16 bit binary representation of a number\n",
- "def showbit(n):\n",
- " for i in range(15,-1,-1):\n",
- " andmask=1<<i\n",
- " k=n&andmask\n",
- " if k==0:\n",
- " print 0, \n",
- " else:\n",
- " print 1, \n",
- " print \"\\n\"\n",
- "showbit(32)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 \n",
- "\n"
- ]
- }
- ],
- "prompt_number": 14
- }
- ],
- "metadata": {}
- }
- ]
+{ + "metadata": { + "name": "", + "signature": "sha256:e28261ed5c0b0cf7c10e8c91bd2debc8e99bf24519225a056fc19ed619f1e95d" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "CHAPTER 14: OPERATIONS ON BITS" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:449" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def showbits(x):\n", + " return bin(x)[2:].zfill(16)\n", + "for j in range(0,6,1):\n", + " print \"Decimal %d is same as binary\" % (j)\n", + " print showbits(j)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Decimal 0 is same as binary\n", + "0000000000000000\n", + "Decimal 1 is same as binary\n", + "0000000000000001\n", + "Decimal 2 is same as binary\n", + "0000000000000010\n", + "Decimal 3 is same as binary\n", + "0000000000000011\n", + "Decimal 4 is same as binary\n", + "0000000000000100\n", + "Decimal 5 is same as binary\n", + "0000000000000101\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:450" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def showbits(x):\n", + " return bin(x)[2:].zfill(16)\n", + "for j in range(0,4,1):\n", + " print \"Decimal %d is same as binary\" % (j)\n", + " print showbits(j)\n", + " k=j^65535 #using xor for making one's complement\n", + " print \"One's complement of %d is\" % (j)\n", + " print showbits(k)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Decimal 0 is same as binary\n", + "0000000000000000\n", + "One's complement of 0 is\n", + "1111111111111111\n", + "Decimal 1 is same as binary\n", + "0000000000000001\n", + "One's complement of 1 is\n", + "1111111111111110\n", + "Decimal 2 is same as binary\n", + "0000000000000010\n", + "One's complement of 2 is\n", + "1111111111111101\n", + "Decimal 3 is same as binary\n", + "0000000000000011\n", + "One's complement of 3 is\n", + "1111111111111100\n" + ] + } + ], + "prompt_number": 15 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:451" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "import sys\n", + "def encrypt():\n", + " try:\n", + " fs=open('SOURCE.txt','r') #normal file\n", + " ft=open('TARGET.txt','w') #encrypted file\n", + " except:\n", + " print \"File opening error!\"\n", + " sys.exit(1)\n", + " while True:\n", + " ch=fs.read(1)\n", + " if not ch:\n", + " break\n", + " ft.write(bytearray([ord(ch)^0xff]))\n", + " fs.close()\n", + " ft.close()\n", + "encrypt()" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:452" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def showbits(x):\n", + " return bin(x)[2:].zfill(16)\n", + "i=5225\n", + "print \"Decimal %d is same as binary\" % (i)\n", + "print showbits(i)\n", + "for j in range(0,6,1):\n", + " k=i>>j #right shift operator\n", + " print \"%d right shift %d gives\" % (i,j)\n", + " print showbits(k)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Decimal 5225 is same as binary\n", + "0001010001101001\n", + "5225 right shift 0 gives\n", + "0001010001101001\n", + "5225 right shift 1 gives\n", + "0000101000110100\n", + "5225 right shift 2 gives\n", + "0000010100011010\n", + "5225 right shift 3 gives\n", + "0000001010001101\n", + "5225 right shift 4 gives\n", + "0000000101000110\n", + "5225 right shift 5 gives\n", + "0000000010100011\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:453-454" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def showbits(x):\n", + " return bin(x)[2:].zfill(16)\n", + "i=5225\n", + "print \"Decimal %d is same as binary\" % (i)\n", + "print showbits(i)\n", + "for j in range(0,5,1):\n", + " mask = 2 ** 16 - 1\n", + " k = (i << j) & mask #left shift operator\n", + " print \"%d right shift %d gives\" % (i,j)\n", + " print showbits(k)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Decimal 5225 is same as binary\n", + "0001010001101001\n", + "5225 right shift 0 gives\n", + "0001010001101001\n", + "5225 right shift 1 gives\n", + "0010100011010010\n", + "5225 right shift 2 gives\n", + "0101000110100100\n", + "5225 right shift 3 gives\n", + "1010001101001000\n", + "5225 right shift 4 gives\n", + "0100011010010000\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:457-458" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "d=9\n", + "m=3\n", + "y=1990\n", + "date=(y-1980)*512+m*32+d\n", + "print \"Date=%u\\n\" % (date)\n", + "year=1980+(date>>9)\n", + "month=((date<<7)>>12)\n", + "day=((date<<11)>>11)\n", + "print \"Year=%u\" % (year)\n", + "print \"Month=%u\" % (month)\n", + "print \"Day=%u\" % (day)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Date=5225\n", + "\n", + "Year=1990\n", + "Month=163\n", + "Day=5225\n" + ] + } + ], + "prompt_number": 11 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:460" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "i=65\n", + "print \"Value of i=%d\\n\" % (i)\n", + "j=i&32\n", + "if j==0:\n", + " print \"and its fifth bit is off\\n\"\n", + "else:\n", + " print \"and its fifth bit is on\\n\"\n", + "j=i&64\n", + "if j==0:\n", + " print \"whereas its sixth bit is off\\n\"\n", + "else:\n", + " print \"whereas its sixth bit is on\\n\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Value of i=65\n", + "\n", + "and its fifth bit is off\n", + "\n", + "whereas its sixth bit is on\n", + "\n" + ] + } + ], + "prompt_number": 12 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:463" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "b=50\n", + "b=b^12 #XOR operator\n", + "print \"%d\\n\" % (b) #this will print 62\n", + "b=b^12\n", + "print \"%d\\n\" % (b) #this will print 50" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "62\n", + "\n", + "50\n", + "\n" + ] + } + ], + "prompt_number": 13 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:463-464" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def showbit(n):\n", + " for i in range(15,-1,-1):\n", + " andmask=1<<i\n", + " k=n&andmask\n", + " if k==0:\n", + " print 0, \n", + " else:\n", + " print 1, \n", + " print \"\\n\"\n", + "showbit(32)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 \n", + "\n" + ] + } + ], + "prompt_number": 14 + } + ], + "metadata": {} + } + ] }
\ No newline at end of file diff --git a/ANSI_C_Programming/chapter15.ipynb b/ANSI_C_Programming/chapter15.ipynb index 9a67d04b..6421f521 100644 --- a/ANSI_C_Programming/chapter15.ipynb +++ b/ANSI_C_Programming/chapter15.ipynb @@ -1,691 +1,692 @@ -{
- "metadata": {
- "name": "chapter15.ipynb"
- },
- "nbformat": 3,
- "nbformat_minor": 0,
- "worksheets": [
- {
- "cells": [
- {
- "cell_type": "heading",
- "level": 1,
- "metadata": {},
- "source": [
- "CHAPTER 15: MISCELLANEOUS FEATURES"
- ]
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:473-474"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Use of enumerated data type for an employee department\n",
- "def emp_dept(*sequential, **named): #definition of enumeration\n",
- " enums = dict(zip(sequential, range(len(sequential))), **named)\n",
- " return type('Enum', (), enums)\n",
- "class employee(): #structure\n",
- " def __init__(self,**kwds):\n",
- " self.__dict__.update(kwds)\n",
- "department=emp_dept('assembly','manufacturing','accounts','stores') #defining a variable of type enum emp_dept\n",
- "e=employee(name=\"Lothar Mattheus\",age=28,bs=5575.50,department=department.manufacturing)\n",
- "print \"Name=%s\\n\" % (e.name)\n",
- "print \"Age=%d\\n\" % (e.age)\n",
- "print \"Basic salary=%f\\n\" % (e.bs)\n",
- "print \"Dept=%d\\n\" % (e.department)\n",
- "if e.department==department.accounts:\n",
- " print \"%s is an accountant\\n\" % (e.name)\n",
- "else:\n",
- " print \"%s is not an accountant\\n\" % (e.name)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Name=Lothar Mattheus\n",
- "\n",
- "Age=28\n",
- "\n",
- "Basic salary=5575.500000\n",
- "\n",
- "Dept=1\n",
- "\n",
- "Lothar Mattheus is not an accountant\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 1
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:475"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Above program using macros\n",
- "def ASSEMBLY():\n",
- " return 0\n",
- "def MANUFACTURING():\n",
- " return 1\n",
- "def ACCOUNTS():\n",
- " return 2\n",
- "def STORES():\n",
- " return 3\n",
- "class employee():\n",
- " def __init__(self,**kwds):\n",
- " self.__dict__.update(kwds)\n",
- "e=employee()\n",
- "e.name=\"Lothar Mattheus\"\n",
- "e.age=28\n",
- "e.bs=5575.50\n",
- "e.department=MANUFACTURING()\n",
- "print \"Name=%s\\n\" % (e.name)\n",
- "print \"Age=%d\\n\" % (e.age)\n",
- "print \"Basic salary=%f\\n\" % (e.bs)\n",
- "print \"Dept=%d\\n\" % (e.department)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Name=Lothar Mattheus\n",
- "\n",
- "Age=28\n",
- "\n",
- "Basic salary=5575.500000\n",
- "\n",
- "Dept=1\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 2
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:477-478"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#program showing need for typecasting\n",
- "x=6\n",
- "y=4\n",
- "a=x/y #int\n",
- "print \"Value of a=%f\\n\" % (a)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Value of a=1.000000\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 3
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:478"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#program using typecasting to convert an expression into particular data type\n",
- "x=6\n",
- "y=4\n",
- "a=float(x)/y #float\n",
- "print \"Value of a=%f\\n\" % (a)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Value of a=1.500000\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 4
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:478-479"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Another example of typecasting\n",
- "a=6.35\n",
- "print \"Value of a on type casting=%d\\n\" % (int(a)) #converts float to int\n",
- "print \"Value of a=%f\\n\" % (a) "
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Value of a on type casting=6\n",
- "\n",
- "Value of a=6.350000\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 5
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:480"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#using bit fields for stroring variables\n",
- "from ctypes import *\n",
- "def MALE():\n",
- " return 0\n",
- "def FEMALE():\n",
- " return 1\n",
- "def SINGLE():\n",
- " return 0\n",
- "def MARRIED():\n",
- " return 1\n",
- "def DIVORCED():\n",
- " return 2\n",
- "def WIDOWED():\n",
- " return 3\n",
- "class employee(Structure):\n",
- " _fields_= [(\"gender\",c_short, 1), #1 bit size for storage\n",
- " (\"mar_stat\", c_short, 2), #2 bit size for storage\n",
- " (\"hobby\",c_short, 3), #3 bit size for storage\n",
- " (\"scheme\",c_short, 4)] #4 bit size for storage\n",
- "e=employee()\n",
- "e.gender=MALE()\n",
- "e.mar_status=DIVORCED()\n",
- "e.hobby=5\n",
- "e.scheme=9\n",
- "print \"Gender=%d\\n\" % (e.gender)\n",
- "print \"Marital status=%d\\n\" % (e.mar_status)\n",
- "import ctypes\n",
- "print \"Bytes occupied by e=%d\\n\" % (ctypes.sizeof(e))"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Gender=0\n",
- "\n",
- "Marital status=2\n",
- "\n",
- "Bytes occupied by e=2\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 6
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:481"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#printing address of function\n",
- "def display():\n",
- " print \"Long live viruses!!\\n\"\n",
- "print \"Address of function display is %s\\n\" % (display)\n",
- "display()"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Address of function display is <function display at 0x02D93DF0>\n",
- "\n",
- "Long live viruses!!\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 7
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:481-482"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#printing address of function\n",
- "def display():\n",
- " print \"\\nLong live viruses!!\"\n",
- "func_ptr=display\n",
- "print \"Address of function display is %s\\n\" % (display)\n",
- "func_ptr()"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Address of function display is <function display at 0x05696730>\n",
- "\n",
- "\n",
- "Long live viruses!!\n"
- ]
- }
- ],
- "prompt_number": 8
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:483"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#function returning address\n",
- "def fun():\n",
- " i=20\n",
- " return (id(i))\n",
- "p=fun\n",
- "p()"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "metadata": {},
- "output_type": "pyout",
- "prompt_number": 9,
- "text": [
- "20688356"
- ]
- }
- ],
- "prompt_number": 9
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:483-484"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#program to copy one string into another\n",
- "def copy(t,s):\n",
- " t=\"\\0\"\n",
- " for item in s:\n",
- " t=t+item\n",
- " return t\n",
- "source=\"Jaded\"\n",
- "target=[]\n",
- "str1=copy(target,source)\n",
- "print \"%s\\n\" % (str1)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "\u0000Jaded\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 18
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:485"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#function with variable number of arguments\n",
- "def findmax(tot_num,*num): #tuples\n",
- " max=0\n",
- " for count in range(0,len(num),1):\n",
- " if num[count]>max:\n",
- " max=num[count]\n",
- " return max\n",
- "max=findmax(5,23,15,1,92,50)\n",
- "print \"maximum=%d\\n\" % (max)\n",
- "max=findmax(3,100,300,29)\n",
- "print \"maximum=%d\\n\" % (max)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "maximum=92\n",
- "\n",
- "maximum=300\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 19
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:486-487"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#function for printing any number of arguments of any type\n",
- "def display(type,num,*t):\n",
- " def integer():\n",
- " for j in range(0,num,1):\n",
- " print \"%d\" % (t[j])\n",
- " def char():\n",
- " for j in range(0,num,1):\n",
- " print \"%c\" % (t[j])\n",
- " def floating():\n",
- " for j in range(0,num,1):\n",
- " print \"%f\" % (t[j])\n",
- " def switch(ch):\n",
- " return {1: integer,\n",
- " 2: char,\n",
- " 3: floating,\n",
- " }[ch]()\n",
- " switch(type)\n",
- "display(1,2,5,6)\n",
- "display(2,4,'A','a','b','c')\n",
- "display(3,3,2.5,299.3,-1.0)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "5\n",
- "6\n",
- "A\n",
- "a\n",
- "b\n",
- "c\n",
- "2.500000\n",
- "299.300000\n",
- "-1.000000\n"
- ]
- }
- ],
- "prompt_number": 21
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:488"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Demo of union at work\n",
- "from ctypes import *\n",
- "class a(Union): #definition of union\n",
- " _fields_= [(\"i\", c_short),\n",
- " (\"ch\",c_byte*2)]\n",
- "key=a()\n",
- "key.i=512\n",
- "print key.i\n",
- "print key.ch[0]\n",
- "print key.ch[1]"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "512\n",
- "0\n",
- "2\n"
- ]
- }
- ],
- "prompt_number": 22
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:491"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Program explaining operation of union\n",
- "from ctypes import *\n",
- "class a(Union):\n",
- " _fields_= [(\"i\", c_short),\n",
- " (\"ch\",c_byte*2)]\n",
- "key=a()\n",
- "key.i=512\n",
- "print key.i\n",
- "print key.ch[0]\n",
- "print key.ch[1]\n",
- "key.ch[0]=50\n",
- "print key.i\n",
- "print key.ch[0]\n",
- "print key.ch[1]"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "512\n",
- "0\n",
- "2\n",
- "562\n",
- "50\n",
- "2\n"
- ]
- }
- ],
- "prompt_number": 1
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:492-493"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#program for union of structures\n",
- "from ctypes import *\n",
- "class a(Structure):\n",
- " _fields_ = [(\"i\", c_short),\n",
- " (\"c\", c_byte*2)]\n",
- "\n",
- "class b(Structure):\n",
- " _fields_ = [(\"j\", c_short),\n",
- " (\"d\", c_byte*2)]\n",
- "\n",
- "class z(Union):\n",
- " _fields_ = [(\"key\", a),\n",
- " (\"data\", b)]\n",
- "strange = z()\n",
- "strange.key.i=512\n",
- "strange.data.d[0]=0\n",
- "strange.data.d[1]=32\n",
- "print strange.key.i\n",
- "print strange.data.j\n",
- "print strange.key.c[0]\n",
- "print strange.data.d[0]\n",
- "print strange.key.c[1]\n",
- "print strange.data.d[1]"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "512\n",
- "512\n",
- "0\n",
- "0\n",
- "32\n",
- "32\n"
- ]
- }
- ],
- "prompt_number": 2
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:494-495"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Utility of Unions\n",
- "from ctypes import *\n",
- "class info1(Structure):\n",
- " _fields_ = [(\"hobby\", c_byte*10),\n",
- " (\"crcardno\", c_short)]\n",
- "\n",
- "class info2(Structure):\n",
- " _fields_ = [(\"vehno\", c_byte*10),\n",
- " (\"dist\", c_short)]\n",
- "\n",
- "class info(Union):\n",
- " _fields_ = [(\"a\", info1),\n",
- " (\"b\", info2)]\n",
- "class emp(Structure):\n",
- " _fields_ = [(\"n\", c_byte*20),\n",
- " (\"grade\", c_byte*4),\n",
- " (\"age\", c_short),\n",
- " (\"f\", info)]\n",
- "e=emp()"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [],
- "prompt_number": 3
- }
- ],
- "metadata": {}
- }
- ]
+{ + "metadata": { + "name": "", + "signature": "sha256:831402bdcf6a50366e6a400bf971e09b25983de0ebf01458d42a693aa22990c1" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "CHAPTER 15: MISCELLANEOUS FEATURES" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:473-474" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def emp_dept(*sequential, **named): #definition of enumeration\n", + " enums = dict(zip(sequential, range(len(sequential))), **named)\n", + " return type('Enum', (), enums)\n", + "class employee(): #structure\n", + " def __init__(self,**kwds):\n", + " self.__dict__.update(kwds)\n", + "department=emp_dept('assembly','manufacturing','accounts','stores') #defining a variable of type enum emp_dept\n", + "e=employee(name=\"Lothar Mattheus\",age=28,bs=5575.50,department=department.manufacturing)\n", + "print \"Name=%s\\n\" % (e.name)\n", + "print \"Age=%d\\n\" % (e.age)\n", + "print \"Basic salary=%f\\n\" % (e.bs)\n", + "print \"Dept=%d\\n\" % (e.department)\n", + "if e.department==department.accounts:\n", + " print \"%s is an accountant\\n\" % (e.name)\n", + "else:\n", + " print \"%s is not an accountant\\n\" % (e.name)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Name=Lothar Mattheus\n", + "\n", + "Age=28\n", + "\n", + "Basic salary=5575.500000\n", + "\n", + "Dept=1\n", + "\n", + "Lothar Mattheus is not an accountant\n", + "\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:475" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def ASSEMBLY():\n", + " return 0\n", + "def MANUFACTURING():\n", + " return 1\n", + "def ACCOUNTS():\n", + " return 2\n", + "def STORES():\n", + " return 3\n", + "class employee():\n", + " def __init__(self,**kwds):\n", + " self.__dict__.update(kwds)\n", + "e=employee()\n", + "e.name=\"Lothar Mattheus\"\n", + "e.age=28\n", + "e.bs=5575.50\n", + "e.department=MANUFACTURING()\n", + "print \"Name=%s\\n\" % (e.name)\n", + "print \"Age=%d\\n\" % (e.age)\n", + "print \"Basic salary=%f\\n\" % (e.bs)\n", + "print \"Dept=%d\\n\" % (e.department)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Name=Lothar Mattheus\n", + "\n", + "Age=28\n", + "\n", + "Basic salary=5575.500000\n", + "\n", + "Dept=1\n", + "\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:477-478" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "x=6\n", + "y=4\n", + "a=x/y #int\n", + "print \"Value of a=%f\\n\" % (a)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Value of a=1.000000\n", + "\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:478" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "x=6\n", + "y=4\n", + "a=float(x)/y #float\n", + "print \"Value of a=%f\\n\" % (a)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Value of a=1.500000\n", + "\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:478-479" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "a=6.35\n", + "print \"Value of a on type casting=%d\\n\" % (int(a)) #converts float to int\n", + "print \"Value of a=%f\\n\" % (a) " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Value of a on type casting=6\n", + "\n", + "Value of a=6.350000\n", + "\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:480" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "from ctypes import *\n", + "def MALE():\n", + " return 0\n", + "def FEMALE():\n", + " return 1\n", + "def SINGLE():\n", + " return 0\n", + "def MARRIED():\n", + " return 1\n", + "def DIVORCED():\n", + " return 2\n", + "def WIDOWED():\n", + " return 3\n", + "class employee(Structure):\n", + " _fields_= [(\"gender\",c_short, 1), #1 bit size for storage\n", + " (\"mar_stat\", c_short, 2), #2 bit size for storage\n", + " (\"hobby\",c_short, 3), #3 bit size for storage\n", + " (\"scheme\",c_short, 4)] #4 bit size for storage\n", + "e=employee()\n", + "e.gender=MALE()\n", + "e.mar_status=DIVORCED()\n", + "e.hobby=5\n", + "e.scheme=9\n", + "print \"Gender=%d\\n\" % (e.gender)\n", + "print \"Marital status=%d\\n\" % (e.mar_status)\n", + "import ctypes\n", + "print \"Bytes occupied by e=%d\\n\" % (ctypes.sizeof(e))" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Gender=0\n", + "\n", + "Marital status=2\n", + "\n", + "Bytes occupied by e=2\n", + "\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:481" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def display():\n", + " print \"Long live viruses!!\\n\"\n", + "print \"Address of function display is %s\\n\" % (display)\n", + "display()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Address of function display is <function display at 0x02D93DF0>\n", + "\n", + "Long live viruses!!\n", + "\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:481-482" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def display():\n", + " print \"\\nLong live viruses!!\"\n", + "func_ptr=display\n", + "print \"Address of function display is %s\\n\" % (display)\n", + "func_ptr()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Address of function display is <function display at 0x05696730>\n", + "\n", + "\n", + "Long live viruses!!\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:483" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def fun():\n", + " i=20\n", + " return (id(i))\n", + "p=fun\n", + "p()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 9, + "text": [ + "20688356" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:483-484" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def copy(t,s):\n", + " t=\"\\0\"\n", + " for item in s:\n", + " t=t+item\n", + " return t\n", + "source=\"Jaded\"\n", + "target=[]\n", + "str1=copy(target,source)\n", + "print \"%s\\n\" % (str1)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\u0000Jaded\n", + "\n" + ] + } + ], + "prompt_number": 18 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:485" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def findmax(tot_num,*num): #tuples\n", + " max=0\n", + " for count in range(0,len(num),1):\n", + " if num[count]>max:\n", + " max=num[count]\n", + " return max\n", + "max=findmax(5,23,15,1,92,50)\n", + "print \"maximum=%d\\n\" % (max)\n", + "max=findmax(3,100,300,29)\n", + "print \"maximum=%d\\n\" % (max)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "maximum=92\n", + "\n", + "maximum=300\n", + "\n" + ] + } + ], + "prompt_number": 19 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:486-487" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def display(type,num,*t):\n", + " def integer():\n", + " for j in range(0,num,1):\n", + " print \"%d\" % (t[j])\n", + " def char():\n", + " for j in range(0,num,1):\n", + " print \"%c\" % (t[j])\n", + " def floating():\n", + " for j in range(0,num,1):\n", + " print \"%f\" % (t[j])\n", + " def switch(ch):\n", + " return {1: integer,\n", + " 2: char,\n", + " 3: floating,\n", + " }[ch]()\n", + " switch(type)\n", + "display(1,2,5,6)\n", + "display(2,4,'A','a','b','c')\n", + "display(3,3,2.5,299.3,-1.0)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n", + "6\n", + "A\n", + "a\n", + "b\n", + "c\n", + "2.500000\n", + "299.300000\n", + "-1.000000\n" + ] + } + ], + "prompt_number": 21 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:488" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "from ctypes import *\n", + "class a(Union): #definition of union\n", + " _fields_= [(\"i\", c_short),\n", + " (\"ch\",c_byte*2)]\n", + "key=a()\n", + "key.i=512\n", + "print key.i\n", + "print key.ch[0]\n", + "print key.ch[1]" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "512\n", + "0\n", + "2\n" + ] + } + ], + "prompt_number": 22 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:491" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "from ctypes import *\n", + "class a(Union):\n", + " _fields_= [(\"i\", c_short),\n", + " (\"ch\",c_byte*2)]\n", + "key=a()\n", + "key.i=512\n", + "print key.i\n", + "print key.ch[0]\n", + "print key.ch[1]\n", + "key.ch[0]=50\n", + "print key.i\n", + "print key.ch[0]\n", + "print key.ch[1]" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "512\n", + "0\n", + "2\n", + "562\n", + "50\n", + "2\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:492-493" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "from ctypes import *\n", + "class a(Structure):\n", + " _fields_ = [(\"i\", c_short),\n", + " (\"c\", c_byte*2)]\n", + "\n", + "class b(Structure):\n", + " _fields_ = [(\"j\", c_short),\n", + " (\"d\", c_byte*2)]\n", + "\n", + "class z(Union):\n", + " _fields_ = [(\"key\", a),\n", + " (\"data\", b)]\n", + "strange = z()\n", + "strange.key.i=512\n", + "strange.data.d[0]=0\n", + "strange.data.d[1]=32\n", + "print strange.key.i\n", + "print strange.data.j\n", + "print strange.key.c[0]\n", + "print strange.data.d[0]\n", + "print strange.key.c[1]\n", + "print strange.data.d[1]" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "512\n", + "512\n", + "0\n", + "0\n", + "32\n", + "32\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:494-495" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "from ctypes import *\n", + "class info1(Structure):\n", + " _fields_ = [(\"hobby\", c_byte*10),\n", + " (\"crcardno\", c_short)]\n", + "\n", + "class info2(Structure):\n", + " _fields_ = [(\"vehno\", c_byte*10),\n", + " (\"dist\", c_short)]\n", + "\n", + "class info(Union):\n", + " _fields_ = [(\"a\", info1),\n", + " (\"b\", info2)]\n", + "class emp(Structure):\n", + " _fields_ = [(\"n\", c_byte*20),\n", + " (\"grade\", c_byte*4),\n", + " (\"age\", c_short),\n", + " (\"f\", info)]\n", + "e=emp()" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 3 + } + ], + "metadata": {} + } + ] }
\ No newline at end of file diff --git a/ANSI_C_Programming/chapter2.ipynb b/ANSI_C_Programming/chapter2.ipynb index 255326b2..644241bb 100644 --- a/ANSI_C_Programming/chapter2.ipynb +++ b/ANSI_C_Programming/chapter2.ipynb @@ -1,782 +1,783 @@ -{
- "metadata": {
- "name": "chapter2.ipynb"
- },
- "nbformat": 3,
- "nbformat_minor": 0,
- "worksheets": [
- {
- "cells": [
- {
- "cell_type": "heading",
- "level": 1,
- "metadata": {},
- "source": [
- "CHAPTER 2: THE DECISION CONTROL STRUCTURE"
- ]
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE-45"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Demonstration of if statement\n",
- "print \"Enter a number less than 10\"\n",
- "num=eval(raw_input()) #Inputs float variables #you can also input float variables using input()\n",
- "if num<10:\n",
- " print \"What an obedient servant you are !\\n\""
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter a number less than 10\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "5\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "What an obedient servant you are !\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 1
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "Example 2.1: Page-47"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Calculation of total expenses\n",
- "print \"Enter quantity and rate\"\n",
- "qty=eval(raw_input()) #Inputs float variables\n",
- "rate=eval(raw_input())\n",
- "dis=0\n",
- "if qty>1000:\n",
- " dis=10\n",
- "tot=(qty*rate)-(qty*rate*dis/100)\n",
- "print \"Total expenses=Rs.\" ,tot"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter quantity and rate\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "1200\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "15.50\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Total expenses=Rs. 16740.0\n"
- ]
- }
- ],
- "prompt_number": 2
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "Example 2.2: Page-49"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Calculation of bonus\n",
- "print \"Enter current year and year of joining\"\n",
- "cy=eval(raw_input())\n",
- "yoj=eval(raw_input())\n",
- "yos=cy-yoj\n",
- "if yos>3:\n",
- " bonus=2500\n",
- " print \"Bonus=Rs.\" ,bonus"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter current year and year of joining\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "2014\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "2009\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Bonus=Rs. 2500\n"
- ]
- }
- ],
- "prompt_number": 4
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "Example 2.3: Page-51"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Calculation of gross salary\n",
- "print \"Enter basic salary\"\n",
- "bs=eval(raw_input())\n",
- "if bs<1500:\n",
- " hra=bs*10/100\n",
- " da=bs*90/100\n",
- "else:\n",
- " hra=500\n",
- " da=bs*98/100\n",
- "gs=bs+hra+da\n",
- "print \"gross salary=Rs.\" ,gs"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter basic salary\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "1000\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "gross salary=Rs. 2000\n"
- ]
- }
- ],
- "prompt_number": 5
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON Page-53"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#A quick demo of nested if-else\n",
- "print \"Enter either 1 or 2\"\n",
- "i=eval(raw_input())\n",
- "if i==1:\n",
- " print \"You would go to heaven !\\n\"\n",
- "else:\n",
- " if i==2:\n",
- " print \"Hell was created with you in mind\\n\"\n",
- " else:\n",
- " print \"How about mother earth !\\n\""
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter either 1 or 2\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "3\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "How about mother earth !\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 6
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "Example 2.4: Page-55-56"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Method-I: program using nested if-else to find division of student\n",
- "print \"Enter marks in five subjects\"\n",
- "m1=eval(raw_input())\n",
- "m2=eval(raw_input())\n",
- "m3=eval(raw_input())\n",
- "m4=eval(raw_input())\n",
- "m5=eval(raw_input())\n",
- "per=(m1+m2+m3+m4+m5)*100/500\n",
- "if per>=60:\n",
- " print \"First division\\n\"\n",
- "else:\n",
- " if per>=50:\n",
- " print \"Second division\\n\"\n",
- " else:\n",
- " if per>=40:\n",
- " print \"Third division\\n\"\n",
- " else:\n",
- " print \"Fail\\n\""
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks in five subjects\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "70\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "87\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "91\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "91\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "98\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "First division\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 7
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON Page-56-57"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Method-II: program using if statement to find division of student\n",
- "print \"Enter marks in five subjects\"\n",
- "m1=eval(raw_input())\n",
- "m2=eval(raw_input())\n",
- "m3=eval(raw_input())\n",
- "m4=eval(raw_input())\n",
- "m5=eval(raw_input())\n",
- "per=(m1+m2+m3+m4+m5)*100/500\n",
- "if per>=60:\n",
- " print \"First division\\n\"\n",
- "if per>=50 and per<60:\n",
- " print \"Second division\\n\"\n",
- "if per>=40 and per<50:\n",
- " print \"Third division\\n\"\n",
- "if per<40:\n",
- " print \"Fail\\n\""
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks in five subjects\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "70\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "87\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "91\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "91\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "98\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "First division\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 8
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON Page-58"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#else if ladder demo\n",
- "print \"Enter marks in five subjects\"\n",
- "m1=eval(raw_input())\n",
- "m2=eval(raw_input())\n",
- "m3=eval(raw_input())\n",
- "m4=eval(raw_input())\n",
- "m5=eval(raw_input())\n",
- "per=(m1+m2+m3+m4+m5)*100/500\n",
- "if per>=60:\n",
- " print \"First division\\n\"\n",
- "elif per>=50:\n",
- " print \"Second division\\n\"\n",
- "elif per>=40:\n",
- " print \"Third division\\n\"\n",
- "else:\n",
- " print \"fail\\n\""
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks in five subjects\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "70\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "87\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "91\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "91\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "98\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "First division\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 9
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "Example 2.5: Page-59"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Insurance of driver-without using logical operators\n",
- "print \"Enter age,sex,marital status\"\n",
- "age=eval(raw_input())\n",
- "sex=raw_input() #Inputs string variables i,e; we can't use these variables in calculations\n",
- "ms=raw_input() #Inputs string variables\n",
- "if ms=='M':\n",
- " print \"Driver is insured\\n\"\n",
- "else:\n",
- " if sex=='M':\n",
- " if age>30:\n",
- " print \"Driver is insured\\n\"\n",
- " else:\n",
- " print \"Driver is not insured\\n\"\n",
- " else:\n",
- " if age>25:\n",
- " print \"Driver is insured\\n\"\n",
- " else:\n",
- " print \"Driver is not insured\\n\""
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter age,sex,marital status\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "20\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "M\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "U\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Driver is not insured\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 10
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON Page-60"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Insurance of driver-using logical operators\n",
- "print \"Enter age,sex,marital status\"\n",
- "age=eval(raw_input())\n",
- "sex=raw_input()\n",
- "ms=raw_input()\n",
- "if (ms=='M') or (ms=='U' and sex=='M' and age>30) or (ms=='U' and sex=='F' and age>25):\n",
- " print \"Driver is insured\\n\"\n",
- "else:\n",
- " print \"Driver is not insured\\n\""
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter age,sex,marital status\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "20\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "M\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "U\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Driver is not insured\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 11
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "Example 2.6: Page-61-62"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Calculate the salary\n",
- "print \"Enter Gender,Years of Service and Qualifications(0=G,1=PG):\"\n",
- "g=raw_input()\n",
- "yos=eval(raw_input())\n",
- "qual=eval(raw_input())\n",
- "sal=0\n",
- "if g=='m' and yos>=10 and qual==1:\n",
- " sal=15000\n",
- "elif (g=='m' and yos>=10 and qual==0) or (g=='m' and yos<10 and qual==1):\n",
- " sal=10000\n",
- "elif g=='m' and yos<10 and qual==0:\n",
- " sal=7000\n",
- "elif g=='f' and yos>=10 and qual==1:\n",
- " sal=12000\n",
- "elif g=='f' and yos>=10 and qual==0:\n",
- " sal=9000\n",
- "elif g=='f' and yos<10 and qual==1:\n",
- " sal=10000\n",
- "elif g=='f' and yos<10 and qual==0:\n",
- " sal=6000\n",
- "print \"\\nSalary of Employee=\" ,sal"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter Gender,Years of Service and Qualifications(0=G,1=PG):\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "m\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "5\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "0\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "\n",
- "Salary of Employee= 7000\n"
- ]
- }
- ],
- "prompt_number": 12
- }
- ],
- "metadata": {}
- }
- ]
+{ + "metadata": { + "name": "", + "signature": "sha256:6a166cd5bc555687c7e5271b8e9483765eadd38f5e9c5bfca7169a4ded24b03b" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "CHAPTER 2: THE DECISION CONTROL STRUCTURE" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE-45" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "print \"Enter a number less than 10\"\n", + "num=eval(raw_input()) #Inputs float variables #you can also input float variables using input()\n", + "if num<10:\n", + " print \"What an obedient servant you are !\\n\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a number less than 10\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "What an obedient servant you are !\n", + "\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.1: Page-47" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "print \"Enter quantity and rate\"\n", + "qty=eval(raw_input()) #Inputs float variables\n", + "rate=eval(raw_input())\n", + "dis=0\n", + "if qty>1000:\n", + " dis=10\n", + "tot=(qty*rate)-(qty*rate*dis/100)\n", + "print \"Total expenses=Rs.\" ,tot" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter quantity and rate\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1200\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "15.50\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Total expenses=Rs. 16740.0\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.2: Page-49" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "print \"Enter current year and year of joining\"\n", + "cy=eval(raw_input())\n", + "yoj=eval(raw_input())\n", + "yos=cy-yoj\n", + "if yos>3:\n", + " bonus=2500\n", + " print \"Bonus=Rs.\" ,bonus" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter current year and year of joining\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2014\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2009\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Bonus=Rs. 2500\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.3: Page-51" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "print \"Enter basic salary\"\n", + "bs=eval(raw_input())\n", + "if bs<1500:\n", + " hra=bs*10/100\n", + " da=bs*90/100\n", + "else:\n", + " hra=500\n", + " da=bs*98/100\n", + "gs=bs+hra+da\n", + "print \"gross salary=Rs.\" ,gs" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter basic salary\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1000\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "gross salary=Rs. 2000\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON Page-53" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "print \"Enter either 1 or 2\"\n", + "i=eval(raw_input())\n", + "if i==1:\n", + " print \"You would go to heaven !\\n\"\n", + "else:\n", + " if i==2:\n", + " print \"Hell was created with you in mind\\n\"\n", + " else:\n", + " print \"How about mother earth !\\n\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter either 1 or 2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "How about mother earth !\n", + "\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.4: Page-55-56" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "print \"Enter marks in five subjects\"\n", + "m1=eval(raw_input())\n", + "m2=eval(raw_input())\n", + "m3=eval(raw_input())\n", + "m4=eval(raw_input())\n", + "m5=eval(raw_input())\n", + "per=(m1+m2+m3+m4+m5)*100/500\n", + "if per>=60:\n", + " print \"First division\\n\"\n", + "else:\n", + " if per>=50:\n", + " print \"Second division\\n\"\n", + " else:\n", + " if per>=40:\n", + " print \"Third division\\n\"\n", + " else:\n", + " print \"Fail\\n\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter marks in five subjects\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "70\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "87\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "91\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "91\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "98\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "First division\n", + "\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON Page-56-57" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "print \"Enter marks in five subjects\"\n", + "m1=eval(raw_input())\n", + "m2=eval(raw_input())\n", + "m3=eval(raw_input())\n", + "m4=eval(raw_input())\n", + "m5=eval(raw_input())\n", + "per=(m1+m2+m3+m4+m5)*100/500\n", + "if per>=60:\n", + " print \"First division\\n\"\n", + "if per>=50 and per<60:\n", + " print \"Second division\\n\"\n", + "if per>=40 and per<50:\n", + " print \"Third division\\n\"\n", + "if per<40:\n", + " print \"Fail\\n\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter marks in five subjects\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "70\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "87\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "91\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "91\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "98\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "First division\n", + "\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON Page-58" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "print \"Enter marks in five subjects\"\n", + "m1=eval(raw_input())\n", + "m2=eval(raw_input())\n", + "m3=eval(raw_input())\n", + "m4=eval(raw_input())\n", + "m5=eval(raw_input())\n", + "per=(m1+m2+m3+m4+m5)*100/500\n", + "if per>=60:\n", + " print \"First division\\n\"\n", + "elif per>=50:\n", + " print \"Second division\\n\"\n", + "elif per>=40:\n", + " print \"Third division\\n\"\n", + "else:\n", + " print \"fail\\n\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter marks in five subjects\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "70\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "87\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "91\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "91\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "98\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "First division\n", + "\n" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.5: Page-59" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "print \"Enter age,sex,marital status\"\n", + "age=eval(raw_input())\n", + "sex=raw_input() #Inputs string variables i,e; we can't use these variables in calculations\n", + "ms=raw_input() #Inputs string variables\n", + "if ms=='M':\n", + " print \"Driver is insured\\n\"\n", + "else:\n", + " if sex=='M':\n", + " if age>30:\n", + " print \"Driver is insured\\n\"\n", + " else:\n", + " print \"Driver is not insured\\n\"\n", + " else:\n", + " if age>25:\n", + " print \"Driver is insured\\n\"\n", + " else:\n", + " print \"Driver is not insured\\n\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter age,sex,marital status\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "20\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "M\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "U\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Driver is not insured\n", + "\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON Page-60" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "print \"Enter age,sex,marital status\"\n", + "age=eval(raw_input())\n", + "sex=raw_input()\n", + "ms=raw_input()\n", + "if (ms=='M') or (ms=='U' and sex=='M' and age>30) or (ms=='U' and sex=='F' and age>25):\n", + " print \"Driver is insured\\n\"\n", + "else:\n", + " print \"Driver is not insured\\n\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter age,sex,marital status\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "20\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "M\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "U\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Driver is not insured\n", + "\n" + ] + } + ], + "prompt_number": 11 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 2.6: Page-61-62" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "print \"Enter Gender,Years of Service and Qualifications(0=G,1=PG):\"\n", + "g=raw_input()\n", + "yos=eval(raw_input())\n", + "qual=eval(raw_input())\n", + "sal=0\n", + "if g=='m' and yos>=10 and qual==1:\n", + " sal=15000\n", + "elif (g=='m' and yos>=10 and qual==0) or (g=='m' and yos<10 and qual==1):\n", + " sal=10000\n", + "elif g=='m' and yos<10 and qual==0:\n", + " sal=7000\n", + "elif g=='f' and yos>=10 and qual==1:\n", + " sal=12000\n", + "elif g=='f' and yos>=10 and qual==0:\n", + " sal=9000\n", + "elif g=='f' and yos<10 and qual==1:\n", + " sal=10000\n", + "elif g=='f' and yos<10 and qual==0:\n", + " sal=6000\n", + "print \"\\nSalary of Employee=\" ,sal" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter Gender,Years of Service and Qualifications(0=G,1=PG):\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "m\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "0\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Salary of Employee= 7000\n" + ] + } + ], + "prompt_number": 12 + } + ], + "metadata": {} + } + ] }
\ No newline at end of file diff --git a/ANSI_C_Programming/chapter3.ipynb b/ANSI_C_Programming/chapter3.ipynb index 18b9c2f4..24b5eb3a 100644 --- a/ANSI_C_Programming/chapter3.ipynb +++ b/ANSI_C_Programming/chapter3.ipynb @@ -1,1590 +1,1590 @@ -{
- "metadata": {
- "name": "chapter3.ipynb"
- },
- "nbformat": 3,
- "nbformat_minor": 0,
- "worksheets": [
- {
- "cells": [
- {
- "cell_type": "heading",
- "level": 1,
- "metadata": {},
- "source": [
- "CHAPTER 3: THE LOOP CONTROL STRUCTURE"
- ]
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:89-90"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Calculation of simple interest for 3 sets of p,n and r\n",
- "count=1\n",
- "while count<=3: #while loop codition check\n",
- " print \"Enter values of p,n and r\"\n",
- " p=eval(raw_input())\n",
- " n=eval(raw_input())\n",
- " r=eval(raw_input())\n",
- " si=p*n*r/100\n",
- " print \"Simple interest=Rs.%f\" % (si)\n",
- " count=count+1 #increment"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter values of p,n and r\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "1000\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "5\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "13.5\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Simple interest=Rs.675.000000\n",
- "Enter values of p,n and r\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "2000\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "5\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "13.5\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Simple interest=Rs.1350.000000\n",
- "Enter values of p,n and r\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "3500\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "5\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "3.5\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Simple interest=Rs.612.500000\n"
- ]
- }
- ],
- "prompt_number": 1
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:92"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#a program for indefinite loop\n",
- "i=1\n",
- "while i<=10:\n",
- " print \"%d\\n\" % (i) #there is no increment/decrement thats why indefinite loop"
- ],
- "language": "python",
- "metadata": {},
- "outputs": []
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:93"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#printing numbers from 1 to 10\n",
- "i=1\n",
- "while i<=10:\n",
- " print \"\\n\" ,i\n",
- " i=i+1"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "\n",
- "1\n",
- "\n",
- "2\n",
- "\n",
- "3\n",
- "\n",
- "4\n",
- "\n",
- "5\n",
- "\n",
- "6\n",
- "\n",
- "7\n",
- "\n",
- "8\n",
- "\n",
- "9\n",
- "\n",
- "10\n"
- ]
- }
- ],
- "prompt_number": 2
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:93"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#printing a message repeatedly\n",
- "i=5\n",
- "while i>=1:\n",
- " print \"Make the computer literate!\\n\"\n",
- " i=i-1"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Make the computer literate!\n",
- "\n",
- "Make the computer literate!\n",
- "\n",
- "Make the computer literate!\n",
- "\n",
- "Make the computer literate!\n",
- "\n",
- "Make the computer literate!\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 3
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:93"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#printing a message repeatedly by taking float loop counter\n",
- "a=10.0\n",
- "while a<=10.5:\n",
- " print \"Raindrops on roses...\"\n",
- " print \"...and whiskers on kittens\\n\"\n",
- " a=a+0.1"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Raindrops on roses...\n",
- "...and whiskers on kittens\n",
- "\n",
- "Raindrops on roses...\n",
- "...and whiskers on kittens\n",
- "\n",
- "Raindrops on roses...\n",
- "...and whiskers on kittens\n",
- "\n",
- "Raindrops on roses...\n",
- "...and whiskers on kittens\n",
- "\n",
- "Raindrops on roses...\n",
- "...and whiskers on kittens\n",
- "\n",
- "Raindrops on roses...\n",
- "...and whiskers on kittens\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 4
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:94"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#This is not indefinite loop in python because range of int in python is larger than 32767\n",
- "i=1\n",
- "while i<=32767: #print numbers from 1 to 32767\n",
- " print \"%d\\n\" ,i \n",
- " i=i+1"
- ],
- "language": "python",
- "metadata": {},
- "outputs": []
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:95"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Method 1:print numbers from 1 to 10\n",
- "i=1 \n",
- "while i<=10:\n",
- " print i\n",
- " i=i+1"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "1\n",
- "2\n",
- "3\n",
- "4\n",
- "5\n",
- "6\n",
- "7\n",
- "8\n",
- "9\n",
- "10\n"
- ]
- }
- ],
- "prompt_number": 5
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:96"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Method 2:print numbers from 1 to 10\n",
- "i=1\n",
- "while i<=10:\n",
- " print \"\\n\" ,i\n",
- " i+=1 #increment short hand operator"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "\n",
- "1\n",
- "\n",
- "2\n",
- "\n",
- "3\n",
- "\n",
- "4\n",
- "\n",
- "5\n",
- "\n",
- "6\n",
- "\n",
- "7\n",
- "\n",
- "8\n",
- "\n",
- "9\n",
- "\n",
- "10\n"
- ]
- }
- ],
- "prompt_number": 6
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:98"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Calculation of simple interest for 3 sets of p,n and r using \"for loop\"\n",
- "for count in range(1,4,1): #for loop\n",
- " print \"Enter values of p,n and r\"\n",
- " p=eval(raw_input())\n",
- " n=eval(raw_input())\n",
- " r=eval(raw_input())\n",
- " si=p*n*r/100\n",
- " print \"Simple Interest=Rs.%f\" % (si)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter values of p,n and r\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "1000\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "5\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "13.5\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Simple Interest=Rs.675.000000\n",
- "Enter values of p,n and r\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "2000\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "5\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "13.5\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Simple Interest=Rs.1350.000000\n",
- "Enter values of p,n and r\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "3500\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "5\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "3.5\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Simple Interest=Rs.612.500000\n"
- ]
- }
- ],
- "prompt_number": 7
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:101"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#print numbers from 1 to 10 using for loop\n",
- "for i in range(1,11,1):\n",
- " print \"%d\\n\" % (i)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "1\n",
- "\n",
- "2\n",
- "\n",
- "3\n",
- "\n",
- "4\n",
- "\n",
- "5\n",
- "\n",
- "6\n",
- "\n",
- "7\n",
- "\n",
- "8\n",
- "\n",
- "9\n",
- "\n",
- "10\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 8
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:101"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#print numbers from 1 to 10 with incrementation in the body\n",
- "for i in range(1,11):\n",
- " print \"\\n\" ,i\n",
- " i=i+1"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "\n",
- "1\n",
- "\n",
- "2\n",
- "\n",
- "3\n",
- "\n",
- "4\n",
- "\n",
- "5\n",
- "\n",
- "6\n",
- "\n",
- "7\n",
- "\n",
- "8\n",
- "\n",
- "9\n",
- "\n",
- "10\n"
- ]
- }
- ],
- "prompt_number": 9
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:101"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#print numbers from 1 to 10 with initialisation above for loop\n",
- "i=1\n",
- "for i in range(i,11,1):\n",
- " print \"%d\\n\" % (i)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "1\n",
- "\n",
- "2\n",
- "\n",
- "3\n",
- "\n",
- "4\n",
- "\n",
- "5\n",
- "\n",
- "6\n",
- "\n",
- "7\n",
- "\n",
- "8\n",
- "\n",
- "9\n",
- "\n",
- "10\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 10
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:102"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#print 1 to 10 with initialisation above for loop and incrementation in the body\n",
- "i=1\n",
- "for i in range(i,11):\n",
- " print \"%d\\n\" % (i)\n",
- " i=i+1"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "1\n",
- "\n",
- "2\n",
- "\n",
- "3\n",
- "\n",
- "4\n",
- "\n",
- "5\n",
- "\n",
- "6\n",
- "\n",
- "7\n",
- "\n",
- "8\n",
- "\n",
- "9\n",
- "\n",
- "10\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 11
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:103"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Demonstration of nested loops\n",
- "for r in range(1,4,1): #outer loop\n",
- " for c in range(1,3,1): #inner loop \n",
- " sum=r+c\n",
- " print \"r=%d c=%d sum=%d\\n\" % (r,c,sum)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "r=1 c=1 sum=2\n",
- "\n",
- "r=1 c=2 sum=3\n",
- "\n",
- "r=2 c=1 sum=3\n",
- "\n",
- "r=2 c=2 sum=4\n",
- "\n",
- "r=3 c=1 sum=4\n",
- "\n",
- "r=3 c=2 sum=5\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 12
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:104-105"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Execution of a loop an unknown number of times\n",
- "#Implementing do-while loop using while loop\n",
- "while(1): #do-while loop is not present in python\n",
- " print \"Enter a number\"\n",
- " num=eval(raw_input())\n",
- " print \"square of %d is %d\\n\" % (num,num*num)\n",
- " print \"Want to enter another number y/n\"\n",
- " another=raw_input()\n",
- " if(another!='y'):\n",
- " break"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter a number\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "5\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "square of 5 is 25\n",
- "\n",
- "Want to enter another number y/n\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "y\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter a number\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "7\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "square of 7 is 49\n",
- "\n",
- "Want to enter another number y/n\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "n\n"
- ]
- }
- ],
- "prompt_number": 1
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:105"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#odd loop using a for loop\n",
- "another='y'\n",
- "for i in range(1,1000): #check if another is in word\n",
- " if another=='y':\n",
- " print \"Enter a number\"\n",
- " num=eval(raw_input())\n",
- " print \"square of %d is %d\\n\" % (num,num*num)\n",
- " print \"Want to enter another number y/n\"\n",
- " another=raw_input()"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter a number\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "5\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "square of 5 is 25\n",
- "\n",
- "Want to enter another number y/n\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "y\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter a number\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "7\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "square of 7 is 49\n",
- "\n",
- "Want to enter another number y/n\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "n\n"
- ]
- }
- ],
- "prompt_number": 13
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:105-106"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#odd loop using a while loop\n",
- "another='y'\n",
- "while(another=='y'):\n",
- " print \"Enter a number\"\n",
- " num=eval(raw_input())\n",
- " print \"square of %d is %d\\n\" % (num,num*num)\n",
- " print \"Want to enter another number y/n\"\n",
- " another=raw_input()"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter a number\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "5\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "square of 5 is 25\n",
- "\n",
- "Want to enter another number y/n\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "y\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter a number\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "7\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "square of 7 is 49\n",
- "\n",
- "Want to enter another number y/n\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "n\n"
- ]
- }
- ],
- "prompt_number": 14
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:106"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Program to check prime number using break in while loop\n",
- "print \"Enter a number\"\n",
- "num=eval(raw_input())\n",
- "i=2\n",
- "while i<=num-1:\n",
- " if num%i==0:\n",
- " print \"Not a prime number\\n\"\n",
- " break #exit the loop\n",
- " i+=1\n",
- "if i==num:\n",
- " print \"Prime number\\n\""
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter a number\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "11\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Prime number\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 15
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:107"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#program using break statement\n",
- "i=1\n",
- "j=1\n",
- "while (i<=100):\n",
- " i+=1\n",
- " while (j<=200):\n",
- " j+=1\n",
- " if (j==150):\n",
- " break #it will terminate the inner loop only\n",
- " else:\n",
- " print \"%d %d\\n\" % (i,j)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "2 2\n",
- "\n",
- "2 3\n",
- "\n",
- "2 4\n",
- "\n",
- "2 5\n",
- "\n",
- "2 6\n",
- "\n",
- "2 7\n",
- "\n",
- "2 8\n",
- "\n",
- "2 9\n",
- "\n",
- "2 10\n",
- "\n",
- "2 11\n",
- "\n",
- "2 12\n",
- "\n",
- "2 13\n",
- "\n",
- "2 14\n",
- "\n",
- "2 15\n",
- "\n",
- "2 16\n",
- "\n",
- "2 17\n",
- "\n",
- "2 18\n",
- "\n",
- "2 19\n",
- "\n",
- "2 20\n",
- "\n",
- "2 21\n",
- "\n",
- "2 22\n",
- "\n",
- "2 23\n",
- "\n",
- "2 24\n",
- "\n",
- "2 25\n",
- "\n",
- "2 26\n",
- "\n",
- "2 27\n",
- "\n",
- "2 28\n",
- "\n",
- "2 29\n",
- "\n",
- "2 30\n",
- "\n",
- "2 31\n",
- "\n",
- "2 32\n",
- "\n",
- "2 33\n",
- "\n",
- "2 34\n",
- "\n",
- "2 35\n",
- "\n",
- "2 36\n",
- "\n",
- "2 37\n",
- "\n",
- "2 38\n",
- "\n",
- "2 39\n",
- "\n",
- "2 40\n",
- "\n",
- "2 41\n",
- "\n",
- "2 42\n",
- "\n",
- "2 43\n",
- "\n",
- "2 44\n",
- "\n",
- "2 45\n",
- "\n",
- "2 46\n",
- "\n",
- "2 47\n",
- "\n",
- "2 48\n",
- "\n",
- "2 49\n",
- "\n",
- "2 50\n",
- "\n",
- "2 51\n",
- "\n",
- "2 52\n",
- "\n",
- "2 53\n",
- "\n",
- "2 54\n",
- "\n",
- "2 55\n",
- "\n",
- "2 56\n",
- "\n",
- "2 57\n",
- "\n",
- "2 58\n",
- "\n",
- "2 59\n",
- "\n",
- "2 60\n",
- "\n",
- "2 61\n",
- "\n",
- "2 62\n",
- "\n",
- "2 63\n",
- "\n",
- "2 64\n",
- "\n",
- "2 65\n",
- "\n",
- "2 66\n",
- "\n",
- "2 67\n",
- "\n",
- "2 68\n",
- "\n",
- "2 69\n",
- "\n",
- "2 70\n",
- "\n",
- "2 71\n",
- "\n",
- "2 72\n",
- "\n",
- "2 73\n",
- "\n",
- "2 74\n",
- "\n",
- "2 75\n",
- "\n",
- "2 76\n",
- "\n",
- "2 77\n",
- "\n",
- "2 78\n",
- "\n",
- "2 79\n",
- "\n",
- "2 80\n",
- "\n",
- "2 81\n",
- "\n",
- "2 82\n",
- "\n",
- "2 83\n",
- "\n",
- "2 84\n",
- "\n",
- "2 85\n",
- "\n",
- "2 86\n",
- "\n",
- "2 87\n",
- "\n",
- "2 88\n",
- "\n",
- "2 89\n",
- "\n",
- "2 90\n",
- "\n",
- "2 91\n",
- "\n",
- "2 92\n",
- "\n",
- "2 93\n",
- "\n",
- "2 94\n",
- "\n",
- "2 95\n",
- "\n",
- "2 96\n",
- "\n",
- "2 97\n",
- "\n",
- "2 98\n",
- "\n",
- "2 99\n",
- "\n",
- "2 100\n",
- "\n",
- "2 101\n",
- "\n",
- "2 102\n",
- "\n",
- "2 103\n",
- "\n",
- "2 104\n",
- "\n",
- "2 105\n",
- "\n",
- "2 106\n",
- "\n",
- "2 107\n",
- "\n",
- "2 108\n",
- "\n",
- "2 109\n",
- "\n",
- "2 110\n",
- "\n",
- "2 111\n",
- "\n",
- "2 112\n",
- "\n",
- "2 113\n",
- "\n",
- "2 114\n",
- "\n",
- "2 115\n",
- "\n",
- "2 116\n",
- "\n",
- "2 117\n",
- "\n",
- "2 118\n",
- "\n",
- "2 119\n",
- "\n",
- "2 120\n",
- "\n",
- "2 121\n",
- "\n",
- "2 122\n",
- "\n",
- "2 123\n",
- "\n",
- "2 124\n",
- "\n",
- "2 125\n",
- "\n",
- "2 126\n",
- "\n",
- "2 127\n",
- "\n",
- "2 128\n",
- "\n",
- "2 129\n",
- "\n",
- "2 130\n",
- "\n",
- "2 131\n",
- "\n",
- "2 132\n",
- "\n",
- "2 133\n",
- "\n",
- "2 134\n",
- "\n",
- "2 135\n",
- "\n",
- "2 136\n",
- "\n",
- "2 137\n",
- "\n",
- "2 138\n",
- "\n",
- "2 139\n",
- "\n",
- "2 140\n",
- "\n",
- "2 141\n",
- "\n",
- "2 142\n",
- "\n",
- "2 143\n",
- "\n",
- "2 144\n",
- "\n",
- "2 145\n",
- "\n",
- "2 146\n",
- "\n",
- "2 147\n",
- "\n",
- "2 148\n",
- "\n",
- "2 149\n",
- "\n",
- "3 151\n",
- "\n",
- "3 152\n",
- "\n",
- "3 153\n",
- "\n",
- "3 154\n",
- "\n",
- "3 155\n",
- "\n",
- "3 156\n",
- "\n",
- "3 157\n",
- "\n",
- "3 158\n",
- "\n",
- "3 159\n",
- "\n",
- "3 160\n",
- "\n",
- "3 161\n",
- "\n",
- "3 162\n",
- "\n",
- "3 163\n",
- "\n",
- "3 164\n",
- "\n",
- "3 165\n",
- "\n",
- "3 166\n",
- "\n",
- "3 167\n",
- "\n",
- "3 168\n",
- "\n",
- "3 169\n",
- "\n",
- "3 170\n",
- "\n",
- "3 171\n",
- "\n",
- "3 172\n",
- "\n",
- "3 173\n",
- "\n",
- "3 174\n",
- "\n",
- "3 175\n",
- "\n",
- "3 176\n",
- "\n",
- "3 177\n",
- "\n",
- "3 178\n",
- "\n",
- "3 179\n",
- "\n",
- "3 180\n",
- "\n",
- "3 181\n",
- "\n",
- "3 182\n",
- "\n",
- "3 183\n",
- "\n",
- "3 184\n",
- "\n",
- "3 185\n",
- "\n",
- "3 186\n",
- "\n",
- "3 187\n",
- "\n",
- "3 188\n",
- "\n",
- "3 189\n",
- "\n",
- "3 190\n",
- "\n",
- "3 191\n",
- "\n",
- "3 192\n",
- "\n",
- "3 193\n",
- "\n",
- "3 194\n",
- "\n",
- "3 195\n",
- "\n",
- "3 196\n",
- "\n",
- "3 197\n",
- "\n",
- "3 198\n",
- "\n",
- "3 199\n",
- "\n",
- "3 200\n",
- "\n",
- "3 201\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 16
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:108"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#program using continue statement\n",
- "for i in range(1,3,1):\n",
- " for j in range(1,3,1):\n",
- " if i==j:\n",
- " continue #it will again send back to loop without executing succeeding statements\n",
- " print \"%d %d\\n\" % (i,j)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "1 2\n",
- "\n",
- "2 1\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 17
- }
- ],
- "metadata": {}
- }
- ]
+{ + "metadata": { + "name": "", + "signature": "sha256:2f63105cbe9f49f191de7b50016730212f162ceb128a95b6bc9a54da3a6efc20" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "CHAPTER 3: THE LOOP CONTROL STRUCTURE" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:89-90" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "count=1\n", + "while count<=3: #while loop codition check\n", + " print \"Enter values of p,n and r\"\n", + " p=eval(raw_input())\n", + " n=eval(raw_input())\n", + " r=eval(raw_input())\n", + " si=p*n*r/100\n", + " print \"Simple interest=Rs.%f\" % (si)\n", + " count=count+1 #increment" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter values of p,n and r\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1000\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "13.5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Simple interest=Rs.675.000000\n", + "Enter values of p,n and r\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2000\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "13.5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Simple interest=Rs.1350.000000\n", + "Enter values of p,n and r\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3500\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3.5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Simple interest=Rs.612.500000\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:92" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "i=1\n", + "while i<=10:\n", + " print \"%d\\n\" % (i) #there is no increment/decrement thats why indefinite loop" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:93" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "i=1\n", + "while i<=10:\n", + " print \"\\n\" ,i\n", + " i=i+1" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "1\n", + "\n", + "2\n", + "\n", + "3\n", + "\n", + "4\n", + "\n", + "5\n", + "\n", + "6\n", + "\n", + "7\n", + "\n", + "8\n", + "\n", + "9\n", + "\n", + "10\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:93" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "i=5\n", + "while i>=1:\n", + " print \"Make the computer literate!\\n\"\n", + " i=i-1" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Make the computer literate!\n", + "\n", + "Make the computer literate!\n", + "\n", + "Make the computer literate!\n", + "\n", + "Make the computer literate!\n", + "\n", + "Make the computer literate!\n", + "\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:93" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "a=10.0\n", + "while a<=10.5:\n", + " print \"Raindrops on roses...\"\n", + " print \"...and whiskers on kittens\\n\"\n", + " a=a+0.1" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Raindrops on roses...\n", + "...and whiskers on kittens\n", + "\n", + "Raindrops on roses...\n", + "...and whiskers on kittens\n", + "\n", + "Raindrops on roses...\n", + "...and whiskers on kittens\n", + "\n", + "Raindrops on roses...\n", + "...and whiskers on kittens\n", + "\n", + "Raindrops on roses...\n", + "...and whiskers on kittens\n", + "\n", + "Raindrops on roses...\n", + "...and whiskers on kittens\n", + "\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:94" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "i=1\n", + "while i<=32767: #print numbers from 1 to 32767\n", + " print \"%d\\n\" ,i \n", + " i=i+1" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:95" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "i=1 \n", + "while i<=10:\n", + " print i\n", + " i=i+1" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n", + "2\n", + "3\n", + "4\n", + "5\n", + "6\n", + "7\n", + "8\n", + "9\n", + "10\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:96" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "i=1\n", + "while i<=10:\n", + " print \"\\n\" ,i\n", + " i+=1 #increment short hand operator" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "1\n", + "\n", + "2\n", + "\n", + "3\n", + "\n", + "4\n", + "\n", + "5\n", + "\n", + "6\n", + "\n", + "7\n", + "\n", + "8\n", + "\n", + "9\n", + "\n", + "10\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:98" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "for count in range(1,4,1): #for loop\n", + " print \"Enter values of p,n and r\"\n", + " p=eval(raw_input())\n", + " n=eval(raw_input())\n", + " r=eval(raw_input())\n", + " si=p*n*r/100\n", + " print \"Simple Interest=Rs.%f\" % (si)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter values of p,n and r\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1000\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "13.5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Simple Interest=Rs.675.000000\n", + "Enter values of p,n and r\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2000\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "13.5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Simple Interest=Rs.1350.000000\n", + "Enter values of p,n and r\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3500\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3.5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Simple Interest=Rs.612.500000\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:101" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "for i in range(1,11,1):\n", + " print \"%d\\n\" % (i)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n", + "\n", + "2\n", + "\n", + "3\n", + "\n", + "4\n", + "\n", + "5\n", + "\n", + "6\n", + "\n", + "7\n", + "\n", + "8\n", + "\n", + "9\n", + "\n", + "10\n", + "\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:101" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "for i in range(1,11):\n", + " print \"\\n\" ,i\n", + " i=i+1" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "1\n", + "\n", + "2\n", + "\n", + "3\n", + "\n", + "4\n", + "\n", + "5\n", + "\n", + "6\n", + "\n", + "7\n", + "\n", + "8\n", + "\n", + "9\n", + "\n", + "10\n" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:101" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "i=1\n", + "for i in range(i,11,1):\n", + " print \"%d\\n\" % (i)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n", + "\n", + "2\n", + "\n", + "3\n", + "\n", + "4\n", + "\n", + "5\n", + "\n", + "6\n", + "\n", + "7\n", + "\n", + "8\n", + "\n", + "9\n", + "\n", + "10\n", + "\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:102" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "i=1\n", + "for i in range(i,11):\n", + " print \"%d\\n\" % (i)\n", + " i=i+1" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n", + "\n", + "2\n", + "\n", + "3\n", + "\n", + "4\n", + "\n", + "5\n", + "\n", + "6\n", + "\n", + "7\n", + "\n", + "8\n", + "\n", + "9\n", + "\n", + "10\n", + "\n" + ] + } + ], + "prompt_number": 11 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:103" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "for r in range(1,4,1): #outer loop\n", + " for c in range(1,3,1): #inner loop \n", + " sum=r+c\n", + " print \"r=%d c=%d sum=%d\\n\" % (r,c,sum)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "r=1 c=1 sum=2\n", + "\n", + "r=1 c=2 sum=3\n", + "\n", + "r=2 c=1 sum=3\n", + "\n", + "r=2 c=2 sum=4\n", + "\n", + "r=3 c=1 sum=4\n", + "\n", + "r=3 c=2 sum=5\n", + "\n" + ] + } + ], + "prompt_number": 12 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:104-105" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "while(1): #do-while loop is not present in python\n", + " print \"Enter a number\"\n", + " num=eval(raw_input())\n", + " print \"square of %d is %d\\n\" % (num,num*num)\n", + " print \"Want to enter another number y/n\"\n", + " another=raw_input()\n", + " if(another!='y'):\n", + " break" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a number\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "square of 5 is 25\n", + "\n", + "Want to enter another number y/n\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "y\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a number\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "7\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "square of 7 is 49\n", + "\n", + "Want to enter another number y/n\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "n\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:105" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "another='y'\n", + "for i in range(1,1000): #check if another is in word\n", + " if another=='y':\n", + " print \"Enter a number\"\n", + " num=eval(raw_input())\n", + " print \"square of %d is %d\\n\" % (num,num*num)\n", + " print \"Want to enter another number y/n\"\n", + " another=raw_input()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a number\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "square of 5 is 25\n", + "\n", + "Want to enter another number y/n\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "y\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a number\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "7\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "square of 7 is 49\n", + "\n", + "Want to enter another number y/n\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "n\n" + ] + } + ], + "prompt_number": 13 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:105-106" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "another='y'\n", + "while(another=='y'):\n", + " print \"Enter a number\"\n", + " num=eval(raw_input())\n", + " print \"square of %d is %d\\n\" % (num,num*num)\n", + " print \"Want to enter another number y/n\"\n", + " another=raw_input()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a number\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "square of 5 is 25\n", + "\n", + "Want to enter another number y/n\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "y\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a number\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "7\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "square of 7 is 49\n", + "\n", + "Want to enter another number y/n\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "n\n" + ] + } + ], + "prompt_number": 14 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:106" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "print \"Enter a number\"\n", + "num=eval(raw_input())\n", + "i=2\n", + "while i<=num-1:\n", + " if num%i==0:\n", + " print \"Not a prime number\\n\"\n", + " break #exit the loop\n", + " i+=1\n", + "if i==num:\n", + " print \"Prime number\\n\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a number\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "11\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Prime number\n", + "\n" + ] + } + ], + "prompt_number": 15 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:107" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "i=1\n", + "j=1\n", + "while (i<=100):\n", + " i+=1\n", + " while (j<=200):\n", + " j+=1\n", + " if (j==150):\n", + " break #it will terminate the inner loop only\n", + " else:\n", + " print \"%d %d\\n\" % (i,j)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "2 2\n", + "\n", + "2 3\n", + "\n", + "2 4\n", + "\n", + "2 5\n", + "\n", + "2 6\n", + "\n", + "2 7\n", + "\n", + "2 8\n", + "\n", + "2 9\n", + "\n", + "2 10\n", + "\n", + "2 11\n", + "\n", + "2 12\n", + "\n", + "2 13\n", + "\n", + "2 14\n", + "\n", + "2 15\n", + "\n", + "2 16\n", + "\n", + "2 17\n", + "\n", + "2 18\n", + "\n", + "2 19\n", + "\n", + "2 20\n", + "\n", + "2 21\n", + "\n", + "2 22\n", + "\n", + "2 23\n", + "\n", + "2 24\n", + "\n", + "2 25\n", + "\n", + "2 26\n", + "\n", + "2 27\n", + "\n", + "2 28\n", + "\n", + "2 29\n", + "\n", + "2 30\n", + "\n", + "2 31\n", + "\n", + "2 32\n", + "\n", + "2 33\n", + "\n", + "2 34\n", + "\n", + "2 35\n", + "\n", + "2 36\n", + "\n", + "2 37\n", + "\n", + "2 38\n", + "\n", + "2 39\n", + "\n", + "2 40\n", + "\n", + "2 41\n", + "\n", + "2 42\n", + "\n", + "2 43\n", + "\n", + "2 44\n", + "\n", + "2 45\n", + "\n", + "2 46\n", + "\n", + "2 47\n", + "\n", + "2 48\n", + "\n", + "2 49\n", + "\n", + "2 50\n", + "\n", + "2 51\n", + "\n", + "2 52\n", + "\n", + "2 53\n", + "\n", + "2 54\n", + "\n", + "2 55\n", + "\n", + "2 56\n", + "\n", + "2 57\n", + "\n", + "2 58\n", + "\n", + "2 59\n", + "\n", + "2 60\n", + "\n", + "2 61\n", + "\n", + "2 62\n", + "\n", + "2 63\n", + "\n", + "2 64\n", + "\n", + "2 65\n", + "\n", + "2 66\n", + "\n", + "2 67\n", + "\n", + "2 68\n", + "\n", + "2 69\n", + "\n", + "2 70\n", + "\n", + "2 71\n", + "\n", + "2 72\n", + "\n", + "2 73\n", + "\n", + "2 74\n", + "\n", + "2 75\n", + "\n", + "2 76\n", + "\n", + "2 77\n", + "\n", + "2 78\n", + "\n", + "2 79\n", + "\n", + "2 80\n", + "\n", + "2 81\n", + "\n", + "2 82\n", + "\n", + "2 83\n", + "\n", + "2 84\n", + "\n", + "2 85\n", + "\n", + "2 86\n", + "\n", + "2 87\n", + "\n", + "2 88\n", + "\n", + "2 89\n", + "\n", + "2 90\n", + "\n", + "2 91\n", + "\n", + "2 92\n", + "\n", + "2 93\n", + "\n", + "2 94\n", + "\n", + "2 95\n", + "\n", + "2 96\n", + "\n", + "2 97\n", + "\n", + "2 98\n", + "\n", + "2 99\n", + "\n", + "2 100\n", + "\n", + "2 101\n", + "\n", + "2 102\n", + "\n", + "2 103\n", + "\n", + "2 104\n", + "\n", + "2 105\n", + "\n", + "2 106\n", + "\n", + "2 107\n", + "\n", + "2 108\n", + "\n", + "2 109\n", + "\n", + "2 110\n", + "\n", + "2 111\n", + "\n", + "2 112\n", + "\n", + "2 113\n", + "\n", + "2 114\n", + "\n", + "2 115\n", + "\n", + "2 116\n", + "\n", + "2 117\n", + "\n", + "2 118\n", + "\n", + "2 119\n", + "\n", + "2 120\n", + "\n", + "2 121\n", + "\n", + "2 122\n", + "\n", + "2 123\n", + "\n", + "2 124\n", + "\n", + "2 125\n", + "\n", + "2 126\n", + "\n", + "2 127\n", + "\n", + "2 128\n", + "\n", + "2 129\n", + "\n", + "2 130\n", + "\n", + "2 131\n", + "\n", + "2 132\n", + "\n", + "2 133\n", + "\n", + "2 134\n", + "\n", + "2 135\n", + "\n", + "2 136\n", + "\n", + "2 137\n", + "\n", + "2 138\n", + "\n", + "2 139\n", + "\n", + "2 140\n", + "\n", + "2 141\n", + "\n", + "2 142\n", + "\n", + "2 143\n", + "\n", + "2 144\n", + "\n", + "2 145\n", + "\n", + "2 146\n", + "\n", + "2 147\n", + "\n", + "2 148\n", + "\n", + "2 149\n", + "\n", + "3 151\n", + "\n", + "3 152\n", + "\n", + "3 153\n", + "\n", + "3 154\n", + "\n", + "3 155\n", + "\n", + "3 156\n", + "\n", + "3 157\n", + "\n", + "3 158\n", + "\n", + "3 159\n", + "\n", + "3 160\n", + "\n", + "3 161\n", + "\n", + "3 162\n", + "\n", + "3 163\n", + "\n", + "3 164\n", + "\n", + "3 165\n", + "\n", + "3 166\n", + "\n", + "3 167\n", + "\n", + "3 168\n", + "\n", + "3 169\n", + "\n", + "3 170\n", + "\n", + "3 171\n", + "\n", + "3 172\n", + "\n", + "3 173\n", + "\n", + "3 174\n", + "\n", + "3 175\n", + "\n", + "3 176\n", + "\n", + "3 177\n", + "\n", + "3 178\n", + "\n", + "3 179\n", + "\n", + "3 180\n", + "\n", + "3 181\n", + "\n", + "3 182\n", + "\n", + "3 183\n", + "\n", + "3 184\n", + "\n", + "3 185\n", + "\n", + "3 186\n", + "\n", + "3 187\n", + "\n", + "3 188\n", + "\n", + "3 189\n", + "\n", + "3 190\n", + "\n", + "3 191\n", + "\n", + "3 192\n", + "\n", + "3 193\n", + "\n", + "3 194\n", + "\n", + "3 195\n", + "\n", + "3 196\n", + "\n", + "3 197\n", + "\n", + "3 198\n", + "\n", + "3 199\n", + "\n", + "3 200\n", + "\n", + "3 201\n", + "\n" + ] + } + ], + "prompt_number": 16 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:108" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "for i in range(1,3,1):\n", + " for j in range(1,3,1):\n", + " if i==j:\n", + " continue #it will again send back to loop without executing succeeding statements\n", + " print \"%d %d\\n\" % (i,j)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "1 2\n", + "\n", + "2 1\n", + "\n" + ] + } + ], + "prompt_number": 17 + } + ], + "metadata": {} + } + ] }
\ No newline at end of file diff --git a/ANSI_C_Programming/chapter4.ipynb b/ANSI_C_Programming/chapter4.ipynb index a3d738f1..3a15cce2 100644 --- a/ANSI_C_Programming/chapter4.ipynb +++ b/ANSI_C_Programming/chapter4.ipynb @@ -1,389 +1,390 @@ -{
- "metadata": {
- "name": "chapter4.ipynb"
- },
- "nbformat": 3,
- "nbformat_minor": 0,
- "worksheets": [
- {
- "cells": [
- {
- "cell_type": "heading",
- "level": 1,
- "metadata": {},
- "source": [
- "CHAPTER 4: THE CASE CONTROL STRUCTURE"
- ]
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:126"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Switch case statement using dictionary\n",
- "i=2\n",
- "def switch(i):\n",
- " return {True: 'I am in default\\n', #default case\n",
- " i==1: 'I am in case1\\n',\n",
- " i==2: 'I am in case2\\n',\n",
- " i==3: 'I am in case3\\n',\n",
- " }[True]\n",
- "print switch(i)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "I am in case2\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 1
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:128"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Switch case using scrambled case order\n",
- "i=22\n",
- "def switch(i):\n",
- " return{True: 'I am in default\\n',\n",
- " i==121: 'I am in case 121\\n',\n",
- " i==7: 'I am in case 7\\n',\n",
- " i==22: 'I am in case 22\\n',\n",
- " }[True]\n",
- "print switch(i)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "I am in case 22\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 2
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:128-129"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Switch case using char values in case\n",
- "c='x'\n",
- "def switch(c):\n",
- " return {True: 'I am in default\\n',\n",
- " c=='v': 'I am in case v\\n',\n",
- " c=='a': 'I am in case a\\n',\n",
- " c=='x': 'I am in case x\\n',\n",
- " }[True]\n",
- "print switch(c)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "I am in case x\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 3
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:129-130"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Executing common set of statements for multiple cases\n",
- "print \"Enter any one of the alphabets a,b,or c\"\n",
- "ch=raw_input()\n",
- "def casea():\n",
- " print \"\"\n",
- "def caseb():\n",
- " print \"\"\n",
- "def casec():\n",
- " print \"\"\n",
- "def switch(ch):\n",
- " return {True: 'wish you knew what are alphabets\\n',\n",
- " ch=='a' or ch=='A': 'a as in ashar\\n',\n",
- " ch=='b' or ch=='B': 'b as in brain\\n',\n",
- " ch=='c' or ch=='C': 'c as in cookie\\n',\n",
- " }[True]\n",
- "print switch(ch)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter any one of the alphabets a,b,or c\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "B\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "b as in brain\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 6
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:130"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Iniatialisation according to cases\n",
- "print \"Enter value of i\"\n",
- "i=eval(raw_input())\n",
- "def switch(i):\n",
- " print \"Hello\\n\" #It's python,not C :)\n",
- " try:\n",
- " return {1: a,\n",
- " 2: b,\n",
- " }[i]()\n",
- " except:\n",
- " print \"wrong choice\" #print message in default case\n",
- "def a():\n",
- " j=10\n",
- " print j #print j as 10 if i=1\n",
- "def b():\n",
- " j=20 \n",
- " print j #print j as 20 if i=2\n",
- "switch(i)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter value of i\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "1\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Hello\n",
- "\n",
- "10\n"
- ]
- }
- ],
- "prompt_number": 10
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:133"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Conditional printing of messages\n",
- "import sys\n",
- "print \"Enter the number of goals scored against india\"\n",
- "goals=eval(raw_input())\n",
- "if goals<=5:\n",
- " print \"To err is human!\\n\"\n",
- "else:\n",
- " print \"About time soccer players learnt C\\n\"\n",
- " print \"and said goodbye! adieu! to soccer\\n\""
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter the number of goals scored against india\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "7\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "About time soccer players learnt C\n",
- "\n",
- "and said goodbye! adieu! to soccer\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 11
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:134-135"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Program to conditional print of numbers\n",
- "for i in range(1,4,1):\n",
- " for j in range(1,4,1):\n",
- " for k in range(1,4,1):\n",
- " if (i==3 and j==3 and k==3):\n",
- " print \"Out of the loop at last!\\n\"\n",
- " else:\n",
- " print \"%d %d %d\\n\" % (i,j,k)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "1 1 1\n",
- "\n",
- "1 1 2\n",
- "\n",
- "1 1 3\n",
- "\n",
- "1 2 1\n",
- "\n",
- "1 2 2\n",
- "\n",
- "1 2 3\n",
- "\n",
- "1 3 1\n",
- "\n",
- "1 3 2\n",
- "\n",
- "1 3 3\n",
- "\n",
- "2 1 1\n",
- "\n",
- "2 1 2\n",
- "\n",
- "2 1 3\n",
- "\n",
- "2 2 1\n",
- "\n",
- "2 2 2\n",
- "\n",
- "2 2 3\n",
- "\n",
- "2 3 1\n",
- "\n",
- "2 3 2\n",
- "\n",
- "2 3 3\n",
- "\n",
- "3 1 1\n",
- "\n",
- "3 1 2\n",
- "\n",
- "3 1 3\n",
- "\n",
- "3 2 1\n",
- "\n",
- "3 2 2\n",
- "\n",
- "3 2 3\n",
- "\n",
- "3 3 1\n",
- "\n",
- "3 3 2\n",
- "\n",
- "Out of the loop at last!\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 12
- }
- ],
- "metadata": {}
- }
- ]
+{ + "metadata": { + "name": "", + "signature": "sha256:c88e4872fc4418c2962652967a7ca0a93c345eb9f3bd38488ba817f74e5fc2eb" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "CHAPTER 4: THE CASE CONTROL STRUCTURE" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:126" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "i=2\n", + "def switch(i):\n", + " return {True: 'I am in default\\n', #default case\n", + " i==1: 'I am in case1\\n',\n", + " i==2: 'I am in case2\\n',\n", + " i==3: 'I am in case3\\n',\n", + " }[True]\n", + "print switch(i)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "I am in case2\n", + "\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:128" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "i=22\n", + "def switch(i):\n", + " return{True: 'I am in default\\n',\n", + " i==121: 'I am in case 121\\n',\n", + " i==7: 'I am in case 7\\n',\n", + " i==22: 'I am in case 22\\n',\n", + " }[True]\n", + "print switch(i)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "I am in case 22\n", + "\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:128-129" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "c='x'\n", + "def switch(c):\n", + " return {True: 'I am in default\\n',\n", + " c=='v': 'I am in case v\\n',\n", + " c=='a': 'I am in case a\\n',\n", + " c=='x': 'I am in case x\\n',\n", + " }[True]\n", + "print switch(c)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "I am in case x\n", + "\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:129-130" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "print \"Enter any one of the alphabets a,b,or c\"\n", + "ch=raw_input()\n", + "def casea():\n", + " print \"\"\n", + "def caseb():\n", + " print \"\"\n", + "def casec():\n", + " print \"\"\n", + "def switch(ch):\n", + " return {True: 'wish you knew what are alphabets\\n',\n", + " ch=='a' or ch=='A': 'a as in ashar\\n',\n", + " ch=='b' or ch=='B': 'b as in brain\\n',\n", + " ch=='c' or ch=='C': 'c as in cookie\\n',\n", + " }[True]\n", + "print switch(ch)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter any one of the alphabets a,b,or c\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "B\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "b as in brain\n", + "\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:130" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "print \"Enter value of i\"\n", + "i=eval(raw_input())\n", + "def switch(i):\n", + " print \"Hello\\n\" #It's python,not C :)\n", + " try:\n", + " return {1: a,\n", + " 2: b,\n", + " }[i]()\n", + " except:\n", + " print \"wrong choice\" #print message in default case\n", + "def a():\n", + " j=10\n", + " print j #print j as 10 if i=1\n", + "def b():\n", + " j=20 \n", + " print j #print j as 20 if i=2\n", + "switch(i)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter value of i\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Hello\n", + "\n", + "10\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:133" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "import sys\n", + "print \"Enter the number of goals scored against india\"\n", + "goals=eval(raw_input())\n", + "if goals<=5:\n", + " print \"To err is human!\\n\"\n", + "else:\n", + " print \"About time soccer players learnt C\\n\"\n", + " print \"and said goodbye! adieu! to soccer\\n\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the number of goals scored against india\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "7\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "About time soccer players learnt C\n", + "\n", + "and said goodbye! adieu! to soccer\n", + "\n" + ] + } + ], + "prompt_number": 11 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:134-135" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "for i in range(1,4,1):\n", + " for j in range(1,4,1):\n", + " for k in range(1,4,1):\n", + " if (i==3 and j==3 and k==3):\n", + " print \"Out of the loop at last!\\n\"\n", + " else:\n", + " print \"%d %d %d\\n\" % (i,j,k)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "1 1 1\n", + "\n", + "1 1 2\n", + "\n", + "1 1 3\n", + "\n", + "1 2 1\n", + "\n", + "1 2 2\n", + "\n", + "1 2 3\n", + "\n", + "1 3 1\n", + "\n", + "1 3 2\n", + "\n", + "1 3 3\n", + "\n", + "2 1 1\n", + "\n", + "2 1 2\n", + "\n", + "2 1 3\n", + "\n", + "2 2 1\n", + "\n", + "2 2 2\n", + "\n", + "2 2 3\n", + "\n", + "2 3 1\n", + "\n", + "2 3 2\n", + "\n", + "2 3 3\n", + "\n", + "3 1 1\n", + "\n", + "3 1 2\n", + "\n", + "3 1 3\n", + "\n", + "3 2 1\n", + "\n", + "3 2 2\n", + "\n", + "3 2 3\n", + "\n", + "3 3 1\n", + "\n", + "3 3 2\n", + "\n", + "Out of the loop at last!\n", + "\n" + ] + } + ], + "prompt_number": 12 + } + ], + "metadata": {} + } + ] }
\ No newline at end of file diff --git a/ANSI_C_Programming/chapter5.ipynb b/ANSI_C_Programming/chapter5.ipynb index aadabff0..6e2f2667 100644 --- a/ANSI_C_Programming/chapter5.ipynb +++ b/ANSI_C_Programming/chapter5.ipynb @@ -1,971 +1,970 @@ -{
- "metadata": {
- "name": "chapter5.ipynb"
- },
- "nbformat": 3,
- "nbformat_minor": 0,
- "worksheets": [
- {
- "cells": [
- {
- "cell_type": "heading",
- "level": 1,
- "metadata": {},
- "source": [
- "CHAPTER 5: FUNCTIONS AND POINTERS"
- ]
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:144"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Defining & calling a function\n",
- "def message(): #function definition\n",
- " print \"Smile,and the world smiles with you...\\n\"\n",
- "message() #function call\n",
- "print \"Cry, and you stop the monotony!\\n\""
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Smile,and the world smiles with you...\n",
- "\n",
- "Cry, and you stop the monotony!\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 1
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:146"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Calling of multiple functions\n",
- "def italy():\n",
- " print \"I am in italy\\n\"\n",
- "def brazil():\n",
- " print \"I am in brazil\\n\"\n",
- "def argentina():\n",
- " print \"I am in argentina\\n\"\n",
- "print \"I am in main\\n\"\n",
- "italy(); #italy() will be called\n",
- "brazil(); #brazil() will be called\n",
- "argentina(); #argentina() will be called"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "I am in main\n",
- "\n",
- "I am in italy\n",
- "\n",
- "I am in brazil\n",
- "\n",
- "I am in argentina\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 2
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:147"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Calling of function inside another function\n",
- "def italy():\n",
- " print \"I am in italy\\n\"\n",
- " brazil(); #it will call brazil()\n",
- " print \"I am back in italy\\n\"\n",
- "def brazil():\n",
- " print \"I am in brazil\\n\"\n",
- " argentina(); #it will call argentina()\n",
- "def argentina():\n",
- " print \"I am in argentina\\n\"\n",
- "print \"I am in main\\n\"\n",
- "italy(); #italy() is called\n",
- "print \"I am finally back in main\\n\""
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "I am in main\n",
- "\n",
- "I am in italy\n",
- "\n",
- "I am in brazil\n",
- "\n",
- "I am in argentina\n",
- "\n",
- "I am back in italy\n",
- "\n",
- "I am finally back in main\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 3
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:148"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Calling of main from another function\n",
- "def message():\n",
- " print \"Can't imagine life without C\\n\"\n",
- " main() #it will call back main()\n",
- "def main():\n",
- " message()\n",
- "main()"
- ],
- "language": "python",
- "metadata": {},
- "outputs": []
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:149"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Calling a function multiple number of times\n",
- "def message():\n",
- " print \"Jewel Thief!!\\n\"\n",
- "message() #1st call\n",
- "message() #2nd call"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Jewel Thief!!\n",
- "\n",
- "Jewel Thief!!\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 4
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:149"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Function calls doesn't depend on the order of functions definition\n",
- "def message2():\n",
- " print \"But the butter was bitter\\n\"\n",
- "def message1():\n",
- " print \"Mary bought some butter\\n\"\n",
- "message1()\n",
- "message2()"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Mary bought some butter\n",
- "\n",
- "But the butter was bitter\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 5
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:151-152"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Sending and receiving values between functions\n",
- "def calsum(x,y,z): #parameterized function\n",
- " d=x+y+z\n",
- " return d\n",
- "print \"Enter any three numbers\"\n",
- "a=eval(raw_input())\n",
- "b=eval(raw_input())\n",
- "c=eval(raw_input())\n",
- "sum=calsum(a,b,c) #passing values as arguments\n",
- "print \"Sum=%d\\n\" % (sum)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter any three numbers\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "10\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "20\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "30\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Sum=60\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 6
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:153"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#A function can have multiple return statements\n",
- "def fun():\n",
- " print \"Enter any number\"\n",
- " n=eval(raw_input())\n",
- " if (n>=10 and n<=90):\n",
- " return n\n",
- " else:\n",
- " return n+32\n",
- "fun()"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter any number\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "100\n"
- ]
- },
- {
- "metadata": {},
- "output_type": "pyout",
- "prompt_number": 7,
- "text": [
- "132"
- ]
- }
- ],
- "prompt_number": 7
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:154"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Changing formal argument in function doesn't affect the actual argument\n",
- "def fun(b):\n",
- " b=60\n",
- " print \"%d\\n\" % (b) #prints 60\n",
- "a=30\n",
- "fun(a) #prints 30\n",
- "print \"%d\\n\" % (a)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "60\n",
- "\n",
- "30\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 8
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:155"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Program explaining scope of a variable\n",
- "def display(j):\n",
- " k=35\n",
- " print \"%d\\n\" % (j) #we can't print i directly here because scope of variable is local by default\n",
- " print \"%d\\n\" % (k)\n",
- "i=20\n",
- "display(i)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "20\n",
- "\n",
- "35\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 9
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:158-159"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Square of a floating point number using a function\n",
- "def square(x):\n",
- " y=x*x\n",
- " return y\n",
- "print \"Enter any number\"\n",
- "a=eval(raw_input())\n",
- "b=square(a)\n",
- "print \"Square of %f is %f\\n\" % (a,b)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter any number\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "2.5\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Square of 2.500000 is 6.250000\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 10
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:161"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Printing address location of a variable\n",
- "#Note:There is no concept of pointer in python thats why i am only writing it to get the similar output\n",
- "i=3\n",
- "print \"Address of i=%u\\n\" % (id(i)) #id() will return the loaction of a variable\n",
- "print \"Value of i=%d\\n\" % (i)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Address of i=20688560\n",
- "\n",
- "Value of i=3\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 12
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:161"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Program to print address & value\n",
- "i=3\n",
- "print \"Address of i=%u\\n\" % (id(i))\n",
- "print \"Value of i=%d\\n\" % (i)\n",
- "print \"Value of i=%d\\n\" % (i)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Address of i=20688560\n",
- "\n",
- "Value of i=3\n",
- "\n",
- "Value of i=3\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 13
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:162"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Assigning address to other variable and printing them\n",
- "i=3\n",
- "j=id(i)\n",
- "print \"Address of i=%u\\n\" % (id(i)) #print address of i\n",
- "print \"Address of i=%u\\n\" % (j) #print address of i\n",
- "print \"Address of j=%u\\n\" % (id(j)) #print address of j\n",
- "print \"Value of j=%u\\n\" % (j) #print value of j\n",
- "print \"Value of i=%d\\n\" % (i)\n",
- "print \"Value of i=%d\\n\" % (i)\n",
- "print \"Value of i=%d\\n\" % (i)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Address of i=20688560\n",
- "\n",
- "Address of i=20688560\n",
- "\n",
- "Address of j=88986780\n",
- "\n",
- "Value of j=20688560\n",
- "\n",
- "Value of i=3\n",
- "\n",
- "Value of i=3\n",
- "\n",
- "Value of i=3\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 14
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:164"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Variable containg address of another variable\n",
- "i=3\n",
- "j=id(i)\n",
- "k=id(j)\n",
- "print \"Address of i=%u\\n\" % (id(i)) #print address of i\n",
- "print \"Address of i=%u\\n\" % (j) #print address of i\n",
- "print \"Address of i=%u\\n\" % (j) #print address of i\n",
- "print \"Address of j=%u\\n\" % (id(j)) #print address of i\n",
- "print \"Address of j=%u\\n\" % (k) #print address of j\n",
- "print \"Address of k=%u\\n\" % (id(k)) #print address of k\n",
- "print \"Value of j=%u\\n\" % (j) #print value of j\n",
- "print \"Value of k=%u\\n\" % (k) #print value of k\n",
- "print \"Value of i=%u\\n\" % (i) #print value of i\n",
- "print \"Value of i=%u\\n\" % (i)\n",
- "print \"Value of i=%u\\n\" % (i)\n",
- "print \"Value of i=%u\\n\" % (i)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Address of i=20688560\n",
- "\n",
- "Address of i=20688560\n",
- "\n",
- "Address of i=20688560\n",
- "\n",
- "Address of j=88986672\n",
- "\n",
- "Address of j=88986672\n",
- "\n",
- "Address of k=88986708\n",
- "\n",
- "Value of j=20688560\n",
- "\n",
- "Value of k=88986672\n",
- "\n",
- "Value of i=3\n",
- "\n",
- "Value of i=3\n",
- "\n",
- "Value of i=3\n",
- "\n",
- "Value of i=3\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 15
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:166"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Swapping of two variables\n",
- "def swapv(x,y):\n",
- " x,y=y,x\n",
- " print \"x=%d y=%d\\n\" % (x,y)\n",
- "a=10\n",
- "b=20\n",
- "swapv(a,b)\n",
- "print \"a=%d b=%d\\n\" % (a,b)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "x=20 y=10\n",
- "\n",
- "a=10 b=20\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 1
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE-167"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#program to swap 2 variables\n",
- "#Note:There is no concept of pointer in python that's why i used global variables to get the same output\n",
- "def swapr():\n",
- " global a,b #global declaration\n",
- " a,b=b,a\n",
- "a=10\n",
- "b=20\n",
- "swapr()\n",
- "print \"a=%d b=%d\\n\" % (a,b)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "a=20 b=10\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 1
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:167-168"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Program to print area & perimeter of circle\n",
- "def areaperi(r,a,p):\n",
- " a=3.14*r*r #formula of area\n",
- " p=2*3.14*r #formula of perimeter\n",
- " print \"Area=%f\\n\" % (a)\n",
- " print \"Perimeter=%f\\n\" % (p)\n",
- "area=0\n",
- "perimeter=0\n",
- "print \"Enter radius of a circle\"\n",
- "radius=eval(raw_input())\n",
- "areaperi(radius,area,perimeter)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter radius of a circle\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "5\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Area=78.500000\n",
- "\n",
- "Perimeter=31.400000\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 22
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:169"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Finding factorial of a number without recursion\n",
- "def factorial(x):\n",
- " f=1\n",
- " for i in range(x,1,-1):\n",
- " f=f*i\n",
- " return f\n",
- "print \"Enter any number\"\n",
- "a=eval(raw_input())\n",
- "fact=factorial(a)\n",
- "print \"Factorial value=%d\\n\" % (fact)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter any number\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "3\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Factorial value=6\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 23
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:170"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Finding factorial of a number with recursion\n",
- "def rec(x):\n",
- " if x==1:\n",
- " return 1\n",
- " else:\n",
- " f=x*rec(x-1) \n",
- " return f #will call back the rec() function\n",
- "print \"Enter any number\"\n",
- "a=eval(raw_input())\n",
- "fact=rec(a)\n",
- "print \"Factorial value=%d\\n\" % (fact)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter any number\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "5\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Factorial value=120\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 24
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:173"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Sum of two variables using function\n",
- "def add(i,j):\n",
- " sum=i+j\n",
- " return sum\n",
- "a=5\n",
- "b=2\n",
- "c=add(a,b) #Transfers control to add()\n",
- "print \"sum=%d\\n\" % (c)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "sum=7\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 25
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:175-176"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "def factorial(num):\n",
- " f=1\n",
- " for i in range(1,num+1,1):\n",
- " f=f*i;\n",
- " return f\n",
- "f=factorial(5)\n",
- "print \"%d\\n\" % f"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "120\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 3
- }
- ],
- "metadata": {}
- }
- ]
+{ + "metadata": { + "name": "", + "signature": "sha256:2cf0a369761f6b9850c2adad43b1edd33ca8cbea4df08e2e06de2898a570851d" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "CHAPTER 5: FUNCTIONS AND POINTERS" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:144" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def message(): #function definition\n", + " print \"Smile,and the world smiles with you...\\n\"\n", + "message() #function call\n", + "print \"Cry, and you stop the monotony!\\n\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Smile,and the world smiles with you...\n", + "\n", + "Cry, and you stop the monotony!\n", + "\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:146" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def italy():\n", + " print \"I am in italy\\n\"\n", + "def brazil():\n", + " print \"I am in brazil\\n\"\n", + "def argentina():\n", + " print \"I am in argentina\\n\"\n", + "print \"I am in main\\n\"\n", + "italy(); #italy() will be called\n", + "brazil(); #brazil() will be called\n", + "argentina(); #argentina() will be called" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "I am in main\n", + "\n", + "I am in italy\n", + "\n", + "I am in brazil\n", + "\n", + "I am in argentina\n", + "\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:147" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def italy():\n", + " print \"I am in italy\\n\"\n", + " brazil(); #it will call brazil()\n", + " print \"I am back in italy\\n\"\n", + "def brazil():\n", + " print \"I am in brazil\\n\"\n", + " argentina(); #it will call argentina()\n", + "def argentina():\n", + " print \"I am in argentina\\n\"\n", + "print \"I am in main\\n\"\n", + "italy(); #italy() is called\n", + "print \"I am finally back in main\\n\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "I am in main\n", + "\n", + "I am in italy\n", + "\n", + "I am in brazil\n", + "\n", + "I am in argentina\n", + "\n", + "I am back in italy\n", + "\n", + "I am finally back in main\n", + "\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:148" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def message():\n", + " print \"Can't imagine life without C\\n\"\n", + " main() #it will call back main()\n", + "def main():\n", + " message()\n", + "main()" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:149" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def message():\n", + " print \"Jewel Thief!!\\n\"\n", + "message() #1st call\n", + "message() #2nd call" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Jewel Thief!!\n", + "\n", + "Jewel Thief!!\n", + "\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:149" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def message2():\n", + " print \"But the butter was bitter\\n\"\n", + "def message1():\n", + " print \"Mary bought some butter\\n\"\n", + "message1()\n", + "message2()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Mary bought some butter\n", + "\n", + "But the butter was bitter\n", + "\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:151-152" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def calsum(x,y,z): #parameterized function\n", + " d=x+y+z\n", + " return d\n", + "print \"Enter any three numbers\"\n", + "a=eval(raw_input())\n", + "b=eval(raw_input())\n", + "c=eval(raw_input())\n", + "sum=calsum(a,b,c) #passing values as arguments\n", + "print \"Sum=%d\\n\" % (sum)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter any three numbers\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "10\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "20\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "30\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sum=60\n", + "\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:153" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def fun():\n", + " print \"Enter any number\"\n", + " n=eval(raw_input())\n", + " if (n>=10 and n<=90):\n", + " return n\n", + " else:\n", + " return n+32\n", + "fun()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter any number\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "100\n" + ] + }, + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 7, + "text": [ + "132" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:154" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def fun(b):\n", + " b=60\n", + " print \"%d\\n\" % (b) #prints 60\n", + "a=30\n", + "fun(a) #prints 30\n", + "print \"%d\\n\" % (a)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "60\n", + "\n", + "30\n", + "\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:155" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def display(j):\n", + " k=35\n", + " print \"%d\\n\" % (j) #we can't print i directly here because scope of variable is local by default\n", + " print \"%d\\n\" % (k)\n", + "i=20\n", + "display(i)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "20\n", + "\n", + "35\n", + "\n" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:158-159" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def square(x):\n", + " y=x*x\n", + " return y\n", + "print \"Enter any number\"\n", + "a=eval(raw_input())\n", + "b=square(a)\n", + "print \"Square of %f is %f\\n\" % (a,b)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter any number\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2.5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Square of 2.500000 is 6.250000\n", + "\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:161" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "i=3\n", + "print \"Address of i=%u\\n\" % (id(i)) #id() will return the loaction of a variable\n", + "print \"Value of i=%d\\n\" % (i)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Address of i=20688560\n", + "\n", + "Value of i=3\n", + "\n" + ] + } + ], + "prompt_number": 12 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:161" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "i=3\n", + "print \"Address of i=%u\\n\" % (id(i))\n", + "print \"Value of i=%d\\n\" % (i)\n", + "print \"Value of i=%d\\n\" % (i)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Address of i=20688560\n", + "\n", + "Value of i=3\n", + "\n", + "Value of i=3\n", + "\n" + ] + } + ], + "prompt_number": 13 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:162" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "i=3\n", + "j=id(i)\n", + "print \"Address of i=%u\\n\" % (id(i)) #print address of i\n", + "print \"Address of i=%u\\n\" % (j) #print address of i\n", + "print \"Address of j=%u\\n\" % (id(j)) #print address of j\n", + "print \"Value of j=%u\\n\" % (j) #print value of j\n", + "print \"Value of i=%d\\n\" % (i)\n", + "print \"Value of i=%d\\n\" % (i)\n", + "print \"Value of i=%d\\n\" % (i)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Address of i=20688560\n", + "\n", + "Address of i=20688560\n", + "\n", + "Address of j=88986780\n", + "\n", + "Value of j=20688560\n", + "\n", + "Value of i=3\n", + "\n", + "Value of i=3\n", + "\n", + "Value of i=3\n", + "\n" + ] + } + ], + "prompt_number": 14 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:164" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "i=3\n", + "j=id(i)\n", + "k=id(j)\n", + "print \"Address of i=%u\\n\" % (id(i)) #print address of i\n", + "print \"Address of i=%u\\n\" % (j) #print address of i\n", + "print \"Address of i=%u\\n\" % (j) #print address of i\n", + "print \"Address of j=%u\\n\" % (id(j)) #print address of i\n", + "print \"Address of j=%u\\n\" % (k) #print address of j\n", + "print \"Address of k=%u\\n\" % (id(k)) #print address of k\n", + "print \"Value of j=%u\\n\" % (j) #print value of j\n", + "print \"Value of k=%u\\n\" % (k) #print value of k\n", + "print \"Value of i=%u\\n\" % (i) #print value of i\n", + "print \"Value of i=%u\\n\" % (i)\n", + "print \"Value of i=%u\\n\" % (i)\n", + "print \"Value of i=%u\\n\" % (i)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Address of i=20688560\n", + "\n", + "Address of i=20688560\n", + "\n", + "Address of i=20688560\n", + "\n", + "Address of j=88986672\n", + "\n", + "Address of j=88986672\n", + "\n", + "Address of k=88986708\n", + "\n", + "Value of j=20688560\n", + "\n", + "Value of k=88986672\n", + "\n", + "Value of i=3\n", + "\n", + "Value of i=3\n", + "\n", + "Value of i=3\n", + "\n", + "Value of i=3\n", + "\n" + ] + } + ], + "prompt_number": 15 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:166" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def swapv(x,y):\n", + " x,y=y,x\n", + " print \"x=%d y=%d\\n\" % (x,y)\n", + "a=10\n", + "b=20\n", + "swapv(a,b)\n", + "print \"a=%d b=%d\\n\" % (a,b)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "x=20 y=10\n", + "\n", + "a=10 b=20\n", + "\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE-167" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def swapr():\n", + " global a,b #global declaration\n", + " a,b=b,a\n", + "a=10\n", + "b=20\n", + "swapr()\n", + "print \"a=%d b=%d\\n\" % (a,b)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "a=20 b=10\n", + "\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:167-168" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def areaperi(r,a,p):\n", + " a=3.14*r*r #formula of area\n", + " p=2*3.14*r #formula of perimeter\n", + " print \"Area=%f\\n\" % (a)\n", + " print \"Perimeter=%f\\n\" % (p)\n", + "area=0\n", + "perimeter=0\n", + "print \"Enter radius of a circle\"\n", + "radius=eval(raw_input())\n", + "areaperi(radius,area,perimeter)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter radius of a circle\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Area=78.500000\n", + "\n", + "Perimeter=31.400000\n", + "\n" + ] + } + ], + "prompt_number": 22 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:169" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def factorial(x):\n", + " f=1\n", + " for i in range(x,1,-1):\n", + " f=f*i\n", + " return f\n", + "print \"Enter any number\"\n", + "a=eval(raw_input())\n", + "fact=factorial(a)\n", + "print \"Factorial value=%d\\n\" % (fact)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter any number\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Factorial value=6\n", + "\n" + ] + } + ], + "prompt_number": 23 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:170" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def rec(x):\n", + " if x==1:\n", + " return 1\n", + " else:\n", + " f=x*rec(x-1) \n", + " return f #will call back the rec() function\n", + "print \"Enter any number\"\n", + "a=eval(raw_input())\n", + "fact=rec(a)\n", + "print \"Factorial value=%d\\n\" % (fact)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter any number\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Factorial value=120\n", + "\n" + ] + } + ], + "prompt_number": 24 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:173" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def add(i,j):\n", + " sum=i+j\n", + " return sum\n", + "a=5\n", + "b=2\n", + "c=add(a,b) #Transfers control to add()\n", + "print \"sum=%d\\n\" % (c)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "sum=7\n", + "\n" + ] + } + ], + "prompt_number": 25 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:175-176" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def factorial(num):\n", + " f=1\n", + " for i in range(1,num+1,1):\n", + " f=f*i;\n", + " return f\n", + "f=factorial(5)\n", + "print \"%d\\n\" % f" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "120\n", + "\n" + ] + } + ], + "prompt_number": 3 + } + ], + "metadata": {} + } + ] }
\ No newline at end of file diff --git a/ANSI_C_Programming/chapter6.ipynb b/ANSI_C_Programming/chapter6.ipynb index 1cdd6dae..08372811 100644 --- a/ANSI_C_Programming/chapter6.ipynb +++ b/ANSI_C_Programming/chapter6.ipynb @@ -1,1731 +1,1731 @@ -{
- "metadata": {
- "name": "chapter6.ipynb"
- },
- "nbformat": 3,
- "nbformat_minor": 0,
- "worksheets": [
- {
- "cells": [
- {
- "cell_type": "heading",
- "level": 1,
- "metadata": {},
- "source": [
- "CHAPTER 6: DATA TYPES REVISITED"
- ]
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:199"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#printing character corresponding to integer value\n",
- "ch=291\n",
- "if ch>255:\n",
- " ch=ch-256\n",
- "print \"\\n%d %c\\n\" % (ch,ch)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "\n",
- "35 #\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 1
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:199"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#print ascii values and their corresponding characters\n",
- "for ch in range(0,256,1):\n",
- " print \"%d %c\\n\" % (ch,ch)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "0 \u0000\n",
- "\n",
- "1 \u0001\n",
- "\n",
- "2 \u0002\n",
- "\n",
- "3 \u0003\n",
- "\n",
- "4 \u0004\n",
- "\n",
- "5 \u0005\n",
- "\n",
- "6 \u0006\n",
- "\n",
- "7 \u0007\n",
- "\n",
- "8 \b\n",
- "\n",
- "9 \t\n",
- "\n",
- "10 \n",
- "\n",
- "\n",
- "11 \u000b",
- "\n",
- "\n",
- "12 \f",
- "\n",
- "\n",
- "13 \r\n",
- "\n",
- "14 \u000e\n",
- "\n",
- "15 \u000f\n",
- "\n",
- "16 \u0010\n",
- "\n",
- "17 \u0011\n",
- "\n",
- "18 \u0012\n",
- "\n",
- "19 \u0013\n",
- "\n",
- "20 \u0014\n",
- "\n",
- "21 \u0015\n",
- "\n",
- "22 \u0016\n",
- "\n",
- "23 \u0017\n",
- "\n",
- "24 \u0018\n",
- "\n",
- "25 \u0019\n",
- "\n",
- "26 \u001a\n",
- "\n",
- "27 \u001b\n",
- "\n",
- "28 \u001c",
- "\n",
- "\n",
- "29 \u001d",
- "\n",
- "\n",
- "30 \u001e",
- "\n",
- "\n",
- "31 \u001f\n",
- "\n",
- "32 \n",
- "\n",
- "33 !\n",
- "\n",
- "34 \"\n",
- "\n",
- "35 #\n",
- "\n",
- "36 $\n",
- "\n",
- "37 %\n",
- "\n",
- "38 &\n",
- "\n",
- "39 '\n",
- "\n",
- "40 (\n",
- "\n",
- "41 )\n",
- "\n",
- "42 *\n",
- "\n",
- "43 +\n",
- "\n",
- "44 ,\n",
- "\n",
- "45 -\n",
- "\n",
- "46 .\n",
- "\n",
- "47 /\n",
- "\n",
- "48 0\n",
- "\n",
- "49 1\n",
- "\n",
- "50 2\n",
- "\n",
- "51 3\n",
- "\n",
- "52 4\n",
- "\n",
- "53 5\n",
- "\n",
- "54 6\n",
- "\n",
- "55 7\n",
- "\n",
- "56 8\n",
- "\n",
- "57 9\n",
- "\n",
- "58 :\n",
- "\n",
- "59 ;\n",
- "\n",
- "60 <\n",
- "\n",
- "61 =\n",
- "\n",
- "62 >\n",
- "\n",
- "63 ?\n",
- "\n",
- "64 @\n",
- "\n",
- "65 A\n",
- "\n",
- "66 B\n",
- "\n",
- "67 C\n",
- "\n",
- "68 D\n",
- "\n",
- "69 E\n",
- "\n",
- "70 F\n",
- "\n",
- "71 G\n",
- "\n",
- "72 H\n",
- "\n",
- "73 I\n",
- "\n",
- "74 J\n",
- "\n",
- "75 K\n",
- "\n",
- "76 L\n",
- "\n",
- "77 M\n",
- "\n",
- "78 N\n",
- "\n",
- "79 O\n",
- "\n",
- "80 P\n",
- "\n",
- "81 Q\n",
- "\n",
- "82 R\n",
- "\n",
- "83 S\n",
- "\n",
- "84 T\n",
- "\n",
- "85 U\n",
- "\n",
- "86 V\n",
- "\n",
- "87 W\n",
- "\n",
- "88 X\n",
- "\n",
- "89 Y\n",
- "\n",
- "90 Z\n",
- "\n",
- "91 [\n",
- "\n",
- "92 \\\n",
- "\n",
- "93 ]\n",
- "\n",
- "94 ^\n",
- "\n",
- "95 _\n",
- "\n",
- "96 `\n",
- "\n",
- "97 a\n",
- "\n",
- "98 b\n",
- "\n",
- "99 c\n",
- "\n",
- "100 d\n",
- "\n",
- "101 e\n",
- "\n",
- "102 f\n",
- "\n",
- "103 g\n",
- "\n",
- "104 h\n",
- "\n",
- "105 i\n",
- "\n",
- "106 j\n",
- "\n",
- "107 k\n",
- "\n",
- "108 l\n",
- "\n",
- "109 m\n",
- "\n",
- "110 n\n",
- "\n",
- "111 o\n",
- "\n",
- "112 p\n",
- "\n",
- "113 q\n",
- "\n",
- "114 r\n",
- "\n",
- "115 s\n",
- "\n",
- "116 t\n",
- "\n",
- "117 u\n",
- "\n",
- "118 v\n",
- "\n",
- "119 w\n",
- "\n",
- "120 x\n",
- "\n",
- "121 y\n",
- "\n",
- "122 z\n",
- "\n",
- "123 {\n",
- "\n",
- "124 |\n",
- "\n",
- "125 }\n",
- "\n",
- "126 ~\n",
- "\n",
- "127 \u007f\n",
- "\n",
- "128 \ufffd\n",
- "\n",
- "129 \ufffd\n",
- "\n",
- "130 \ufffd\n",
- "\n",
- "131 \ufffd\n",
- "\n",
- "132 \ufffd\n",
- "\n",
- "133 \ufffd\n",
- "\n",
- "134 \ufffd\n",
- "\n",
- "135 \ufffd\n",
- "\n",
- "136 \ufffd\n",
- "\n",
- "137 \ufffd\n",
- "\n",
- "138 \ufffd\n",
- "\n",
- "139 \ufffd\n",
- "\n",
- "140 \ufffd\n",
- "\n",
- "141 \ufffd\n",
- "\n",
- "142 \ufffd\n",
- "\n",
- "143 \ufffd\n",
- "\n",
- "144 \ufffd\n",
- "\n",
- "145 \ufffd\n",
- "\n",
- "146 \ufffd\n",
- "\n",
- "147 \ufffd\n",
- "\n",
- "148 \ufffd\n",
- "\n",
- "149 \ufffd\n",
- "\n",
- "150 \ufffd\n",
- "\n",
- "151 \ufffd\n",
- "\n",
- "152 \ufffd\n",
- "\n",
- "153 \ufffd\n",
- "\n",
- "154 \ufffd\n",
- "\n",
- "155 \ufffd\n",
- "\n",
- "156 \ufffd\n",
- "\n",
- "157 \ufffd\n",
- "\n",
- "158 \ufffd\n",
- "\n",
- "159 \ufffd\n",
- "\n",
- "160 \ufffd\n",
- "\n",
- "161 \ufffd\n",
- "\n",
- "162 \ufffd\n",
- "\n",
- "163 \ufffd\n",
- "\n",
- "164 \ufffd\n",
- "\n",
- "165 \ufffd\n",
- "\n",
- "166 \ufffd\n",
- "\n",
- "167 \ufffd\n",
- "\n",
- "168 \ufffd\n",
- "\n",
- "169 \ufffd\n",
- "\n",
- "170 \ufffd\n",
- "\n",
- "171 \ufffd\n",
- "\n",
- "172 \ufffd\n",
- "\n",
- "173 \ufffd\n",
- "\n",
- "174 \ufffd\n",
- "\n",
- "175 \ufffd\n",
- "\n",
- "176 \ufffd\n",
- "\n",
- "177 \ufffd\n",
- "\n",
- "178 \ufffd\n",
- "\n",
- "179 \ufffd\n",
- "\n",
- "180 \ufffd\n",
- "\n",
- "181 \ufffd\n",
- "\n",
- "182 \ufffd\n",
- "\n",
- "183 \ufffd\n",
- "\n",
- "184 \ufffd\n",
- "\n",
- "185 \ufffd\n",
- "\n",
- "186 \ufffd\n",
- "\n",
- "187 \ufffd\n",
- "\n",
- "188 \ufffd\n",
- "\n",
- "189 \ufffd\n",
- "\n",
- "190 \ufffd\n",
- "\n",
- "191 \ufffd\n",
- "\n",
- "192 \ufffd\n",
- "\n",
- "193 \ufffd\n",
- "\n",
- "194 \ufffd\n",
- "\n",
- "195 \ufffd\n",
- "\n",
- "196 \ufffd\n",
- "\n",
- "197 \ufffd\n",
- "\n",
- "198 \ufffd\n",
- "\n",
- "199 \ufffd\n",
- "\n",
- "200 \ufffd\n",
- "\n",
- "201 \ufffd\n",
- "\n",
- "202 \ufffd\n",
- "\n",
- "203 \ufffd\n",
- "\n",
- "204 \ufffd\n",
- "\n",
- "205 \ufffd\n",
- "\n",
- "206 \ufffd\n",
- "\n",
- "207 \ufffd\n",
- "\n",
- "208 \ufffd\n",
- "\n",
- "209 \ufffd\n",
- "\n",
- "210 \ufffd\n",
- "\n",
- "211 \ufffd\n",
- "\n",
- "212 \ufffd\n",
- "\n",
- "213 \ufffd\n",
- "\n",
- "214 \ufffd\n",
- "\n",
- "215 \ufffd\n",
- "\n",
- "216 \ufffd\n",
- "\n",
- "217 \ufffd\n",
- "\n",
- "218 \ufffd\n",
- "\n",
- "219 \ufffd\n",
- "\n",
- "220 \ufffd\n",
- "\n",
- "221 \ufffd\n",
- "\n",
- "222 \ufffd\n",
- "\n",
- "223 \ufffd\n",
- "\n",
- "224 \ufffd\n",
- "\n",
- "225 \ufffd\n",
- "\n",
- "226 \ufffd\n",
- "\n",
- "227 \ufffd\n",
- "\n",
- "228 \ufffd\n",
- "\n",
- "229 \ufffd\n",
- "\n",
- "230 \ufffd\n",
- "\n",
- "231 \ufffd\n",
- "\n",
- "232 \ufffd\n",
- "\n",
- "233 \ufffd\n",
- "\n",
- "234 \ufffd\n",
- "\n",
- "235 \ufffd\n",
- "\n",
- "236 \ufffd\n",
- "\n",
- "237 \ufffd\n",
- "\n",
- "238 \ufffd\n",
- "\n",
- "239 \ufffd\n",
- "\n",
- "240 \ufffd\n",
- "\n",
- "241 \ufffd\n",
- "\n",
- "242 \ufffd\n",
- "\n",
- "243 \ufffd\n",
- "\n",
- "244 \ufffd\n",
- "\n",
- "245 \ufffd\n",
- "\n",
- "246 \ufffd\n",
- "\n",
- "247 \ufffd\n",
- "\n",
- "248 \ufffd\n",
- "\n",
- "249 \ufffd\n",
- "\n",
- "250 \ufffd\n",
- "\n",
- "251 \ufffd\n",
- "\n",
- "252 \ufffd\n",
- "\n",
- "253 \ufffd\n",
- "\n",
- "254 \ufffd\n",
- "\n",
- "255 \ufffd\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 2
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:200"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#print ascii values and their corresponding characters\n",
- "for ch in range(0,255,1):\n",
- " print \"%d %c\\n\" % (ch,ch)\n",
- "print \"%d %c\\n\" % (ch,ch)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "0 \u0000\n",
- "\n",
- "1 \u0001\n",
- "\n",
- "2 \u0002\n",
- "\n",
- "3 \u0003\n",
- "\n",
- "4 \u0004\n",
- "\n",
- "5 \u0005\n",
- "\n",
- "6 \u0006\n",
- "\n",
- "7 \u0007\n",
- "\n",
- "8 \b\n",
- "\n",
- "9 \t\n",
- "\n",
- "10 \n",
- "\n",
- "\n",
- "11 \u000b",
- "\n",
- "\n",
- "12 \f",
- "\n",
- "\n",
- "13 \r\n",
- "\n",
- "14 \u000e\n",
- "\n",
- "15 \u000f\n",
- "\n",
- "16 \u0010\n",
- "\n",
- "17 \u0011\n",
- "\n",
- "18 \u0012\n",
- "\n",
- "19 \u0013\n",
- "\n",
- "20 \u0014\n",
- "\n",
- "21 \u0015\n",
- "\n",
- "22 \u0016\n",
- "\n",
- "23 \u0017\n",
- "\n",
- "24 \u0018\n",
- "\n",
- "25 \u0019\n",
- "\n",
- "26 \u001a\n",
- "\n",
- "27 \u001b\n",
- "\n",
- "28 \u001c",
- "\n",
- "\n",
- "29 \u001d",
- "\n",
- "\n",
- "30 \u001e",
- "\n",
- "\n",
- "31 \u001f\n",
- "\n",
- "32 \n",
- "\n",
- "33 !\n",
- "\n",
- "34 \"\n",
- "\n",
- "35 #\n",
- "\n",
- "36 $\n",
- "\n",
- "37 %\n",
- "\n",
- "38 &\n",
- "\n",
- "39 '\n",
- "\n",
- "40 (\n",
- "\n",
- "41 )\n",
- "\n",
- "42 *\n",
- "\n",
- "43 +\n",
- "\n",
- "44 ,\n",
- "\n",
- "45 -\n",
- "\n",
- "46 .\n",
- "\n",
- "47 /\n",
- "\n",
- "48 0\n",
- "\n",
- "49 1\n",
- "\n",
- "50 2\n",
- "\n",
- "51 3\n",
- "\n",
- "52 4\n",
- "\n",
- "53 5\n",
- "\n",
- "54 6\n",
- "\n",
- "55 7\n",
- "\n",
- "56 8\n",
- "\n",
- "57 9\n",
- "\n",
- "58 :\n",
- "\n",
- "59 ;\n",
- "\n",
- "60 <\n",
- "\n",
- "61 =\n",
- "\n",
- "62 >\n",
- "\n",
- "63 ?\n",
- "\n",
- "64 @\n",
- "\n",
- "65 A\n",
- "\n",
- "66 B\n",
- "\n",
- "67 C\n",
- "\n",
- "68 D\n",
- "\n",
- "69 E\n",
- "\n",
- "70 F\n",
- "\n",
- "71 G\n",
- "\n",
- "72 H\n",
- "\n",
- "73 I\n",
- "\n",
- "74 J\n",
- "\n",
- "75 K\n",
- "\n",
- "76 L\n",
- "\n",
- "77 M\n",
- "\n",
- "78 N\n",
- "\n",
- "79 O\n",
- "\n",
- "80 P\n",
- "\n",
- "81 Q\n",
- "\n",
- "82 R\n",
- "\n",
- "83 S\n",
- "\n",
- "84 T\n",
- "\n",
- "85 U\n",
- "\n",
- "86 V\n",
- "\n",
- "87 W\n",
- "\n",
- "88 X\n",
- "\n",
- "89 Y\n",
- "\n",
- "90 Z\n",
- "\n",
- "91 [\n",
- "\n",
- "92 \\\n",
- "\n",
- "93 ]\n",
- "\n",
- "94 ^\n",
- "\n",
- "95 _\n",
- "\n",
- "96 `\n",
- "\n",
- "97 a\n",
- "\n",
- "98 b\n",
- "\n",
- "99 c\n",
- "\n",
- "100 d\n",
- "\n",
- "101 e\n",
- "\n",
- "102 f\n",
- "\n",
- "103 g\n",
- "\n",
- "104 h\n",
- "\n",
- "105 i\n",
- "\n",
- "106 j\n",
- "\n",
- "107 k\n",
- "\n",
- "108 l\n",
- "\n",
- "109 m\n",
- "\n",
- "110 n\n",
- "\n",
- "111 o\n",
- "\n",
- "112 p\n",
- "\n",
- "113 q\n",
- "\n",
- "114 r\n",
- "\n",
- "115 s\n",
- "\n",
- "116 t\n",
- "\n",
- "117 u\n",
- "\n",
- "118 v\n",
- "\n",
- "119 w\n",
- "\n",
- "120 x\n",
- "\n",
- "121 y\n",
- "\n",
- "122 z\n",
- "\n",
- "123 {\n",
- "\n",
- "124 |\n",
- "\n",
- "125 }\n",
- "\n",
- "126 ~\n",
- "\n",
- "127 \u007f\n",
- "\n",
- "128 \ufffd\n",
- "\n",
- "129 \ufffd\n",
- "\n",
- "130 \ufffd\n",
- "\n",
- "131 \ufffd\n",
- "\n",
- "132 \ufffd\n",
- "\n",
- "133 \ufffd\n",
- "\n",
- "134 \ufffd\n",
- "\n",
- "135 \ufffd\n",
- "\n",
- "136 \ufffd\n",
- "\n",
- "137 \ufffd\n",
- "\n",
- "138 \ufffd\n",
- "\n",
- "139 \ufffd\n",
- "\n",
- "140 \ufffd\n",
- "\n",
- "141 \ufffd\n",
- "\n",
- "142 \ufffd\n",
- "\n",
- "143 \ufffd\n",
- "\n",
- "144 \ufffd\n",
- "\n",
- "145 \ufffd\n",
- "\n",
- "146 \ufffd\n",
- "\n",
- "147 \ufffd\n",
- "\n",
- "148 \ufffd\n",
- "\n",
- "149 \ufffd\n",
- "\n",
- "150 \ufffd\n",
- "\n",
- "151 \ufffd\n",
- "\n",
- "152 \ufffd\n",
- "\n",
- "153 \ufffd\n",
- "\n",
- "154 \ufffd\n",
- "\n",
- "155 \ufffd\n",
- "\n",
- "156 \ufffd\n",
- "\n",
- "157 \ufffd\n",
- "\n",
- "158 \ufffd\n",
- "\n",
- "159 \ufffd\n",
- "\n",
- "160 \ufffd\n",
- "\n",
- "161 \ufffd\n",
- "\n",
- "162 \ufffd\n",
- "\n",
- "163 \ufffd\n",
- "\n",
- "164 \ufffd\n",
- "\n",
- "165 \ufffd\n",
- "\n",
- "166 \ufffd\n",
- "\n",
- "167 \ufffd\n",
- "\n",
- "168 \ufffd\n",
- "\n",
- "169 \ufffd\n",
- "\n",
- "170 \ufffd\n",
- "\n",
- "171 \ufffd\n",
- "\n",
- "172 \ufffd\n",
- "\n",
- "173 \ufffd\n",
- "\n",
- "174 \ufffd\n",
- "\n",
- "175 \ufffd\n",
- "\n",
- "176 \ufffd\n",
- "\n",
- "177 \ufffd\n",
- "\n",
- "178 \ufffd\n",
- "\n",
- "179 \ufffd\n",
- "\n",
- "180 \ufffd\n",
- "\n",
- "181 \ufffd\n",
- "\n",
- "182 \ufffd\n",
- "\n",
- "183 \ufffd\n",
- "\n",
- "184 \ufffd\n",
- "\n",
- "185 \ufffd\n",
- "\n",
- "186 \ufffd\n",
- "\n",
- "187 \ufffd\n",
- "\n",
- "188 \ufffd\n",
- "\n",
- "189 \ufffd\n",
- "\n",
- "190 \ufffd\n",
- "\n",
- "191 \ufffd\n",
- "\n",
- "192 \ufffd\n",
- "\n",
- "193 \ufffd\n",
- "\n",
- "194 \ufffd\n",
- "\n",
- "195 \ufffd\n",
- "\n",
- "196 \ufffd\n",
- "\n",
- "197 \ufffd\n",
- "\n",
- "198 \ufffd\n",
- "\n",
- "199 \ufffd\n",
- "\n",
- "200 \ufffd\n",
- "\n",
- "201 \ufffd\n",
- "\n",
- "202 \ufffd\n",
- "\n",
- "203 \ufffd\n",
- "\n",
- "204 \ufffd\n",
- "\n",
- "205 \ufffd\n",
- "\n",
- "206 \ufffd\n",
- "\n",
- "207 \ufffd\n",
- "\n",
- "208 \ufffd\n",
- "\n",
- "209 \ufffd\n",
- "\n",
- "210 \ufffd\n",
- "\n",
- "211 \ufffd\n",
- "\n",
- "212 \ufffd\n",
- "\n",
- "213 \ufffd\n",
- "\n",
- "214 \ufffd\n",
- "\n",
- "215 \ufffd\n",
- "\n",
- "216 \ufffd\n",
- "\n",
- "217 \ufffd\n",
- "\n",
- "218 \ufffd\n",
- "\n",
- "219 \ufffd\n",
- "\n",
- "220 \ufffd\n",
- "\n",
- "221 \ufffd\n",
- "\n",
- "222 \ufffd\n",
- "\n",
- "223 \ufffd\n",
- "\n",
- "224 \ufffd\n",
- "\n",
- "225 \ufffd\n",
- "\n",
- "226 \ufffd\n",
- "\n",
- "227 \ufffd\n",
- "\n",
- "228 \ufffd\n",
- "\n",
- "229 \ufffd\n",
- "\n",
- "230 \ufffd\n",
- "\n",
- "231 \ufffd\n",
- "\n",
- "232 \ufffd\n",
- "\n",
- "233 \ufffd\n",
- "\n",
- "234 \ufffd\n",
- "\n",
- "235 \ufffd\n",
- "\n",
- "236 \ufffd\n",
- "\n",
- "237 \ufffd\n",
- "\n",
- "238 \ufffd\n",
- "\n",
- "239 \ufffd\n",
- "\n",
- "240 \ufffd\n",
- "\n",
- "241 \ufffd\n",
- "\n",
- "242 \ufffd\n",
- "\n",
- "243 \ufffd\n",
- "\n",
- "244 \ufffd\n",
- "\n",
- "245 \ufffd\n",
- "\n",
- "246 \ufffd\n",
- "\n",
- "247 \ufffd\n",
- "\n",
- "248 \ufffd\n",
- "\n",
- "249 \ufffd\n",
- "\n",
- "250 \ufffd\n",
- "\n",
- "251 \ufffd\n",
- "\n",
- "252 \ufffd\n",
- "\n",
- "253 \ufffd\n",
- "\n",
- "254 \ufffd\n",
- "\n",
- "254 \ufffd\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 3
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:200-201"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Use of format specifiers to output variables\n",
- "#char\n",
- "c=raw_input()\n",
- "d=raw_input()\n",
- "print \"%c %c\\n\" % (c,d)\n",
- "#int\n",
- "i=eval(raw_input())\n",
- "j=eval(raw_input())\n",
- "print \"%d %u\\n\" % (i,j)\n",
- "#short int\n",
- "k=eval(raw_input())\n",
- "l=eval(raw_input())\n",
- "print \"%d %u\\n\" % (k,l)\n",
- "#long int\n",
- "m=eval(raw_input())\n",
- "n=eval(raw_input())\n",
- "print \"%ld %lu\\n\" % (m,n)\n",
- "#float,double,long double\n",
- "x=eval(raw_input())\n",
- "y=eval(raw_input())\n",
- "z=eval(raw_input())\n",
- "print \"%f %lf %Lf\\n\" % (x,y,z)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "a\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "b\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "a b\n",
- "\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "10000\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "7389\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "10000 7389\n",
- "\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "34\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "585\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "34 585\n",
- "\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "34676\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "500000\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "34676 500000\n",
- "\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "445.55\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "57846.44\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "55666885.6655\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "445.550000 57846.440000 55666885.665500\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 3
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:205"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#program explaining concept of scope of variables\n",
- "#no concept of auto storage class\n",
- "def main():\n",
- " i=1\n",
- " def main2():\n",
- " def main3():\n",
- " print \"%d\" % (i) #prints 1\n",
- " main3()\n",
- " print \"%d\" % (i) #prints 1\n",
- " main2()\n",
- " print \"%d\\n\" % (i) #prints 1\n",
- "main()"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "1\n",
- "1\n",
- "1\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 4
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:205"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#program explaing concept of local variables\n",
- "def main():\n",
- " i=1\n",
- " def main2():\n",
- " i=2\n",
- " def main3():\n",
- " i=3\n",
- " print \"%d\" % (i) #prints 3\n",
- " main3()\n",
- " print \"%d\" % (i) #prints 2\n",
- " main2()\n",
- " print \"%d\\n\" % (i) #prints 1\n",
- "main()"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "3\n",
- "2\n",
- "1\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 5
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:206"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#prints numbers from 1 to 10\n",
- "for i in range(1,11,1):\n",
- " print \"%d\\n\" % (i)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "1\n",
- "\n",
- "2\n",
- "\n",
- "3\n",
- "\n",
- "4\n",
- "\n",
- "5\n",
- "\n",
- "6\n",
- "\n",
- "7\n",
- "\n",
- "8\n",
- "\n",
- "9\n",
- "\n",
- "10\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 6
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:208"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#program to explain use of variable\n",
- "def increment():\n",
- " i=1\n",
- " print \"%d\\n\" % (i) #prints 1 every time\n",
- " i=i+1\n",
- "increment()\n",
- "increment()\n",
- "increment()"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "1\n",
- "\n",
- "1\n",
- "\n",
- "1\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 7
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:208"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#program using static variables\n",
- "def static_var(varname, value): #function to make it static\n",
- " def decorate(func):\n",
- " setattr(func, varname, value)\n",
- " return func\n",
- " return decorate\n",
- "@static_var(\"i\", 1) #i is static variable\n",
- "def increment():\n",
- " print \"%d\\n\" % increment.i\n",
- " increment.i += 1\n",
- "increment()\n",
- "increment()\n",
- "increment()"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "1\n",
- "\n",
- "2\n",
- "\n",
- "3\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 8
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:209"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Returning address from function\n",
- "def fun():\n",
- " k=35\n",
- " return id(k) #returns address of k\n",
- "j=fun() #stores address of k\n",
- "print \"%d\\n\" % (j) #prints address of k"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "19639600\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 9
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:210"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#program using global variables\n",
- "def increment():\n",
- " global i #gloabal declaration\n",
- " i=i+1\n",
- " print \"on incrementing i=%d\\n\" % (i)\n",
- "def decrement():\n",
- " global i #global declaration\n",
- " i=i-1\n",
- " print \"on decrementing i=%d\\n\" % (i)\n",
- "i=0\n",
- "print \"\\ni=%d\" % (i)\n",
- "increment()\n",
- "increment()\n",
- "decrement()\n",
- "decrement()"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "\n",
- "i=0\n",
- "on incrementing i=1\n",
- "\n",
- "on incrementing i=2\n",
- "\n",
- "on decrementing i=1\n",
- "\n",
- "on decrementing i=0\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 10
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:211"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#printing of global variables\n",
- "x=21\n",
- "def main():\n",
- " global x,y\n",
- " print \"%d %d\\n\" % (x,y)\n",
- "y=31\n",
- "main()"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "21 31\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 11
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:211"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Global and local variable\n",
- "def display():\n",
- " global x #global variable\n",
- " print \"%d\\n\" % (x) #prints 10\n",
- "x=10\n",
- "def main():\n",
- " x=20 #local variable for main\n",
- " print \"%d\\n\" % (x) #prints 20\n",
- " display()\n",
- "main()"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "20\n",
- "\n",
- "10\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 12
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:212-213"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#GLOBAL VARIABLES CONTINUES\n",
- "def fun1():\n",
- " global i\n",
- " i+=1\n",
- " print \"%d\\n\" % (i)\n",
- "def fun2():\n",
- " global i\n",
- " i-=1\n",
- " print \"%d\\n\" % (i)\n",
- "i=35\n",
- "print \"%d\\n\" % (i)\n",
- "fun1()\n",
- "fun2()"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "35\n",
- "\n",
- "36\n",
- "\n",
- "35\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 13
- }
- ],
- "metadata": {}
- }
- ]
+{ + "metadata": { + "name": "", + "signature": "sha256:16010659f0ef4e7689cf0cba4bf13a7155f0a2c561ad3c2947d5e5c9a18c6603" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "CHAPTER 6: DATA TYPES REVISITED" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:199" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "ch=291\n", + "if ch>255:\n", + " ch=ch-256\n", + "print \"\\n%d %c\\n\" % (ch,ch)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "35 #\n", + "\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:199" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "for ch in range(0,256,1):\n", + " print \"%d %c\\n\" % (ch,ch)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "0 \u0000\n", + "\n", + "1 \u0001\n", + "\n", + "2 \u0002\n", + "\n", + "3 \u0003\n", + "\n", + "4 \u0004\n", + "\n", + "5 \u0005\n", + "\n", + "6 \u0006\n", + "\n", + "7 \u0007\n", + "\n", + "8 \b\n", + "\n", + "9 \t\n", + "\n", + "10 \n", + "\n", + "\n", + "11 \u000b", + "\n", + "\n", + "12 \f", + "\n", + "\n", + "13 \r\n", + "\n", + "14 \u000e\n", + "\n", + "15 \u000f\n", + "\n", + "16 \u0010\n", + "\n", + "17 \u0011\n", + "\n", + "18 \u0012\n", + "\n", + "19 \u0013\n", + "\n", + "20 \u0014\n", + "\n", + "21 \u0015\n", + "\n", + "22 \u0016\n", + "\n", + "23 \u0017\n", + "\n", + "24 \u0018\n", + "\n", + "25 \u0019\n", + "\n", + "26 \u001a\n", + "\n", + "27 \u001b\n", + "\n", + "28 \u001c", + "\n", + "\n", + "29 \u001d", + "\n", + "\n", + "30 \u001e", + "\n", + "\n", + "31 \u001f\n", + "\n", + "32 \n", + "\n", + "33 !\n", + "\n", + "34 \"\n", + "\n", + "35 #\n", + "\n", + "36 $\n", + "\n", + "37 %\n", + "\n", + "38 &\n", + "\n", + "39 '\n", + "\n", + "40 (\n", + "\n", + "41 )\n", + "\n", + "42 *\n", + "\n", + "43 +\n", + "\n", + "44 ,\n", + "\n", + "45 -\n", + "\n", + "46 .\n", + "\n", + "47 /\n", + "\n", + "48 0\n", + "\n", + "49 1\n", + "\n", + "50 2\n", + "\n", + "51 3\n", + "\n", + "52 4\n", + "\n", + "53 5\n", + "\n", + "54 6\n", + "\n", + "55 7\n", + "\n", + "56 8\n", + "\n", + "57 9\n", + "\n", + "58 :\n", + "\n", + "59 ;\n", + "\n", + "60 <\n", + "\n", + "61 =\n", + "\n", + "62 >\n", + "\n", + "63 ?\n", + "\n", + "64 @\n", + "\n", + "65 A\n", + "\n", + "66 B\n", + "\n", + "67 C\n", + "\n", + "68 D\n", + "\n", + "69 E\n", + "\n", + "70 F\n", + "\n", + "71 G\n", + "\n", + "72 H\n", + "\n", + "73 I\n", + "\n", + "74 J\n", + "\n", + "75 K\n", + "\n", + "76 L\n", + "\n", + "77 M\n", + "\n", + "78 N\n", + "\n", + "79 O\n", + "\n", + "80 P\n", + "\n", + "81 Q\n", + "\n", + "82 R\n", + "\n", + "83 S\n", + "\n", + "84 T\n", + "\n", + "85 U\n", + "\n", + "86 V\n", + "\n", + "87 W\n", + "\n", + "88 X\n", + "\n", + "89 Y\n", + "\n", + "90 Z\n", + "\n", + "91 [\n", + "\n", + "92 \\\n", + "\n", + "93 ]\n", + "\n", + "94 ^\n", + "\n", + "95 _\n", + "\n", + "96 `\n", + "\n", + "97 a\n", + "\n", + "98 b\n", + "\n", + "99 c\n", + "\n", + "100 d\n", + "\n", + "101 e\n", + "\n", + "102 f\n", + "\n", + "103 g\n", + "\n", + "104 h\n", + "\n", + "105 i\n", + "\n", + "106 j\n", + "\n", + "107 k\n", + "\n", + "108 l\n", + "\n", + "109 m\n", + "\n", + "110 n\n", + "\n", + "111 o\n", + "\n", + "112 p\n", + "\n", + "113 q\n", + "\n", + "114 r\n", + "\n", + "115 s\n", + "\n", + "116 t\n", + "\n", + "117 u\n", + "\n", + "118 v\n", + "\n", + "119 w\n", + "\n", + "120 x\n", + "\n", + "121 y\n", + "\n", + "122 z\n", + "\n", + "123 {\n", + "\n", + "124 |\n", + "\n", + "125 }\n", + "\n", + "126 ~\n", + "\n", + "127 \u007f\n", + "\n", + "128 \ufffd\n", + "\n", + "129 \ufffd\n", + "\n", + "130 \ufffd\n", + "\n", + "131 \ufffd\n", + "\n", + "132 \ufffd\n", + "\n", + "133 \ufffd\n", + "\n", + "134 \ufffd\n", + "\n", + "135 \ufffd\n", + "\n", + "136 \ufffd\n", + "\n", + "137 \ufffd\n", + "\n", + "138 \ufffd\n", + "\n", + "139 \ufffd\n", + "\n", + "140 \ufffd\n", + "\n", + "141 \ufffd\n", + "\n", + "142 \ufffd\n", + "\n", + "143 \ufffd\n", + "\n", + "144 \ufffd\n", + "\n", + "145 \ufffd\n", + "\n", + "146 \ufffd\n", + "\n", + "147 \ufffd\n", + "\n", + "148 \ufffd\n", + "\n", + "149 \ufffd\n", + "\n", + "150 \ufffd\n", + "\n", + "151 \ufffd\n", + "\n", + "152 \ufffd\n", + "\n", + "153 \ufffd\n", + "\n", + "154 \ufffd\n", + "\n", + "155 \ufffd\n", + "\n", + "156 \ufffd\n", + "\n", + "157 \ufffd\n", + "\n", + "158 \ufffd\n", + "\n", + "159 \ufffd\n", + "\n", + "160 \ufffd\n", + "\n", + "161 \ufffd\n", + "\n", + "162 \ufffd\n", + "\n", + "163 \ufffd\n", + "\n", + "164 \ufffd\n", + "\n", + "165 \ufffd\n", + "\n", + "166 \ufffd\n", + "\n", + "167 \ufffd\n", + "\n", + "168 \ufffd\n", + "\n", + "169 \ufffd\n", + "\n", + "170 \ufffd\n", + "\n", + "171 \ufffd\n", + "\n", + "172 \ufffd\n", + "\n", + "173 \ufffd\n", + "\n", + "174 \ufffd\n", + "\n", + "175 \ufffd\n", + "\n", + "176 \ufffd\n", + "\n", + "177 \ufffd\n", + "\n", + "178 \ufffd\n", + "\n", + "179 \ufffd\n", + "\n", + "180 \ufffd\n", + "\n", + "181 \ufffd\n", + "\n", + "182 \ufffd\n", + "\n", + "183 \ufffd\n", + "\n", + "184 \ufffd\n", + "\n", + "185 \ufffd\n", + "\n", + "186 \ufffd\n", + "\n", + "187 \ufffd\n", + "\n", + "188 \ufffd\n", + "\n", + "189 \ufffd\n", + "\n", + "190 \ufffd\n", + "\n", + "191 \ufffd\n", + "\n", + "192 \ufffd\n", + "\n", + "193 \ufffd\n", + "\n", + "194 \ufffd\n", + "\n", + "195 \ufffd\n", + "\n", + "196 \ufffd\n", + "\n", + "197 \ufffd\n", + "\n", + "198 \ufffd\n", + "\n", + "199 \ufffd\n", + "\n", + "200 \ufffd\n", + "\n", + "201 \ufffd\n", + "\n", + "202 \ufffd\n", + "\n", + "203 \ufffd\n", + "\n", + "204 \ufffd\n", + "\n", + "205 \ufffd\n", + "\n", + "206 \ufffd\n", + "\n", + "207 \ufffd\n", + "\n", + "208 \ufffd\n", + "\n", + "209 \ufffd\n", + "\n", + "210 \ufffd\n", + "\n", + "211 \ufffd\n", + "\n", + "212 \ufffd\n", + "\n", + "213 \ufffd\n", + "\n", + "214 \ufffd\n", + "\n", + "215 \ufffd\n", + "\n", + "216 \ufffd\n", + "\n", + "217 \ufffd\n", + "\n", + "218 \ufffd\n", + "\n", + "219 \ufffd\n", + "\n", + "220 \ufffd\n", + "\n", + "221 \ufffd\n", + "\n", + "222 \ufffd\n", + "\n", + "223 \ufffd\n", + "\n", + "224 \ufffd\n", + "\n", + "225 \ufffd\n", + "\n", + "226 \ufffd\n", + "\n", + "227 \ufffd\n", + "\n", + "228 \ufffd\n", + "\n", + "229 \ufffd\n", + "\n", + "230 \ufffd\n", + "\n", + "231 \ufffd\n", + "\n", + "232 \ufffd\n", + "\n", + "233 \ufffd\n", + "\n", + "234 \ufffd\n", + "\n", + "235 \ufffd\n", + "\n", + "236 \ufffd\n", + "\n", + "237 \ufffd\n", + "\n", + "238 \ufffd\n", + "\n", + "239 \ufffd\n", + "\n", + "240 \ufffd\n", + "\n", + "241 \ufffd\n", + "\n", + "242 \ufffd\n", + "\n", + "243 \ufffd\n", + "\n", + "244 \ufffd\n", + "\n", + "245 \ufffd\n", + "\n", + "246 \ufffd\n", + "\n", + "247 \ufffd\n", + "\n", + "248 \ufffd\n", + "\n", + "249 \ufffd\n", + "\n", + "250 \ufffd\n", + "\n", + "251 \ufffd\n", + "\n", + "252 \ufffd\n", + "\n", + "253 \ufffd\n", + "\n", + "254 \ufffd\n", + "\n", + "255 \ufffd\n", + "\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:200" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "for ch in range(0,255,1):\n", + " print \"%d %c\\n\" % (ch,ch)\n", + "print \"%d %c\\n\" % (ch,ch)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "0 \u0000\n", + "\n", + "1 \u0001\n", + "\n", + "2 \u0002\n", + "\n", + "3 \u0003\n", + "\n", + "4 \u0004\n", + "\n", + "5 \u0005\n", + "\n", + "6 \u0006\n", + "\n", + "7 \u0007\n", + "\n", + "8 \b\n", + "\n", + "9 \t\n", + "\n", + "10 \n", + "\n", + "\n", + "11 \u000b", + "\n", + "\n", + "12 \f", + "\n", + "\n", + "13 \r\n", + "\n", + "14 \u000e\n", + "\n", + "15 \u000f\n", + "\n", + "16 \u0010\n", + "\n", + "17 \u0011\n", + "\n", + "18 \u0012\n", + "\n", + "19 \u0013\n", + "\n", + "20 \u0014\n", + "\n", + "21 \u0015\n", + "\n", + "22 \u0016\n", + "\n", + "23 \u0017\n", + "\n", + "24 \u0018\n", + "\n", + "25 \u0019\n", + "\n", + "26 \u001a\n", + "\n", + "27 \u001b\n", + "\n", + "28 \u001c", + "\n", + "\n", + "29 \u001d", + "\n", + "\n", + "30 \u001e", + "\n", + "\n", + "31 \u001f\n", + "\n", + "32 \n", + "\n", + "33 !\n", + "\n", + "34 \"\n", + "\n", + "35 #\n", + "\n", + "36 $\n", + "\n", + "37 %\n", + "\n", + "38 &\n", + "\n", + "39 '\n", + "\n", + "40 (\n", + "\n", + "41 )\n", + "\n", + "42 *\n", + "\n", + "43 +\n", + "\n", + "44 ,\n", + "\n", + "45 -\n", + "\n", + "46 .\n", + "\n", + "47 /\n", + "\n", + "48 0\n", + "\n", + "49 1\n", + "\n", + "50 2\n", + "\n", + "51 3\n", + "\n", + "52 4\n", + "\n", + "53 5\n", + "\n", + "54 6\n", + "\n", + "55 7\n", + "\n", + "56 8\n", + "\n", + "57 9\n", + "\n", + "58 :\n", + "\n", + "59 ;\n", + "\n", + "60 <\n", + "\n", + "61 =\n", + "\n", + "62 >\n", + "\n", + "63 ?\n", + "\n", + "64 @\n", + "\n", + "65 A\n", + "\n", + "66 B\n", + "\n", + "67 C\n", + "\n", + "68 D\n", + "\n", + "69 E\n", + "\n", + "70 F\n", + "\n", + "71 G\n", + "\n", + "72 H\n", + "\n", + "73 I\n", + "\n", + "74 J\n", + "\n", + "75 K\n", + "\n", + "76 L\n", + "\n", + "77 M\n", + "\n", + "78 N\n", + "\n", + "79 O\n", + "\n", + "80 P\n", + "\n", + "81 Q\n", + "\n", + "82 R\n", + "\n", + "83 S\n", + "\n", + "84 T\n", + "\n", + "85 U\n", + "\n", + "86 V\n", + "\n", + "87 W\n", + "\n", + "88 X\n", + "\n", + "89 Y\n", + "\n", + "90 Z\n", + "\n", + "91 [\n", + "\n", + "92 \\\n", + "\n", + "93 ]\n", + "\n", + "94 ^\n", + "\n", + "95 _\n", + "\n", + "96 `\n", + "\n", + "97 a\n", + "\n", + "98 b\n", + "\n", + "99 c\n", + "\n", + "100 d\n", + "\n", + "101 e\n", + "\n", + "102 f\n", + "\n", + "103 g\n", + "\n", + "104 h\n", + "\n", + "105 i\n", + "\n", + "106 j\n", + "\n", + "107 k\n", + "\n", + "108 l\n", + "\n", + "109 m\n", + "\n", + "110 n\n", + "\n", + "111 o\n", + "\n", + "112 p\n", + "\n", + "113 q\n", + "\n", + "114 r\n", + "\n", + "115 s\n", + "\n", + "116 t\n", + "\n", + "117 u\n", + "\n", + "118 v\n", + "\n", + "119 w\n", + "\n", + "120 x\n", + "\n", + "121 y\n", + "\n", + "122 z\n", + "\n", + "123 {\n", + "\n", + "124 |\n", + "\n", + "125 }\n", + "\n", + "126 ~\n", + "\n", + "127 \u007f\n", + "\n", + "128 \ufffd\n", + "\n", + "129 \ufffd\n", + "\n", + "130 \ufffd\n", + "\n", + "131 \ufffd\n", + "\n", + "132 \ufffd\n", + "\n", + "133 \ufffd\n", + "\n", + "134 \ufffd\n", + "\n", + "135 \ufffd\n", + "\n", + "136 \ufffd\n", + "\n", + "137 \ufffd\n", + "\n", + "138 \ufffd\n", + "\n", + "139 \ufffd\n", + "\n", + "140 \ufffd\n", + "\n", + "141 \ufffd\n", + "\n", + "142 \ufffd\n", + "\n", + "143 \ufffd\n", + "\n", + "144 \ufffd\n", + "\n", + "145 \ufffd\n", + "\n", + "146 \ufffd\n", + "\n", + "147 \ufffd\n", + "\n", + "148 \ufffd\n", + "\n", + "149 \ufffd\n", + "\n", + "150 \ufffd\n", + "\n", + "151 \ufffd\n", + "\n", + "152 \ufffd\n", + "\n", + "153 \ufffd\n", + "\n", + "154 \ufffd\n", + "\n", + "155 \ufffd\n", + "\n", + "156 \ufffd\n", + "\n", + "157 \ufffd\n", + "\n", + "158 \ufffd\n", + "\n", + "159 \ufffd\n", + "\n", + "160 \ufffd\n", + "\n", + "161 \ufffd\n", + "\n", + "162 \ufffd\n", + "\n", + "163 \ufffd\n", + "\n", + "164 \ufffd\n", + "\n", + "165 \ufffd\n", + "\n", + "166 \ufffd\n", + "\n", + "167 \ufffd\n", + "\n", + "168 \ufffd\n", + "\n", + "169 \ufffd\n", + "\n", + "170 \ufffd\n", + "\n", + "171 \ufffd\n", + "\n", + "172 \ufffd\n", + "\n", + "173 \ufffd\n", + "\n", + "174 \ufffd\n", + "\n", + "175 \ufffd\n", + "\n", + "176 \ufffd\n", + "\n", + "177 \ufffd\n", + "\n", + "178 \ufffd\n", + "\n", + "179 \ufffd\n", + "\n", + "180 \ufffd\n", + "\n", + "181 \ufffd\n", + "\n", + "182 \ufffd\n", + "\n", + "183 \ufffd\n", + "\n", + "184 \ufffd\n", + "\n", + "185 \ufffd\n", + "\n", + "186 \ufffd\n", + "\n", + "187 \ufffd\n", + "\n", + "188 \ufffd\n", + "\n", + "189 \ufffd\n", + "\n", + "190 \ufffd\n", + "\n", + "191 \ufffd\n", + "\n", + "192 \ufffd\n", + "\n", + "193 \ufffd\n", + "\n", + "194 \ufffd\n", + "\n", + "195 \ufffd\n", + "\n", + "196 \ufffd\n", + "\n", + "197 \ufffd\n", + "\n", + "198 \ufffd\n", + "\n", + "199 \ufffd\n", + "\n", + "200 \ufffd\n", + "\n", + "201 \ufffd\n", + "\n", + "202 \ufffd\n", + "\n", + "203 \ufffd\n", + "\n", + "204 \ufffd\n", + "\n", + "205 \ufffd\n", + "\n", + "206 \ufffd\n", + "\n", + "207 \ufffd\n", + "\n", + "208 \ufffd\n", + "\n", + "209 \ufffd\n", + "\n", + "210 \ufffd\n", + "\n", + "211 \ufffd\n", + "\n", + "212 \ufffd\n", + "\n", + "213 \ufffd\n", + "\n", + "214 \ufffd\n", + "\n", + "215 \ufffd\n", + "\n", + "216 \ufffd\n", + "\n", + "217 \ufffd\n", + "\n", + "218 \ufffd\n", + "\n", + "219 \ufffd\n", + "\n", + "220 \ufffd\n", + "\n", + "221 \ufffd\n", + "\n", + "222 \ufffd\n", + "\n", + "223 \ufffd\n", + "\n", + "224 \ufffd\n", + "\n", + "225 \ufffd\n", + "\n", + "226 \ufffd\n", + "\n", + "227 \ufffd\n", + "\n", + "228 \ufffd\n", + "\n", + "229 \ufffd\n", + "\n", + "230 \ufffd\n", + "\n", + "231 \ufffd\n", + "\n", + "232 \ufffd\n", + "\n", + "233 \ufffd\n", + "\n", + "234 \ufffd\n", + "\n", + "235 \ufffd\n", + "\n", + "236 \ufffd\n", + "\n", + "237 \ufffd\n", + "\n", + "238 \ufffd\n", + "\n", + "239 \ufffd\n", + "\n", + "240 \ufffd\n", + "\n", + "241 \ufffd\n", + "\n", + "242 \ufffd\n", + "\n", + "243 \ufffd\n", + "\n", + "244 \ufffd\n", + "\n", + "245 \ufffd\n", + "\n", + "246 \ufffd\n", + "\n", + "247 \ufffd\n", + "\n", + "248 \ufffd\n", + "\n", + "249 \ufffd\n", + "\n", + "250 \ufffd\n", + "\n", + "251 \ufffd\n", + "\n", + "252 \ufffd\n", + "\n", + "253 \ufffd\n", + "\n", + "254 \ufffd\n", + "\n", + "254 \ufffd\n", + "\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:200-201" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + " \n", + "c=raw_input()\n", + "d=raw_input()\n", + "print \"%c %c\\n\" % (c,d)\n", + "#int\n", + "i=eval(raw_input())\n", + "j=eval(raw_input())\n", + "print \"%d %u\\n\" % (i,j)\n", + "#short int\n", + "k=eval(raw_input())\n", + "l=eval(raw_input())\n", + "print \"%d %u\\n\" % (k,l)\n", + "#long int\n", + "m=eval(raw_input())\n", + "n=eval(raw_input())\n", + "print \"%ld %lu\\n\" % (m,n)\n", + "#float,double,long double\n", + "x=eval(raw_input())\n", + "y=eval(raw_input())\n", + "z=eval(raw_input())\n", + "print \"%f %lf %Lf\\n\" % (x,y,z)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "a\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "b\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "a b\n", + "\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "10000\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "7389\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "10000 7389\n", + "\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "34\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "585\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "34 585\n", + "\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "34676\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "500000\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "34676 500000\n", + "\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "445.55\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "57846.44\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "55666885.6655\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "445.550000 57846.440000 55666885.665500\n", + "\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:205" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def main():\n", + " i=1\n", + " def main2():\n", + " def main3():\n", + " print \"%d\" % (i) #prints 1\n", + " main3()\n", + " print \"%d\" % (i) #prints 1\n", + " main2()\n", + " print \"%d\\n\" % (i) #prints 1\n", + "main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n", + "1\n", + "1\n", + "\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:205" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def main():\n", + " i=1\n", + " def main2():\n", + " i=2\n", + " def main3():\n", + " i=3\n", + " print \"%d\" % (i) #prints 3\n", + " main3()\n", + " print \"%d\" % (i) #prints 2\n", + " main2()\n", + " print \"%d\\n\" % (i) #prints 1\n", + "main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n", + "2\n", + "1\n", + "\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:206" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "for i in range(1,11,1):\n", + " print \"%d\\n\" % (i)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n", + "\n", + "2\n", + "\n", + "3\n", + "\n", + "4\n", + "\n", + "5\n", + "\n", + "6\n", + "\n", + "7\n", + "\n", + "8\n", + "\n", + "9\n", + "\n", + "10\n", + "\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:208" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def increment():\n", + " i=1\n", + " print \"%d\\n\" % (i) #prints 1 every time\n", + " i=i+1\n", + "increment()\n", + "increment()\n", + "increment()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n", + "\n", + "1\n", + "\n", + "1\n", + "\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:208" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def static_var(varname, value): #function to make it static\n", + " def decorate(func):\n", + " setattr(func, varname, value)\n", + " return func\n", + " return decorate\n", + "@static_var(\"i\", 1) #i is static variable\n", + "def increment():\n", + " print \"%d\\n\" % increment.i\n", + " increment.i += 1\n", + "increment()\n", + "increment()\n", + "increment()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n", + "\n", + "2\n", + "\n", + "3\n", + "\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:209" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def fun():\n", + " k=35\n", + " return id(k) #returns address of k\n", + "j=fun() #stores address of k\n", + "print \"%d\\n\" % (j) #prints address of k" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "19639600\n", + "\n" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:210" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def increment():\n", + " global i #gloabal declaration\n", + " i=i+1\n", + " print \"on incrementing i=%d\\n\" % (i)\n", + "def decrement():\n", + " global i #global declaration\n", + " i=i-1\n", + " print \"on decrementing i=%d\\n\" % (i)\n", + "i=0\n", + "print \"\\ni=%d\" % (i)\n", + "increment()\n", + "increment()\n", + "decrement()\n", + "decrement()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "i=0\n", + "on incrementing i=1\n", + "\n", + "on incrementing i=2\n", + "\n", + "on decrementing i=1\n", + "\n", + "on decrementing i=0\n", + "\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:211" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "x=21\n", + "def main():\n", + " global x,y\n", + " print \"%d %d\\n\" % (x,y)\n", + "y=31\n", + "main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "21 31\n", + "\n" + ] + } + ], + "prompt_number": 11 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:211" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def display():\n", + " global x #global variable\n", + " print \"%d\\n\" % (x) #prints 10\n", + "x=10\n", + "def main():\n", + " x=20 #local variable for main\n", + " print \"%d\\n\" % (x) #prints 20\n", + " display()\n", + "main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "20\n", + "\n", + "10\n", + "\n" + ] + } + ], + "prompt_number": 12 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:212-213" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def fun1():\n", + " global i\n", + " i+=1\n", + " print \"%d\\n\" % (i)\n", + "def fun2():\n", + " global i\n", + " i-=1\n", + " print \"%d\\n\" % (i)\n", + "i=35\n", + "print \"%d\\n\" % (i)\n", + "fun1()\n", + "fun2()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "35\n", + "\n", + "36\n", + "\n", + "35\n", + "\n" + ] + } + ], + "prompt_number": 13 + } + ], + "metadata": {} + } + ] }
\ No newline at end of file diff --git a/ANSI_C_Programming/chapter7.ipynb b/ANSI_C_Programming/chapter7.ipynb index e79f1d7e..ad0fddd9 100644 --- a/ANSI_C_Programming/chapter7.ipynb +++ b/ANSI_C_Programming/chapter7.ipynb @@ -1,767 +1,766 @@ -{
- "metadata": {
- "name": "chapter7.ipynb"
- },
- "nbformat": 3,
- "nbformat_minor": 0,
- "worksheets": [
- {
- "cells": [
- {
- "cell_type": "heading",
- "level": 1,
- "metadata": {},
- "source": [
- "CHAPTER 7:THE C PREPROCESSOR"
- ]
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:226"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#using macros in for loop\n",
- "#Note:Implementing the concept of macros using function\n",
- "def UPPER(): #set UPPER() as 25\n",
- " return 25\n",
- "for i in range(1,UPPER()+1,1):\n",
- " print \"%d\\n\" % (i)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "1\n",
- "\n",
- "2\n",
- "\n",
- "3\n",
- "\n",
- "4\n",
- "\n",
- "5\n",
- "\n",
- "6\n",
- "\n",
- "7\n",
- "\n",
- "8\n",
- "\n",
- "9\n",
- "\n",
- "10\n",
- "\n",
- "11\n",
- "\n",
- "12\n",
- "\n",
- "13\n",
- "\n",
- "14\n",
- "\n",
- "15\n",
- "\n",
- "16\n",
- "\n",
- "17\n",
- "\n",
- "18\n",
- "\n",
- "19\n",
- "\n",
- "20\n",
- "\n",
- "21\n",
- "\n",
- "22\n",
- "\n",
- "23\n",
- "\n",
- "24\n",
- "\n",
- "25\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 1
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:227"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Defing PI as 3.14 using macros(here functions)\n",
- "def PI(): #set PI() as 3.14\n",
- " return 3.14\n",
- "r=6.25\n",
- "area=PI()*r*r\n",
- "print \"Area of circle=%f\\n\" % (area)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Area of circle=122.656250\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 2
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:229"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Defining AND & OR operations using macros\n",
- "def AND(x,y): #set AND(x,y) for \"and\" operation\n",
- " return (x and y)\n",
- "def OR(a,b): #set OR(a,b) for \"or\" operation\n",
- " return (a or b)\n",
- "f=1\n",
- "x=4\n",
- "y=90\n",
- "if (AND(f<5,OR(x<=20,y<=45))):\n",
- " print \"Your PC will always work fine...\\n\"\n",
- "else:\n",
- " print \"In front of the maintenance man\\n\""
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Your PC will always work fine...\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 3
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:229"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Using macros inside another macros\n",
- "def AND(x,y): #set AND(x,y) for \"and\" operation\n",
- " return (x and y)\n",
- "def ARANGE():\n",
- " return (AND(a>25,a<50)) #check for a>25 and a<50\n",
- "a=30\n",
- "if (ARANGE()):\n",
- " print \"within range\\n\"\n",
- "else:\n",
- " print \"out of range\\n\""
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "within range\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 4
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:230"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#using macros to replace whole statement\n",
- "def FOUND():\n",
- " print \"The Yankee Doodle Virus\\n\"\n",
- "signature='Y'\n",
- "if (signature=='Y'):\n",
- " FOUND()\n",
- "else:\n",
- " print \"Safe... as yet!\\n\""
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "The Yankee Doodle Virus\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 5
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:230"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#macros with arguments to find area of circle\n",
- "def AREA(x):\n",
- " return (3.14*x*x)\n",
- "r1=6.25\n",
- "r2=2.5\n",
- "a=AREA(r1)\n",
- "print \"Area of circle=%f\\n\" % (a)\n",
- "a=AREA(r2)\n",
- "print \"Area of circle=%f\\n\" % (a)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Area of circle=122.656250\n",
- "\n",
- "Area of circle=19.625000\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 6
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:231"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#compiler interpretation of above program\n",
- "r1=6.25\n",
- "r2=2.5\n",
- "a=3.14*r1*r1\n",
- "print \"Area of circle=%f\\n\" % (a)\n",
- "a=3.14*r2*r2\n",
- "print \"Area of circle=%f\\n\" % (a)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Area of circle=122.656250\n",
- "\n",
- "Area of circle=19.625000\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 7
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:231"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#macros with argument to check if user-input is digit or not\n",
- "def ISDIGIT(y):\n",
- " return (y>=48 and y<=57)\n",
- "print \"Enter any digit\"\n",
- "ch=raw_input()\n",
- "if (ISDIGIT(ord(ch))):\n",
- " print \"You entered a digit\\n\"\n",
- "else:\n",
- " print \"Illegal input\\n\""
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter any digit\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "7\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "You entered a digit\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 8
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:232"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#evaluating an expression\n",
- "def SQUARE(n):\n",
- " return (n*n)\n",
- "j=64/SQUARE(4) #it's function not macros ,so j=4\n",
- "print \"j=%d\\n\" % (j)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "j=4\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 4
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:233"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#gotoxy is not present in python,On Linux and other UNIX-like systems you can use curses library.\n",
- "#We can use setsyx func. of curses in place of gotoxy but it will be limited to unix only.\n",
- "def HLINE():\n",
- " for i in range(0,79,1):\n",
- " print \"%c\" % 45, #characters are bytes and therefore encoding-dependent(non utf-8)\n",
- " print \"\\n\"\n",
- "def VLINE(X,Y):\n",
- " print \"%c\" % 124 #If you want to write 179 in place of 124 then use utf-8 encoding\n",
- "HLINE()\n",
- "for y in range(1,25,1):\n",
- " VLINE(39,y)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \n",
- "\n",
- "|\n",
- "|\n",
- "|\n",
- "|\n",
- "|\n",
- "|\n",
- "|\n",
- "|\n",
- "|\n",
- "|\n",
- "|\n",
- "|\n",
- "|\n",
- "|\n",
- "|\n",
- "|\n",
- "|\n",
- "|\n",
- "|\n",
- "|\n",
- "|\n",
- "|\n",
- "|\n",
- "|\n"
- ]
- }
- ],
- "prompt_number": 3
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:236-237"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#using ifdef macros for conditional compilation\n",
- "OKAY=False\n",
- "if OKAY:\n",
- " import logging\n",
- " print \"statement 1\"\n",
- " print \"statement 2\" #detects virus\n",
- " print \"statement 3\" \n",
- " print \"statement 4\" #specific to stone virus\n",
- "print \"statement 5\"\n",
- "print \"statement 6\"\n",
- "print \"statement 7\""
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "statement 5\n",
- "statement 6\n",
- "statement 7\n"
- ]
- }
- ],
- "prompt_number": 5
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:237"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#using ifdef else macros for conditional compilation\n",
- "INTEL=False\n",
- "if INTEL:\n",
- " print \"code suitable for an Intel PC\"\n",
- "else:\n",
- " print \"code suitable for a Motorola PC\"\n",
- "print \"code common to both the computers\""
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "code suitable for a Motorola PC\n",
- "code common to both the computers\n"
- ]
- }
- ],
- "prompt_number": 14
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:238"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#using ifdef else macros for conditional compilation\n",
- "INTEL=True\n",
- "if INTEL:\n",
- " print \"code suitable for a Intel PC\"\n",
- "else:\n",
- " print \"code suitable for a motorola PC\"\n",
- "print \"code common to both the computers\""
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "code suitable for a Intel PC\n",
- "code common to both the computers\n"
- ]
- }
- ],
- "prompt_number": 15
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:239"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#using #if directive for conditional compilation\n",
- "TEST=6\n",
- "if TEST<=5:\n",
- " print \"statement 1\"\n",
- " print \"statement 2\"\n",
- " print \"statement 3\"\n",
- "else:\n",
- " print \"statement 4\"\n",
- " print \"statement 5\"\n",
- " print \"statement 6\""
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "statement 4\n",
- "statement 5\n",
- "statement 6\n"
- ]
- }
- ],
- "prompt_number": 16
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:239"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#using nested if else directives for conditional compilation\n",
- "ADAPTER='SVGA'\n",
- "if ADAPTER=='VGA':\n",
- " print \"code for video graphics array\"\n",
- "else:\n",
- " if ADAPTER=='SVGA':\n",
- " print \"code for super video graphics array\"\n",
- " else:\n",
- " print \"code for extended graphics array\""
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "code for super video graphics array\n"
- ]
- }
- ],
- "prompt_number": 17
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:239-240"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#using #elif directives for conditional compilation\n",
- "ADAPTER='SVGA'\n",
- "if ADAPTER=='VGA':\n",
- " print \"code for video graphics array\"\n",
- "elif ADAPTER=='SVGA':\n",
- " print \"code for super video graphics array\"\n",
- "else:\n",
- " print \"code for extented graphics adapter\""
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "code for super video graphics array\n"
- ]
- }
- ],
- "prompt_number": 18
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:241"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Controlled execution of functions\n",
- "def fun1():\n",
- " print \"Inside fun1\\n\"\n",
- "def fun2():\n",
- " print \"Inside fun 2\\n\"\n",
- "def main():\n",
- " print \"Inside main\\n\"\n",
- "fun1()\n",
- "main()\n",
- "fun2()"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Inside fun1\n",
- "\n",
- "Inside main\n",
- "\n",
- "Inside fun 2\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 19
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:242"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Statements causing warnings & error\n",
- "def f1():\n",
- " a=5\n",
- "def f2(x):\n",
- " print \"Inside f2\" #passed parameter is not being used\n",
- "def f3():\n",
- " x=6\n",
- " return x\n",
- " x+=1 #control can never reach this statement\n",
- "f1()\n",
- "f2(7)\n",
- "f3()"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Inside f2\n"
- ]
- },
- {
- "metadata": {},
- "output_type": "pyout",
- "prompt_number": 20,
- "text": [
- "6"
- ]
- }
- ],
- "prompt_number": 20
- }
- ],
- "metadata": {}
- }
- ]
+{ + "metadata": { + "name": "", + "signature": "sha256:c550564369c57fef1a3abbaae1e7e03e923c7091530a87d2bf35262354fa8d51" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "CHAPTER 7:THE C PREPROCESSOR" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:226" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def UPPER(): #set UPPER() as 25\n", + " return 25\n", + "for i in range(1,UPPER()+1,1):\n", + " print \"%d\\n\" % (i)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n", + "\n", + "2\n", + "\n", + "3\n", + "\n", + "4\n", + "\n", + "5\n", + "\n", + "6\n", + "\n", + "7\n", + "\n", + "8\n", + "\n", + "9\n", + "\n", + "10\n", + "\n", + "11\n", + "\n", + "12\n", + "\n", + "13\n", + "\n", + "14\n", + "\n", + "15\n", + "\n", + "16\n", + "\n", + "17\n", + "\n", + "18\n", + "\n", + "19\n", + "\n", + "20\n", + "\n", + "21\n", + "\n", + "22\n", + "\n", + "23\n", + "\n", + "24\n", + "\n", + "25\n", + "\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:227" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def PI(): #set PI() as 3.14\n", + " return 3.14\n", + "r=6.25\n", + "area=PI()*r*r\n", + "print \"Area of circle=%f\\n\" % (area)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Area of circle=122.656250\n", + "\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:229" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def AND(x,y): #set AND(x,y) for \"and\" operation\n", + " return (x and y)\n", + "def OR(a,b): #set OR(a,b) for \"or\" operation\n", + " return (a or b)\n", + "f=1\n", + "x=4\n", + "y=90\n", + "if (AND(f<5,OR(x<=20,y<=45))):\n", + " print \"Your PC will always work fine...\\n\"\n", + "else:\n", + " print \"In front of the maintenance man\\n\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Your PC will always work fine...\n", + "\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:229" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def AND(x,y): #set AND(x,y) for \"and\" operation\n", + " return (x and y)\n", + "def ARANGE():\n", + " return (AND(a>25,a<50)) #check for a>25 and a<50\n", + "a=30\n", + "if (ARANGE()):\n", + " print \"within range\\n\"\n", + "else:\n", + " print \"out of range\\n\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "within range\n", + "\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:230" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def FOUND():\n", + " print \"The Yankee Doodle Virus\\n\"\n", + "signature='Y'\n", + "if (signature=='Y'):\n", + " FOUND()\n", + "else:\n", + " print \"Safe... as yet!\\n\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The Yankee Doodle Virus\n", + "\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:230" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def AREA(x):\n", + " return (3.14*x*x)\n", + "r1=6.25\n", + "r2=2.5\n", + "a=AREA(r1)\n", + "print \"Area of circle=%f\\n\" % (a)\n", + "a=AREA(r2)\n", + "print \"Area of circle=%f\\n\" % (a)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Area of circle=122.656250\n", + "\n", + "Area of circle=19.625000\n", + "\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:231" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "r1=6.25\n", + "r2=2.5\n", + "a=3.14*r1*r1\n", + "print \"Area of circle=%f\\n\" % (a)\n", + "a=3.14*r2*r2\n", + "print \"Area of circle=%f\\n\" % (a)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Area of circle=122.656250\n", + "\n", + "Area of circle=19.625000\n", + "\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:231" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def ISDIGIT(y):\n", + " return (y>=48 and y<=57)\n", + "print \"Enter any digit\"\n", + "ch=raw_input()\n", + "if (ISDIGIT(ord(ch))):\n", + " print \"You entered a digit\\n\"\n", + "else:\n", + " print \"Illegal input\\n\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter any digit\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "7\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "You entered a digit\n", + "\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:232" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def SQUARE(n):\n", + " return (n*n)\n", + "j=64/SQUARE(4) #it's function not macros ,so j=4\n", + "print \"j=%d\\n\" % (j)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "j=4\n", + "\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:233" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def HLINE():\n", + " for i in range(0,79,1):\n", + " print \"%c\" % 45, #characters are bytes and therefore encoding-dependent(non utf-8)\n", + " print \"\\n\"\n", + "def VLINE(X,Y):\n", + " print \"%c\" % 124 #If you want to write 179 in place of 124 then use utf-8 encoding\n", + "HLINE()\n", + "for y in range(1,25,1):\n", + " VLINE(39,y)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \n", + "\n", + "|\n", + "|\n", + "|\n", + "|\n", + "|\n", + "|\n", + "|\n", + "|\n", + "|\n", + "|\n", + "|\n", + "|\n", + "|\n", + "|\n", + "|\n", + "|\n", + "|\n", + "|\n", + "|\n", + "|\n", + "|\n", + "|\n", + "|\n", + "|\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:236-237" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "OKAY=False\n", + "if OKAY:\n", + " import logging\n", + " print \"statement 1\"\n", + " print \"statement 2\" #detects virus\n", + " print \"statement 3\" \n", + " print \"statement 4\" #specific to stone virus\n", + "print \"statement 5\"\n", + "print \"statement 6\"\n", + "print \"statement 7\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "statement 5\n", + "statement 6\n", + "statement 7\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:237" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "INTEL=False\n", + "if INTEL:\n", + " print \"code suitable for an Intel PC\"\n", + "else:\n", + " print \"code suitable for a Motorola PC\"\n", + "print \"code common to both the computers\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "code suitable for a Motorola PC\n", + "code common to both the computers\n" + ] + } + ], + "prompt_number": 14 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:238" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "INTEL=True\n", + "if INTEL:\n", + " print \"code suitable for a Intel PC\"\n", + "else:\n", + " print \"code suitable for a motorola PC\"\n", + "print \"code common to both the computers\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "code suitable for a Intel PC\n", + "code common to both the computers\n" + ] + } + ], + "prompt_number": 15 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:239" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "TEST=6\n", + "if TEST<=5:\n", + " print \"statement 1\"\n", + " print \"statement 2\"\n", + " print \"statement 3\"\n", + "else:\n", + " print \"statement 4\"\n", + " print \"statement 5\"\n", + " print \"statement 6\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "statement 4\n", + "statement 5\n", + "statement 6\n" + ] + } + ], + "prompt_number": 16 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:239" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "ADAPTER='SVGA'\n", + "if ADAPTER=='VGA':\n", + " print \"code for video graphics array\"\n", + "else:\n", + " if ADAPTER=='SVGA':\n", + " print \"code for super video graphics array\"\n", + " else:\n", + " print \"code for extended graphics array\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "code for super video graphics array\n" + ] + } + ], + "prompt_number": 17 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:239-240" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "ADAPTER='SVGA'\n", + "if ADAPTER=='VGA':\n", + " print \"code for video graphics array\"\n", + "elif ADAPTER=='SVGA':\n", + " print \"code for super video graphics array\"\n", + "else:\n", + " print \"code for extented graphics adapter\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "code for super video graphics array\n" + ] + } + ], + "prompt_number": 18 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:241" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def fun1():\n", + " print \"Inside fun1\\n\"\n", + "def fun2():\n", + " print \"Inside fun 2\\n\"\n", + "def main():\n", + " print \"Inside main\\n\"\n", + "fun1()\n", + "main()\n", + "fun2()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Inside fun1\n", + "\n", + "Inside main\n", + "\n", + "Inside fun 2\n", + "\n" + ] + } + ], + "prompt_number": 19 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:242" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def f1():\n", + " a=5\n", + "def f2(x):\n", + " print \"Inside f2\" #passed parameter is not being used\n", + "def f3():\n", + " x=6\n", + " return x\n", + " x+=1 #control can never reach this statement\n", + "f1()\n", + "f2(7)\n", + "f3()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Inside f2\n" + ] + }, + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 20, + "text": [ + "6" + ] + } + ], + "prompt_number": 20 + } + ], + "metadata": {} + } + ] }
\ No newline at end of file diff --git a/ANSI_C_Programming/chapter8.ipynb b/ANSI_C_Programming/chapter8.ipynb index 9dbb6099..50b1b566 100644 --- a/ANSI_C_Programming/chapter8.ipynb +++ b/ANSI_C_Programming/chapter8.ipynb @@ -1,1400 +1,1400 @@ -{
- "metadata": {
- "name": "chapter8.ipynb"
- },
- "nbformat": 3,
- "nbformat_minor": 0,
- "worksheets": [
- {
- "cells": [
- {
- "cell_type": "heading",
- "level": 1,
- "metadata": {},
- "source": [
- "CHAPTER 8: ARRAYS"
- ]
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:256"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#reassigning value to a variable\n",
- "x=5\n",
- "x=10\n",
- "print \"x=%d\\n\" % (x) #prints 10"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "x=10\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 1
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:257-258"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#program to find average marks obtained by a class of 30 students\n",
- "sum=0\n",
- "marks=[] #array declaration\n",
- "for i in range(30):\n",
- " marks.append(0)\n",
- "for i in range(0,30,1):\n",
- " print \"Enter marks\"\n",
- " marks[i]=eval(raw_input()) #store data in array\n",
- "for i in range(0,30,1):\n",
- " sum=sum+marks[i]\n",
- "avg=sum/30\n",
- "print \"Average marks=%d\\n\" % (avg)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "98\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "88\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "97\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "96\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "87\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "89\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "80\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "86\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "95\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "99\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "90\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "100\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "99\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "94\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "85\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "84\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "92\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "91\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "87\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "76\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "75\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "96\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "83\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "80\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "93\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "79\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "70\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "78\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "85\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "86\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Average marks=87\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 2
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:261"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#storing elements into an array\n",
- "num=[]\n",
- "for i in range(40):\n",
- " num.append(0)\n",
- "for i in range(0,40,1): #be carefull about the size of the array\n",
- " num[i]=i"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [],
- "prompt_number": 2
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:261-262"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Demonstration of call by value\n",
- "def display(m):\n",
- " print \"%d\" % (m)\n",
- "marks=[55,65,75,56,78,78,90]\n",
- "for i in range(0,7,1):\n",
- " display(marks[i])"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "55\n",
- "65\n",
- "75\n",
- "56\n",
- "78\n",
- "78\n",
- "90\n"
- ]
- }
- ],
- "prompt_number": 3
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:263"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#calling of function inside function to print an array\n",
- "def show(n):\n",
- " print \"%d\" % (n)\n",
- "def disp(n):\n",
- " show(n) #calls show()\n",
- "marks=[55,65,75,56,78,78,90]\n",
- "for i in range(0,7,1):\n",
- " disp(marks[i])"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "55\n",
- "65\n",
- "75\n",
- "56\n",
- "78\n",
- "78\n",
- "90\n"
- ]
- }
- ],
- "prompt_number": 4
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:263-264"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#printing address and values of variables\n",
- "i=3\n",
- "j=1.5\n",
- "k='c'\n",
- "print \"Value of i=%d\\n\" % (i)\n",
- "print \"Value of j=%f\\n\" % (j)\n",
- "print \"Value of k=%c\\n\" % (k)\n",
- "x=id(i)\n",
- "y=id(j)\n",
- "z=id(k)\n",
- "print \"Original address in x=%u\\n\" % (x)\n",
- "print \"Original address in y=%u\\n\" % (y)\n",
- "print \"Original address in z=%u\\n\" % (z)\n",
- "x+=1\n",
- "y+=1\n",
- "z+=1\n",
- "print \"New address in x=%u\\n\" % (x)\n",
- "print \"New address in y=%u\\n\" % (y)\n",
- "print \"New address in z=%u\\n\" % (z)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Value of i=3\n",
- "\n",
- "Value of j=1.500000\n",
- "\n",
- "Value of k=c\n",
- "\n",
- "Original address in x=21147312\n",
- "\n",
- "Original address in y=88181272\n",
- "\n",
- "Original address in z=21311640\n",
- "\n",
- "New address in x=21147313\n",
- "\n",
- "New address in y=88181273\n",
- "\n",
- "New address in z=21311641\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 5
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:265"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#printing subtraction of addresses & values of 2 elements of array\n",
- "arr=[10,20,30,45,67,56,74]\n",
- "i=id(arr[1])\n",
- "j=id(arr[5])\n",
- "print \"%d %d\\n\" % (j-i,arr[5]-arr[1])"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "-432 36\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 6
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:266"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Comparison of addresses of elements of same array\n",
- "arr=[10,20,36,72,45,36]\n",
- "j=id(arr[4])\n",
- "k=id(arr[0+4]) #didn't get any other way\n",
- "if j==k:\n",
- " print \"The two pointers point to the same location\\n\"\n",
- "else:\n",
- " print \"The two pointers do not point to the same location\\n\""
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "The two pointers point to the same location\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 7
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:267"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "##printing memory location of elements of array\n",
- "num=[24,34,12,44,56,17]\n",
- "for i in range(0,6,1):\n",
- " print \"element no.%d\" % (i)\n",
- " print \"address=%u\\n\" % (id(num[i]))"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "element no.0\n",
- "address=21147060\n",
- "\n",
- "element no.1\n",
- "address=21146940\n",
- "\n",
- "element no.2\n",
- "address=21147204\n",
- "\n",
- "element no.3\n",
- "address=21146820\n",
- "\n",
- "element no.4\n",
- "address=21146676\n",
- "\n",
- "element no.5\n",
- "address=21147144\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 8
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:268"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#print addresses & elements of array\n",
- "num=[24,34,12,44,56,17]\n",
- "for i in range(0,6,1):\n",
- " print \"address=%u\" % (id(num[i]))\n",
- " print \"element=%d\\n\" % (num[i])"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "address=21147060\n",
- "element=24\n",
- "\n",
- "address=21146940\n",
- "element=34\n",
- "\n",
- "address=21147204\n",
- "element=12\n",
- "\n",
- "address=21146820\n",
- "element=44\n",
- "\n",
- "address=21146676\n",
- "element=56\n",
- "\n",
- "address=21147144\n",
- "element=17\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 10
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:268"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#program for accessing the elements of array\n",
- "num=[24,34,12,44,56,17]\n",
- "j=id(num[0]) #assign address of zeroth element\n",
- "for i in range(0,6,1):\n",
- " print \"address=%u\" % (id(num[i]))\n",
- " print \"element=%d\\n\" % (num[i]) "
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "address=21147060\n",
- "element=24\n",
- "\n",
- "address=21146940\n",
- "element=34\n",
- "\n",
- "address=21147204\n",
- "element=12\n",
- "\n",
- "address=21146820\n",
- "element=44\n",
- "\n",
- "address=21146676\n",
- "element=56\n",
- "\n",
- "address=21147144\n",
- "element=17\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 11
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:270"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Demonstration of passing an entire array to a function\n",
- "def display(j,n):\n",
- " for item in j:\n",
- " print \"element=%d\\n\" % (item)\n",
- "num=[24,34,12,44,56,17]\n",
- "display(num,6)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "element=24\n",
- "\n",
- "element=34\n",
- "\n",
- "element=12\n",
- "\n",
- "element=44\n",
- "\n",
- "element=56\n",
- "\n",
- "element=17\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 12
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:271"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Another way of accessing the elements of array\n",
- "num=[24,34,12,44,56,17]\n",
- "for i in range(0,6,1):\n",
- " print \"address=%u\" % (id(num[i]))\n",
- " print \"element=%d %d\" % (num[i],num[i]) #no other way\n",
- " print \"%d %d\\n\" % (num[i],num[i])"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "address=21147060\n",
- "element=24 24\n",
- "24 24\n",
- "\n",
- "address=21146940\n",
- "element=34 34\n",
- "34 34\n",
- "\n",
- "address=21147204\n",
- "element=12 12\n",
- "12 12\n",
- "\n",
- "address=21146820\n",
- "element=44 44\n",
- "44 44\n",
- "\n",
- "address=21146676\n",
- "element=56 56\n",
- "56 56\n",
- "\n",
- "address=21147144\n",
- "element=17 17\n",
- "17 17\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 13
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:272"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Storing roll no. & marks of 2 students using 2-D array\n",
- "stud=[[0,0],[0,0],[0,0],[0,0]]\n",
- "for i in range(0,4,1):\n",
- " print \"Enter roll no. and marks\"\n",
- " stud[i][0]=eval(raw_input())\n",
- " stud[i][1]=eval(raw_input())\n",
- "for i in range(0,4,1):\n",
- " print \"%d %d\\n\" % (stud[i][0],stud[i][1])"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter roll no. and marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "1206030\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "100\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter roll no. and marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "1206007\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "95\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter roll no. and marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "1206034\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "97\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter roll no. and marks\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "1206026\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "88\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "1206030 100\n",
- "\n",
- "1206007 95\n",
- "\n",
- "1206034 97\n",
- "\n",
- "1206026 88\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 14
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:275"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Demo:2-D array is an array of arrays\n",
- "s=[[1234,56],[1212,33],[1434,80],[1312,78]]\n",
- "for i in range(0,4,1):\n",
- " print \"Address of %dth 1-D array=%u\\n\" % (i,id(s[i]))"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Address of 0th 1-D array=88430392\n",
- "\n",
- "Address of 1th 1-D array=89514192\n",
- "\n",
- "Address of 2th 1-D array=88430712\n",
- "\n",
- "Address of 3th 1-D array=88430312\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 15
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:277"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Accessing elements of 2-D array\n",
- "s=[[1234,56],[1212,33],[1434,80],[1312,78]]\n",
- "for i in range(0,4,1):\n",
- " for j in range(0,2,1):\n",
- " print \"%d\" % (s[i][j])\n",
- " print \"\\n\""
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "1234\n",
- "56\n",
- "\n",
- "\n",
- "1212\n",
- "33\n",
- "\n",
- "\n",
- "1434\n",
- "80\n",
- "\n",
- "\n",
- "1312\n",
- "78\n",
- "\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 16
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:277-278"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Usage of pointer to an array\n",
- "s=[[1234,56],[1212,33],[1434,80],[1312,78]]\n",
- "p=[]\n",
- "for i in range(2):\n",
- " p.append(0)\n",
- "for i in range(0,4,1):\n",
- " p=s\n",
- " pint=p\n",
- " print \"\\n\"\n",
- " for j in range(0,2,1):\n",
- " print \"%d\" % (pint[i][j])"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "\n",
- "\n",
- "1234\n",
- "56\n",
- "\n",
- "\n",
- "1212\n",
- "33\n",
- "\n",
- "\n",
- "1434\n",
- "80\n",
- "\n",
- "\n",
- "1312\n",
- "78\n"
- ]
- }
- ],
- "prompt_number": 18
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:279-280"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Passing 2-D array to a function\n",
- "#can only implement only one way due to absence of pointers in python\n",
- "def display(q,row,col):\n",
- " for item in q:\n",
- " print item\n",
- " print \"\\n\"\n",
- "a=[[1,2,3,4],[5,6,7,8],[9,0,1,6]]\n",
- "display(a,3,4)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "[1, 2, 3, 4]\n",
- "[5, 6, 7, 8]\n",
- "[9, 0, 1, 6]\n",
- "\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 20
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:281-282"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Assigning values to the elements of an array seperately\n",
- "arr=[]\n",
- "for i in range(4):\n",
- " arr.append(0)\n",
- "i=31\n",
- "j=5\n",
- "k=19\n",
- "l=71\n",
- "arr[0]=i\n",
- "arr[1]=j\n",
- "arr[2]=k\n",
- "arr[3]=l\n",
- "for m in range(0,4,1):\n",
- " print \"%d\\n\" % (arr[m])"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "31\n",
- "\n",
- "5\n",
- "\n",
- "19\n",
- "\n",
- "71\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 21
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:282"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#printing address of an element of array\n",
- "a=[0,1,2,3,4]\n",
- "print \"%u %u %d\\n\" % (id(id(a[0])),id(a[0]),a[0])"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "88518416 21147348 0\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 22
- }
- ],
- "metadata": {}
- }
- ]
+{ + "metadata": { + "name": "", + "signature": "sha256:7a02054929dffb84c1e468572cd4b1d9ed109e9267a67821f68ad3b293946118" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "CHAPTER 8: ARRAYS" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:256" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "x=5\n", + "x=10\n", + "print \"x=%d\\n\" % (x) #prints 10" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "x=10\n", + "\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:257-258" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "sum=0\n", + "marks=[] #array declaration\n", + "for i in range(30):\n", + " marks.append(0)\n", + "for i in range(0,30,1):\n", + " print \"Enter marks\"\n", + " marks[i]=eval(raw_input()) #store data in array\n", + "for i in range(0,30,1):\n", + " sum=sum+marks[i]\n", + "avg=sum/30\n", + "print \"Average marks=%d\\n\" % (avg)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter marks\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "98\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter marks\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "88\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter marks\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "97\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter marks\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "96\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter marks\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "87\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter marks\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "89\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter marks\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "80\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter marks\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "86\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter marks\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "95\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter marks\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "99\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter marks\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "90\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter marks\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "100\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter marks\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "99\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter marks\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "94\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter marks\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "85\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter marks\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "84\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter marks\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "92\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter marks\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "91\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter marks\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "87\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter marks\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "76\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter marks\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "75\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter marks\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "96\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter marks\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "83\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter marks\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "80\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter marks\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "93\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter marks\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "79\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter marks\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "70\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter marks\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "78\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter marks\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "85\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter marks\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "86\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Average marks=87\n", + "\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:261" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "num=[]\n", + "for i in range(40):\n", + " num.append(0)\n", + "for i in range(0,40,1): #be carefull about the size of the array\n", + " num[i]=i" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:261-262" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def display(m):\n", + " print \"%d\" % (m)\n", + "marks=[55,65,75,56,78,78,90]\n", + "for i in range(0,7,1):\n", + " display(marks[i])" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "55\n", + "65\n", + "75\n", + "56\n", + "78\n", + "78\n", + "90\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:263" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def show(n):\n", + " print \"%d\" % (n)\n", + "def disp(n):\n", + " show(n) #calls show()\n", + "marks=[55,65,75,56,78,78,90]\n", + "for i in range(0,7,1):\n", + " disp(marks[i])" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "55\n", + "65\n", + "75\n", + "56\n", + "78\n", + "78\n", + "90\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:263-264" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "i=3\n", + "j=1.5\n", + "k='c'\n", + "print \"Value of i=%d\\n\" % (i)\n", + "print \"Value of j=%f\\n\" % (j)\n", + "print \"Value of k=%c\\n\" % (k)\n", + "x=id(i)\n", + "y=id(j)\n", + "z=id(k)\n", + "print \"Original address in x=%u\\n\" % (x)\n", + "print \"Original address in y=%u\\n\" % (y)\n", + "print \"Original address in z=%u\\n\" % (z)\n", + "x+=1\n", + "y+=1\n", + "z+=1\n", + "print \"New address in x=%u\\n\" % (x)\n", + "print \"New address in y=%u\\n\" % (y)\n", + "print \"New address in z=%u\\n\" % (z)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Value of i=3\n", + "\n", + "Value of j=1.500000\n", + "\n", + "Value of k=c\n", + "\n", + "Original address in x=21147312\n", + "\n", + "Original address in y=88181272\n", + "\n", + "Original address in z=21311640\n", + "\n", + "New address in x=21147313\n", + "\n", + "New address in y=88181273\n", + "\n", + "New address in z=21311641\n", + "\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:265" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "arr=[10,20,30,45,67,56,74]\n", + "i=id(arr[1])\n", + "j=id(arr[5])\n", + "print \"%d %d\\n\" % (j-i,arr[5]-arr[1])" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "-432 36\n", + "\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:266" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "arr=[10,20,36,72,45,36]\n", + "j=id(arr[4])\n", + "k=id(arr[0+4]) #didn't get any other way\n", + "if j==k:\n", + " print \"The two pointers point to the same location\\n\"\n", + "else:\n", + " print \"The two pointers do not point to the same location\\n\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The two pointers point to the same location\n", + "\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:267" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "num=[24,34,12,44,56,17]\n", + "for i in range(0,6,1):\n", + " print \"element no.%d\" % (i)\n", + " print \"address=%u\\n\" % (id(num[i]))" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "element no.0\n", + "address=21147060\n", + "\n", + "element no.1\n", + "address=21146940\n", + "\n", + "element no.2\n", + "address=21147204\n", + "\n", + "element no.3\n", + "address=21146820\n", + "\n", + "element no.4\n", + "address=21146676\n", + "\n", + "element no.5\n", + "address=21147144\n", + "\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:268" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "num=[24,34,12,44,56,17]\n", + "for i in range(0,6,1):\n", + " print \"address=%u\" % (id(num[i]))\n", + " print \"element=%d\\n\" % (num[i])" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "address=21147060\n", + "element=24\n", + "\n", + "address=21146940\n", + "element=34\n", + "\n", + "address=21147204\n", + "element=12\n", + "\n", + "address=21146820\n", + "element=44\n", + "\n", + "address=21146676\n", + "element=56\n", + "\n", + "address=21147144\n", + "element=17\n", + "\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:268" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "num=[24,34,12,44,56,17]\n", + "j=id(num[0]) #assign address of zeroth element\n", + "for i in range(0,6,1):\n", + " print \"address=%u\" % (id(num[i]))\n", + " print \"element=%d\\n\" % (num[i]) " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "address=21147060\n", + "element=24\n", + "\n", + "address=21146940\n", + "element=34\n", + "\n", + "address=21147204\n", + "element=12\n", + "\n", + "address=21146820\n", + "element=44\n", + "\n", + "address=21146676\n", + "element=56\n", + "\n", + "address=21147144\n", + "element=17\n", + "\n" + ] + } + ], + "prompt_number": 11 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:270" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def display(j,n):\n", + " for item in j:\n", + " print \"element=%d\\n\" % (item)\n", + "num=[24,34,12,44,56,17]\n", + "display(num,6)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "element=24\n", + "\n", + "element=34\n", + "\n", + "element=12\n", + "\n", + "element=44\n", + "\n", + "element=56\n", + "\n", + "element=17\n", + "\n" + ] + } + ], + "prompt_number": 12 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:271" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "num=[24,34,12,44,56,17]\n", + "for i in range(0,6,1):\n", + " print \"address=%u\" % (id(num[i]))\n", + " print \"element=%d %d\" % (num[i],num[i]) #no other way\n", + " print \"%d %d\\n\" % (num[i],num[i])" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "address=21147060\n", + "element=24 24\n", + "24 24\n", + "\n", + "address=21146940\n", + "element=34 34\n", + "34 34\n", + "\n", + "address=21147204\n", + "element=12 12\n", + "12 12\n", + "\n", + "address=21146820\n", + "element=44 44\n", + "44 44\n", + "\n", + "address=21146676\n", + "element=56 56\n", + "56 56\n", + "\n", + "address=21147144\n", + "element=17 17\n", + "17 17\n", + "\n" + ] + } + ], + "prompt_number": 13 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:272" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "stud=[[0,0],[0,0],[0,0],[0,0]]\n", + "for i in range(0,4,1):\n", + " print \"Enter roll no. and marks\"\n", + " stud[i][0]=eval(raw_input())\n", + " stud[i][1]=eval(raw_input())\n", + "for i in range(0,4,1):\n", + " print \"%d %d\\n\" % (stud[i][0],stud[i][1])" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter roll no. and marks\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1206030\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "100\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter roll no. and marks\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1206007\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "95\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter roll no. and marks\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1206034\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "97\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter roll no. and marks\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1206026\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "88\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "1206030 100\n", + "\n", + "1206007 95\n", + "\n", + "1206034 97\n", + "\n", + "1206026 88\n", + "\n" + ] + } + ], + "prompt_number": 14 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:275" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "s=[[1234,56],[1212,33],[1434,80],[1312,78]]\n", + "for i in range(0,4,1):\n", + " print \"Address of %dth 1-D array=%u\\n\" % (i,id(s[i]))" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Address of 0th 1-D array=88430392\n", + "\n", + "Address of 1th 1-D array=89514192\n", + "\n", + "Address of 2th 1-D array=88430712\n", + "\n", + "Address of 3th 1-D array=88430312\n", + "\n" + ] + } + ], + "prompt_number": 15 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:277" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "s=[[1234,56],[1212,33],[1434,80],[1312,78]]\n", + "for i in range(0,4,1):\n", + " for j in range(0,2,1):\n", + " print \"%d\" % (s[i][j])\n", + " print \"\\n\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "1234\n", + "56\n", + "\n", + "\n", + "1212\n", + "33\n", + "\n", + "\n", + "1434\n", + "80\n", + "\n", + "\n", + "1312\n", + "78\n", + "\n", + "\n" + ] + } + ], + "prompt_number": 16 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:277-278" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "s=[[1234,56],[1212,33],[1434,80],[1312,78]]\n", + "p=[]\n", + "for i in range(2):\n", + " p.append(0)\n", + "for i in range(0,4,1):\n", + " p=s\n", + " pint=p\n", + " print \"\\n\"\n", + " for j in range(0,2,1):\n", + " print \"%d\" % (pint[i][j])" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "\n", + "1234\n", + "56\n", + "\n", + "\n", + "1212\n", + "33\n", + "\n", + "\n", + "1434\n", + "80\n", + "\n", + "\n", + "1312\n", + "78\n" + ] + } + ], + "prompt_number": 18 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:279-280" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def display(q,row,col):\n", + " for item in q:\n", + " print item\n", + " print \"\\n\"\n", + "a=[[1,2,3,4],[5,6,7,8],[9,0,1,6]]\n", + "display(a,3,4)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "[1, 2, 3, 4]\n", + "[5, 6, 7, 8]\n", + "[9, 0, 1, 6]\n", + "\n", + "\n" + ] + } + ], + "prompt_number": 20 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:281-282" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "arr=[]\n", + "for i in range(4):\n", + " arr.append(0)\n", + "i=31\n", + "j=5\n", + "k=19\n", + "l=71\n", + "arr[0]=i\n", + "arr[1]=j\n", + "arr[2]=k\n", + "arr[3]=l\n", + "for m in range(0,4,1):\n", + " print \"%d\\n\" % (arr[m])" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "31\n", + "\n", + "5\n", + "\n", + "19\n", + "\n", + "71\n", + "\n" + ] + } + ], + "prompt_number": 21 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:282" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "a=[0,1,2,3,4]\n", + "print \"%u %u %d\\n\" % (id(id(a[0])),id(a[0]),a[0])" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "88518416 21147348 0\n", + "\n" + ] + } + ], + "prompt_number": 22 + } + ], + "metadata": {} + } + ] }
\ No newline at end of file diff --git a/ANSI_C_Programming/chapter9.ipynb b/ANSI_C_Programming/chapter9.ipynb index 43e1d355..54eec269 100644 --- a/ANSI_C_Programming/chapter9.ipynb +++ b/ANSI_C_Programming/chapter9.ipynb @@ -1,958 +1,958 @@ -{
- "metadata": {
- "name": "chapter9.ipynb"
- },
- "nbformat": 3,
- "nbformat_minor": 0,
- "worksheets": [
- {
- "cells": [
- {
- "cell_type": "heading",
- "level": 1,
- "metadata": {},
- "source": [
- "CHAPTER 9: PUPPETTING ON STRINGS"
- ]
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:311"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Program to demonstrate printing of a string\n",
- "name=\"Klinsman\"\n",
- "i=0\n",
- "while i<=7:\n",
- " print \"%c\" % (name[i]), #method of printing without line feed\n",
- " i+=1\n",
- "print \"\\n\""
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "K l i n s m a n \n",
- "\n"
- ]
- }
- ],
- "prompt_number": 4
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:311-312"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#printing a string stored in 1-D array\n",
- "name=\"Klinsman \"\n",
- "i=0\n",
- "while name[i]!=' ':\n",
- " print \"%c\" % (name[i]), \n",
- " i+=1\n",
- "print \"\\n\""
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- " K l i n s m a n \n",
- "\n"
- ]
- }
- ],
- "prompt_number": 14
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:312"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Program to demonstrate printing of a string\n",
- "name=\"Klinsman \"\n",
- "ptr=[]\n",
- "ptr=name #store base address of string\n",
- "i=0\n",
- "while name[i]!=' ':\n",
- " print \"%c\" % (ptr[i]), \n",
- " i+=1\n",
- "print \"\\n\""
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "K l i n s m a n \n",
- "\n"
- ]
- }
- ],
- "prompt_number": 18
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:313"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#printing a string\n",
- "name=\"Klinsman\"\n",
- "print \"%s\" % (name)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Klinsman\n"
- ]
- }
- ],
- "prompt_number": 19
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:313"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#storing & printing a string\n",
- "print \"Enter your name\"\n",
- "name=raw_input()\n",
- "print \"Hello %s!\\n\" % (name)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter your name\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Debashish\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Hello Debashish!\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 20
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:314"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#storing & printing a string\n",
- "print \"Enter your full name:\"\n",
- "name=raw_input()\n",
- "print \"Hello!\"\n",
- "print name"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter your full name:\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Debashish Roy\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Hello!\n",
- "Debashish Roy\n"
- ]
- }
- ],
- "prompt_number": 21
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:315"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Dynamic variables in python\n",
- "str1=\"Hello\"\n",
- "s=\"Good Morning\"\n",
- "str2=str1 #No error in Python since all variables are by default pointers\n",
- "q=s"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [],
- "prompt_number": 6
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:316"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Dynamic allocation in python\n",
- "str1=\"Hello\"\n",
- "p=\"Hello\"\n",
- "str1=\"Bye\" #No error in python\n",
- "p=\"Bye\""
- ],
- "language": "python",
- "metadata": {},
- "outputs": [],
- "prompt_number": 5
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:317"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#to count number of characters in an array\n",
- "arr=\"Bamboozled\"\n",
- "len1=len(arr) #return length of string\n",
- "len2=len(\"Humpty Dumpty\")\n",
- "print \"string=%s length=%d\\n\" % (arr,len1)\n",
- "print \"string=%s length=%d\\n\" % (\"Humpty Dumpty\",len2)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "string=Bamboozled length=10\n",
- "\n",
- "string=Humpty Dumpty length=13\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 22
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:317-318"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#A look-alike of the function strlen()\n",
- "def xstrlen(s):\n",
- " length=0\n",
- " for item in s:\n",
- " if s!=\"\\0\":\n",
- " length+=1\n",
- " return length\n",
- "arr=\"Bamboozled\"\n",
- "len1=xstrlen(arr) \n",
- "len2=xstrlen(\"Humpty Dumpty\")\n",
- "print \"string=%s length=%d\\n\" % (arr,len1)\n",
- "print \"string=%s length=%d\\n\" % (\"Humpty dumpty\",len2)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "string=Bamboozled length=10\n",
- "\n",
- "string=Humpty dumpty length=13\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 23
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:319"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Copying one string into another\n",
- "import copy\n",
- "source=\"Sayonara\"\n",
- "target=copy.copy(source) #copy string\n",
- "print \"source string=%s\\n\" % (source)\n",
- "print \"target string=%s\\n\" % (target)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "source string=Sayonara\n",
- "\n",
- "target string=Sayonara\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 24
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:319-320"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#A look-alike of the function strcpy()\n",
- "def xstrcpy(t,s):\n",
- " t=\"\\0\"\n",
- " for item in s:\n",
- " t=t+str(item)\n",
- " return t\n",
- "source=\"Sayonara\"\n",
- "target=[]\n",
- "target=xstrcpy(target,source)\n",
- "print \"source string=%s\\n\" % (source)\n",
- "print \"target string=%s\\n\" % (target)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "source string=Sayonara\n",
- "\n",
- "target string=\u0000Sayonara\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 25
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:321"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#print area of circle\n",
- "pi=3.14\n",
- "print \"Enter radius of circle\"\n",
- "r=eval(raw_input())\n",
- "a=pi*r*r\n",
- "print \"Area of circle=%f\\n\" % (a)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter radius of circle\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "3\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Area of circle=28.260000\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 26
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:322"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Concatenating two strings\n",
- "source=\"Folks!\"\n",
- "target=\"Hello\"\n",
- "target=target+source\n",
- "print \"source string=%s\\n\" % (source)\n",
- "print \"target string=%s\\n\" % (target)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "source string=Folks!\n",
- "\n",
- "target string=HelloFolks!\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 27
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:322-323"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Comparing two strings\n",
- "string1=\"Jerry\"\n",
- "string2=\"Ferry\"\n",
- "i=cmp(string1,\"Jerry\")\n",
- "j=cmp(string1,string2)\n",
- "k=cmp(string1,\"Jerry boy\")\n",
- "print \"%d %d %d\\n\" % (i,j,k)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "0 1 -1\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 29
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:323-324"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Application of 2-D araay of characters\n",
- "def FOUND():\n",
- " return 1\n",
- "def NOTFOUND():\n",
- " return 0\n",
- "masterlist=[\"akshay\",\"parag\",\"raman\",\"srinivas\",\"gopal\",\"rajesh\"]\n",
- "print \"Enter your name\"\n",
- "yourname=raw_input()\n",
- "flag=NOTFOUND()\n",
- "for i in range(0,6,1):\n",
- " a=cmp(masterlist[i],yourname)\n",
- " if a==0:\n",
- " print \"Welcome,you can enter the palace\\n\"\n",
- " flag=FOUND()\n",
- " break\n",
- "if flag==NOTFOUND():\n",
- " print \"Sorry,you are a trespasser\\n\""
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter your name\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "gopal\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Welcome,you can enter the palace\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 51
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:327"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Exchange names using 2-D array of characters\n",
- "names=[\"akshay\",\"parag\",\"raman\",\"srinivas\",\"gopal\",\"rajesh\"]\n",
- "names = map(bytearray, names)\n",
- "print \"Original:%s %s\\n\" % (names[2],names[3])\n",
- "s = sorted((names[2], names[3]), key=len)\n",
- "for i in range(len(s[1])):\n",
- " try:\n",
- " t=s[0][i]\n",
- " s[0][i]=s[1][i]\n",
- " s[1][i]=t\n",
- " except IndexError:\n",
- " for _ in range(i, len(s[1])):\n",
- " #remove the items from the longer string to and append them to the shorter one.\n",
- " s[0].append(s[1].pop(i)) \n",
- "print \"New:%s %s\\n\" % (names[2],names[3])"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Original:raman srinivas\n",
- "\n",
- "New:srinivas raman\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 46
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE-328"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Exchange names using 2-D array of characters\n",
- "names=[\"akshay\",\"parag\",\"raman\",\"srinivas\",\"gopal\",\"rajesh\"]\n",
- "print \"Original:%s %s\\n\" % (names[2],names[3])\n",
- "t=names[2]\n",
- "names[2]=names[3]\n",
- "names[3]=t\n",
- "print \"New:%s %s\\n\" % (names[2],names[3])"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Original:raman srinivas\n",
- "\n",
- "New:srinivas raman\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 48
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:329"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Storing elements in an array\n",
- "names=[]\n",
- "for i in range(6):\n",
- " names.append(\"\\0\") \n",
- "for i in range(0,6,1):\n",
- " print \"Enter name\"\n",
- " names[i]=raw_input()"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter name\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "RAM\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter name\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "GOPAL\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter name\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "PANDEY\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter name\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "GOPU\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter name\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "CHOTU\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter name\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "RADHEY\n"
- ]
- }
- ],
- "prompt_number": 50
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "EXAMPLE ON PAGE:329-330"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Dynamic memory allocation of variables\n",
- "#No need of malloc func. in python as by default it's dynamic\n",
- "names=[]\n",
- "for i in range(0,6):\n",
- " names.append('\\0')\n",
- "for i in range(0,6,1):\n",
- " print \"Enter name\"\n",
- " n=raw_input()\n",
- " length=len(n)\n",
- " import copy\n",
- " p=copy.copy(n)\n",
- " names[i]=p\n",
- "for i in range(0,6,1):\n",
- " print \"%s\\n\" % (names[i])"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter name\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "MUKUT\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter name\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "BIHARI\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter name\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "PANDEY\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter name\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "MADHUBALA\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter name\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "PANDEY\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Enter name\n"
- ]
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "SUNITA\n"
- ]
- },
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "MUKUT\n",
- "\n",
- "BIHARI\n",
- "\n",
- "PANDEY\n",
- "\n",
- "MADHUBALA\n",
- "\n",
- "PANDEY\n",
- "\n",
- "SUNITA\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 7
- }
- ],
- "metadata": {}
- }
- ]
+{ + "metadata": { + "name": "", + "signature": "sha256:d543de519ea53ce295e62c624f3b779f3394a58f64540659aef438fffab60f98" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "CHAPTER 9: PUPPETTING ON STRINGS" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:311" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "name=\"Klinsman\"\n", + "i=0\n", + "while i<=7:\n", + " print \"%c\" % (name[i]), #method of printing without line feed\n", + " i+=1\n", + "print \"\\n\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "K l i n s m a n \n", + "\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:311-312" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "name=\"Klinsman \"\n", + "i=0\n", + "while name[i]!=' ':\n", + " print \"%c\" % (name[i]), \n", + " i+=1\n", + "print \"\\n\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " K l i n s m a n \n", + "\n" + ] + } + ], + "prompt_number": 14 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:312" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "name=\"Klinsman \"\n", + "ptr=[]\n", + "ptr=name #store base address of string\n", + "i=0\n", + "while name[i]!=' ':\n", + " print \"%c\" % (ptr[i]), \n", + " i+=1\n", + "print \"\\n\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "K l i n s m a n \n", + "\n" + ] + } + ], + "prompt_number": 18 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:313" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "name=\"Klinsman\"\n", + "print \"%s\" % (name)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Klinsman\n" + ] + } + ], + "prompt_number": 19 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:313" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "print \"Enter your name\"\n", + "name=raw_input()\n", + "print \"Hello %s!\\n\" % (name)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter your name\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Debashish\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Hello Debashish!\n", + "\n" + ] + } + ], + "prompt_number": 20 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:314" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "print \"Enter your full name:\"\n", + "name=raw_input()\n", + "print \"Hello!\"\n", + "print name" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter your full name:\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Debashish Roy\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Hello!\n", + "Debashish Roy\n" + ] + } + ], + "prompt_number": 21 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:315" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "str1=\"Hello\"\n", + "s=\"Good Morning\"\n", + "str2=str1 #No error in Python since all variables are by default pointers\n", + "q=s" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:316" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "str1=\"Hello\"\n", + "p=\"Hello\"\n", + "str1=\"Bye\" #No error in python\n", + "p=\"Bye\"" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:317" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "arr=\"Bamboozled\"\n", + "len1=len(arr) #return length of string\n", + "len2=len(\"Humpty Dumpty\")\n", + "print \"string=%s length=%d\\n\" % (arr,len1)\n", + "print \"string=%s length=%d\\n\" % (\"Humpty Dumpty\",len2)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "string=Bamboozled length=10\n", + "\n", + "string=Humpty Dumpty length=13\n", + "\n" + ] + } + ], + "prompt_number": 22 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:317-318" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def xstrlen(s):\n", + " length=0\n", + " for item in s:\n", + " if s!=\"\\0\":\n", + " length+=1\n", + " return length\n", + "arr=\"Bamboozled\"\n", + "len1=xstrlen(arr) \n", + "len2=xstrlen(\"Humpty Dumpty\")\n", + "print \"string=%s length=%d\\n\" % (arr,len1)\n", + "print \"string=%s length=%d\\n\" % (\"Humpty dumpty\",len2)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "string=Bamboozled length=10\n", + "\n", + "string=Humpty dumpty length=13\n", + "\n" + ] + } + ], + "prompt_number": 23 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:319" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "import copy\n", + "source=\"Sayonara\"\n", + "target=copy.copy(source) #copy string\n", + "print \"source string=%s\\n\" % (source)\n", + "print \"target string=%s\\n\" % (target)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "source string=Sayonara\n", + "\n", + "target string=Sayonara\n", + "\n" + ] + } + ], + "prompt_number": 24 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:319-320" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def xstrcpy(t,s):\n", + " t=\"\\0\"\n", + " for item in s:\n", + " t=t+str(item)\n", + " return t\n", + "source=\"Sayonara\"\n", + "target=[]\n", + "target=xstrcpy(target,source)\n", + "print \"source string=%s\\n\" % (source)\n", + "print \"target string=%s\\n\" % (target)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "source string=Sayonara\n", + "\n", + "target string=\u0000Sayonara\n", + "\n" + ] + } + ], + "prompt_number": 25 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:321" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "pi=3.14\n", + "print \"Enter radius of circle\"\n", + "r=eval(raw_input())\n", + "a=pi*r*r\n", + "print \"Area of circle=%f\\n\" % (a)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter radius of circle\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Area of circle=28.260000\n", + "\n" + ] + } + ], + "prompt_number": 26 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:322" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "source=\"Folks!\"\n", + "target=\"Hello\"\n", + "target=target+source\n", + "print \"source string=%s\\n\" % (source)\n", + "print \"target string=%s\\n\" % (target)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "source string=Folks!\n", + "\n", + "target string=HelloFolks!\n", + "\n" + ] + } + ], + "prompt_number": 27 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:322-323" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "string1=\"Jerry\"\n", + "string2=\"Ferry\"\n", + "i=cmp(string1,\"Jerry\")\n", + "j=cmp(string1,string2)\n", + "k=cmp(string1,\"Jerry boy\")\n", + "print \"%d %d %d\\n\" % (i,j,k)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "0 1 -1\n", + "\n" + ] + } + ], + "prompt_number": 29 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:323-324" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "def FOUND():\n", + " return 1\n", + "def NOTFOUND():\n", + " return 0\n", + "masterlist=[\"akshay\",\"parag\",\"raman\",\"srinivas\",\"gopal\",\"rajesh\"]\n", + "print \"Enter your name\"\n", + "yourname=raw_input()\n", + "flag=NOTFOUND()\n", + "for i in range(0,6,1):\n", + " a=cmp(masterlist[i],yourname)\n", + " if a==0:\n", + " print \"Welcome,you can enter the palace\\n\"\n", + " flag=FOUND()\n", + " break\n", + "if flag==NOTFOUND():\n", + " print \"Sorry,you are a trespasser\\n\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter your name\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "gopal\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Welcome,you can enter the palace\n", + "\n" + ] + } + ], + "prompt_number": 51 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:327" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "names=[\"akshay\",\"parag\",\"raman\",\"srinivas\",\"gopal\",\"rajesh\"]\n", + "names = map(bytearray, names)\n", + "print \"Original:%s %s\\n\" % (names[2],names[3])\n", + "s = sorted((names[2], names[3]), key=len)\n", + "for i in range(len(s[1])):\n", + " try:\n", + " t=s[0][i]\n", + " s[0][i]=s[1][i]\n", + " s[1][i]=t\n", + " except IndexError:\n", + " for _ in range(i, len(s[1])):\n", + " #remove the items from the longer string to and append them to the shorter one.\n", + " s[0].append(s[1].pop(i)) \n", + "print \"New:%s %s\\n\" % (names[2],names[3])" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Original:raman srinivas\n", + "\n", + "New:srinivas raman\n", + "\n" + ] + } + ], + "prompt_number": 46 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE-328" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "names=[\"akshay\",\"parag\",\"raman\",\"srinivas\",\"gopal\",\"rajesh\"]\n", + "print \"Original:%s %s\\n\" % (names[2],names[3])\n", + "t=names[2]\n", + "names[2]=names[3]\n", + "names[3]=t\n", + "print \"New:%s %s\\n\" % (names[2],names[3])" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Original:raman srinivas\n", + "\n", + "New:srinivas raman\n", + "\n" + ] + } + ], + "prompt_number": 48 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:329" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "names=[]\n", + "for i in range(6):\n", + " names.append(\"\\0\") \n", + "for i in range(0,6,1):\n", + " print \"Enter name\"\n", + " names[i]=raw_input()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter name\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "RAM\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter name\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "GOPAL\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter name\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "PANDEY\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter name\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "GOPU\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter name\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "CHOTU\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter name\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "RADHEY\n" + ] + } + ], + "prompt_number": 50 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "EXAMPLE ON PAGE:329-330" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + " \n", + "names=[]\n", + "for i in range(0,6):\n", + " names.append('\\0')\n", + "for i in range(0,6,1):\n", + " print \"Enter name\"\n", + " n=raw_input()\n", + " length=len(n)\n", + " import copy\n", + " p=copy.copy(n)\n", + " names[i]=p\n", + "for i in range(0,6,1):\n", + " print \"%s\\n\" % (names[i])" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter name\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "MUKUT\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter name\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "BIHARI\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter name\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "PANDEY\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter name\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "MADHUBALA\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter name\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "PANDEY\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter name\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "SUNITA\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "MUKUT\n", + "\n", + "BIHARI\n", + "\n", + "PANDEY\n", + "\n", + "MADHUBALA\n", + "\n", + "PANDEY\n", + "\n", + "SUNITA\n", + "\n" + ] + } + ], + "prompt_number": 7 + } + ], + "metadata": {} + } + ] }
\ No newline at end of file |