{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 11: Pointers" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 1, Page Number : PTR-5" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = int(raw_input(\"\\nEnter two integers : \"))\n", "b = int(raw_input(\"\"))\n", "\n", "xptr = id(a)\n", "yptr = id(b)\n", "\n", "#There is no pointer concept in python\n", "s = a + b\n", "\n", "print \"\\nSum = \",s" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter two integers : 5\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "3\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Sum = 8\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 2, Page Number : PTR-6" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = int(raw_input(\"\\nEnter two integers : \"))\n", "b = int(raw_input(\"\"))\n", "\n", "xptr = id(a)\n", "yptr = id(b)\n", "\n", "#There is no pointer concept in python\n", "if a > b:\n", " big = a\n", "else:\n", " big = b\n", " \n", "print \"\\nBiggest number is \",big" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter two integers : 15\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "25\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Biggest number is 25\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 3, Page Number : PTR-7" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def add10(x,y):\n", " #There is no pointer concept in python\n", " x = x + 10\n", " y = y + 10\n", " print \"\\n\\nInside the function a = \",x,\" b = \",y\n", " return x,y\n", " \n", "a = 25\n", "b = 10\n", "\n", "print \"\\nBefore function call a = \",a,\" b = \",b\n", "a,b = add10(a,b)\n", "print \"\\nAfter function call a = \",a,\" b = \",b" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Before function call a = 25 b = 10\n", "\n", "\n", "Inside the function a = 35 b = 20\n", "\n", "After function call a = 35 b = 20\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4, Page Number : PTR-11" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def swap(x,y):\n", " #There is no pointer concept in python\n", " temp = x\n", " x = y\n", " y = temp\n", " return x,y\n", "\n", "a = int(raw_input(\"\\nEnter two integers : \"))\n", "b = int(raw_input(\"\"))\n", "\n", "print \"\\nBefore function call a = \",a,\" b = \",b\n", "\n", "a,b = swap(a,b)\n", "\n", "print \"\\n\\nAfter function call, values are exchanged as \\n a = \",a,\" b = \",b" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter two integers : 25\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "500\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Before function call a = 25 b = 500\n", "\n", "\n", "After function call, values are exchanged as \n", " a = 500 b = 25\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 5, Page Number : PTR-14" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import sys\n", "\n", "x = [0.0 for i in range(0,10)]\n", "n = int(raw_input(\"\\nHow many values ? \"))\n", "\n", "print \"\\nEnter all values in the list\\n\"\n", "\n", "for i in range(0,n):\n", " x[i] = float(raw_input(\"\"))\n", "\n", "sum1 = 0.0\n", "#Since sum is a predefined function in python, sum1 is used\n", "for i in range(0,n):\n", " sum1 = sum1 + x[i]\n", " #There is no pointer concept in python\n", " \n", "mean = sum1/n\n", "\n", "sys.stdout.write(\"\\nArithmetic mean = %0.3f\"%(mean))" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "How many values ? 4\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter all values in the list\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "3.48\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "3.54\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "3.26\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "2.98\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Arithmetic mean = 3.315" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6, Page Number : PTR-19" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import sys\n", "\n", "xp = [0 for i in range(0,10)]\n", "n = int(raw_input(\"\\nHow many values ? \"))\n", "\n", "#There is no pointer concept in python\n", "print \"\\nEnter all values\\n\"\n", "\n", "for i in range(0,n):\n", " xp[i] = int(raw_input(\"\"))\n", "\n", "print \"\\nThe reversed list is\\n\"\n", "for i in range(n-1,0-1,-1):\n", " sys.stdout.write(\"%6d\"%(xp[i]))" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "How many values ? 6\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter all values\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "11\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "12\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "13\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "14\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "15\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "16\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "The reversed list is\n", "\n", " 16 15 14 13 12 11" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 7, Page Number : PTR-21" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import sys\n", "\n", "a = [[0 for i in range(0,10)]for i in range(0,10)]\n", "b = [[0 for i in range(0,10)]for i in range(0,10)]\n", "\n", "c = [[0 for i in range(0,5)]for i in range(0,5)]\n", "m = int(raw_input(\"\\nFor A matrix \\nHow many rows and columns ? \"))\n", "n = int(raw_input(\"\"))\n", "\n", "print \"\\nEnter A matrix values\\n\"\n", "for i in range(0,m):\n", " for j in range(0,n):\n", " a[i][j] = int(raw_input(\"\"))\n", " \n", "n = int(raw_input(\"\\nFor B matrix\\nHow many rows and columns ? \"))\n", "l = int(raw_input(\"\"))\n", "\n", "for i in range(0,n):\n", " for j in range(0,l):\n", " b[i][j] = int(raw_input(\"\"))\n", "\n", "for i in range(0,m):\n", " for j in range(0,l):\n", " c[i][j] = 0\n", " for k in range(0,l):\n", " c[i][j] = c[i][j] + (a[i][k] * b[k][j])\n", " \n", "print \"\\nResultant matrix is \\n\"\n", "for i in range(0,m):\n", " for j in range(0,l):\n", " sys.stdout.write(\"%6d\"%(c[i][j]))\n", " sys.stdout.write(\"\\n\")" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "For A matrix \n", "How many rows and columns ? 3\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "3\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter A matrix values\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "3\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "2\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "1\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "-2\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "0\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "4\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "3\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "2\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "-1\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "For B matrix\n", "How many rows and columns ? 3\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "3\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "6\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "4\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "-1\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "2\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "1\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "2\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "4\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "-5\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "4\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Resultant matrix is \n", "\n", " 26 9 5\n", " 4 -28 18\n", " 18 19 -3\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 8, Page Number : PTR-24" ] }, { "cell_type": "code", "collapsed": false, "input": [ "count = 0\n", "\n", "st = raw_input(\"\\nEnter a string : \")\n", "sptr = st\n", "i = 0\n", "\n", "while i < len(st):\n", " if st[i].upper() == 'A' or st[i].upper() == 'E' or st[i].upper() == 'I' or st[i].upper() == 'O' or st[i].upper() == 'U':\n", " count = count + 1\n", " i = i + 1\n", " \n", "print \"\\nNumber of vowels = \",count" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter a string : GOOD LUCK to all\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Number of vowels = 5\n" ] } ], "prompt_number": 32 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 9, Page Number : PTR-25" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def sort_names(sn,n):\n", " for i in range(0,n):\n", " for j in range(i+1,n):\n", " if sn[i] > sn[j]:\n", " temp = sn[i]\n", " sn[i] = sn[j]\n", " sn[j] = temp\n", " \n", "\n", "n = int(raw_input(\"\\nHow many names ?\"))\n", "\n", "snames = [\"\" for i in range(0,10)]\n", "\n", "print \"\\nEnter all names \\n\"\n", "for i in range(0,n):\n", " snames[i] = raw_input(\"\")\n", " \n", "sort_names(snames,n)\n", "\n", "print \"\\nThe name list in alphabetical order\"\n", "for i in range(0,n):\n", " print snames[i]" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "How many names ?4\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter all names \n", "\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "DEEPAK\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "SHERIN\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "SONIKA\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "ARUN\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "The name list in alphabetical order\n", "ARUN\n", "DEEPAK\n", "SHERIN\n", "SONIKA\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 10, Page Number : PTR-27" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"\\nEnter a string : \"\n", "st = raw_input(\"\")\n", "rst = ['' for i in range(0,80)]\n", "\n", "rst = st[::-1]\n", " \n", "if st == rst:\n", " print st,\" is a palindrome string\"\n", "else:\n", " print st,\" is not a palindrome string\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter a string : \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "LIRIL\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "LIRIL is a palindrome string\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 11, Page Number : PTR-28" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"\\nEnter a string : \"\n", "st = raw_input(\"\")\n", "\n", "#There is no pointer concept in python\n", "city = st\n", "\n", "print \"\\nThe copied string : \",city" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter a string : \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "NEW DELHI\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "The copied string : NEW DELHI\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 12, Page Number : PTR-30" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#class is used instead of structure in python\n", "\n", "class student():\n", " rno = 0\n", " sname = ''\n", " tot = 0\n", "\n", "sptr = student()\n", "sptr.rno = int(raw_input(\"\\nEnter register No.:\"))\n", "sptr.sname = raw_input(\"\\nEnter student name : \")\n", "sptr.tot = int(raw_input(\"\\nEnter total marks : \"))\n", "\n", "print \"\\n\\tDetails entered are \\n\"\n", "print \"\\nReg.No, : \",sptr.rno\n", "print \"\\nName : \",sptr.sname\n", "print \"\\nTotal : \",sptr.tot\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter register No.:101\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter student name : Ashwin\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter total marks : 89\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "\tDetails entered are \n", "\n", "\n", "Reg.No, : 101\n", "\n", "Name : Ashwin\n", "\n", "Total : 89\n" ] } ], "prompt_number": 9 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 13, Page Number : PTR-32" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#There is no pointer concept in python\n", "def add(x,y):\n", " return x+y\n", "\n", "def multiply(x,y):\n", " return x*y\n", "\n", "a = int(raw_input(\"\\nEnter two integers : \"))\n", "b = int(raw_input(\"\"))\n", "\n", "sum1 = add(a,b)\n", "\n", "print \"Sum a + b = \",sum1\n", "\n", "product = multiply(a,b)\n", "print \"\\nProduct a * b = \",product" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Enter two integers : 5\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "3\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Sum a + b = 8\n", "\n", "Product a * b = 15\n" ] } ], "prompt_number": 10 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }