"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Print size of various data types using arrays.\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"i = [0 for i in range(0,10)]\n",
"c = ['0' for i in range(0,10)]\n",
"l = [sys.maxint+5 for i in range(0,10)]\n",
"\n",
"#Result\n",
"sys.stdout.write(\"The type 'int' requires %d Bytes\"%(sys.getsizeof(int)))\n",
"sys.stdout.write(\"\\nThe type 'char' requires %d Bytes\"%(sys.getsizeof(str)))\n",
"sys.stdout.write(\"\\nThe type 'long' requires %d Bytes\"%(sys.getsizeof(long)))\n",
"sys.stdout.write(\"\\n%d memory locations are reserved for ten 'int' elements\"%(sys.getsizeof(i)))\n",
"sys.stdout.write(\"\\n%d memory locations are reserved for ten 'int' elements\"%(sys.getsizeof(c)))\n",
"sys.stdout.write(\"\\n%d memory locations are reserved for ten 'int' elements\"%(sys.getsizeof(l)))\n",
"\n",
"#python data types are different from C"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"The type 'int' requires 436 Bytes\n",
"The type 'char' requires 436 Bytes\n",
"The type 'long' requires 436 Bytes\n",
"12 memory locations are reserved for ten 'int' elements\n",
"100 memory locations are reserved for ten 'int' elements\n",
"100 memory locations are reserved for ten 'int' elements"
]
}
],
"prompt_number": 40
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Find sum of even and odd numbers from 1 to 10. \n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"sumo = 0\n",
"sume = 0\n",
"i = 0\n",
"a = -1\n",
"b = -1\n",
"\n",
"odd = [0 for i in range(0,5)]\n",
"even = [0 for i in range(0,5)]\n",
"\n",
"#Calculation\n",
"for i in range(1,10+1):\n",
" if i%2 == 0:\n",
" a += 1\n",
" even[a] = i\n",
" else:\n",
" b += 1\n",
" odd[b] = i\n",
" \n",
"#Result\n",
"sys.stdout.write(\"\\n\\tEven \\t\\tOdd\")\n",
"for i in range(0,5):\n",
" sys.stdout.write(\"\\n\\t %d\\t\\t %d\"%(even[i],odd[i]))\n",
" sume = sume + even[i]\n",
" sumo = sumo + odd[i]\n",
"\n",
"sys.stdout.write(\"\\n\\t==========================\\n\")\n",
"sys.stdout.write(\"Addition : %d %14d\"%(sume,sumo))\n",
" "
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\tEven \t\tOdd\n",
"\t 2\t\t 1\n",
"\t 4\t\t 3\n",
"\t 6\t\t 5\n",
"\t 8\t\t 7\n",
"\t 10\t\t 9\n",
"\t==========================\n",
"Addition : 30 25"
]
}
],
"prompt_number": 6
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 7.4, Page number: 189
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Find sum of even numbers and product of odd numbers from 5 input values.\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"a = 0\n",
"m = 1\n",
"num = [0 for i in range(0,5)]\n",
"\n",
"for i in range(0,5):\n",
" sys.stdout.write(\"\\nEnter Number [%d] : \"%(i+1))\n",
" num[i] = int(raw_input(\"\"))\n",
"\n",
"#Calculation and Result\n",
"sys.stdout.write(\"\\n==================================\")\n",
"for i in range(0,5):\n",
" if num[i]%2 == 0:\n",
" sys.stdout.write(\"\\nEven Number : %d\"%(num[i]))\n",
" a = a + num[i]\n",
" else:\n",
" sys.stdout.write(\"\\nOdd Number : %d\"%(num[i]))\n",
" m = m * num[i]\n",
"\n",
"sys.stdout.write(\"\\n==================================\")\n",
"sys.stdout.write(\"\\nAddition of Even Numbers : %d\"%(a))\n",
"sys.stdout.write(\"\\nProduct of Odd Numbers : %d\"%(m))\n",
"sys.stdout.write(\"\\n==================================\")"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Enter Number [1] : "
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"1\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Enter Number [2] : "
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"2\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Enter Number [3] : "
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"3\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Enter Number [4] : "
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"4\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Enter Number [5] : "
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"5\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"==================================\n",
"Odd Number : 1\n",
"Even Number : 2\n",
"Odd Number : 3\n",
"Even Number : 4\n",
"Odd Number : 5\n",
"==================================\n",
"Addition of Even Numbers : 6\n",
"Product of Odd Numbers : 15\n",
"=================================="
]
}
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 7.5, Page number: 190
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Print string in the reverse order.\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"s = ['0' for i in range(0,15)]\n",
"\n",
"s = raw_input(\"Enter a String : \\n\")\n",
"\n",
"#Result\n",
"sys.stdout.write(\"Reverse String : \\n\")\n",
"\n",
"#Since there is no null terminating character for string in python, length of string is taken to give the index range\n",
"#index range is given -1 to include the index 0 also.\n",
"\n",
"for i in range(len(s)-1,-1,-1): \n",
" sys.stdout.write(\"%c\"%s[i])"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter a String : \n",
"HELLO\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Reverse String : \n",
"OLLEH"
]
}
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 7.6, Page number: 191
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Detect the occurrence of a character in a given string.\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"s = ['0' for i in range(0,15)]\n",
"c = 0\n",
"\n",
"s = raw_input(\"Enter a String : \")\n",
"f = raw_input(\"Enter a Character to Find :\")\n",
"\n",
"#Calculation\n",
"for i in range(0,len(s)):\n",
" if s[i] == f:\n",
" c += 1\n",
" \n",
"#Result\n",
"sys.stdout.write(\"The Character (%c) in a String (%s) occurs %d times.\"%(f,s,c))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter a String : programmer\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter a Character to Find :r\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"The Character (r) in a String (programmer) occurs 3 times."
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 7.7, Page number: 192
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Display the elements of two arrays in two separate columns and add their\n",
"#corresponding elements and display in the 3rd column.\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"num = [24,34,12,44,56,17]\n",
"num1 = [12,24,35,78,85,22]\n",
"\n",
"#Result\n",
"sys.stdout.write(\"Element of Array 1st - 2nd Array Addition\\n\")\n",
"for i in range(0,5+1):\n",
" sys.stdout.write(\"\\n\\t\\t%d + %d = %d\"%(num[i],num1[i],num[i]+num1[i]))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Element of Array 1st - 2nd Array Addition\n",
"\n",
"\t\t24 + 12 = 36\n",
"\t\t34 + 24 = 58\n",
"\t\t12 + 35 = 47\n",
"\t\t44 + 78 = 122\n",
"\t\t56 + 85 = 141\n",
"\t\t17 + 22 = 39"
]
}
],
"prompt_number": 27
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 7.8, Page number: 192
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Find sum of 3 numbers by entering as character type.\n",
"\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"real = [['0' for i in range(0,6)] for i in range(0,6)]\n",
"ima = [['0' for i in range(0,6)] for i in range(0,6)]\n",
"\n",
"for l in range(0,3):\n",
" c = int(raw_input(\"Enter Number : \"))\n",
" r = c/255\n",
" i = c%255\n",
" real[l][5] = r\n",
" ima[l][5] = i\n",
"\n",
"c = 0\n",
"\n",
"for l in range(0,3):\n",
" c = c + real[l][5]*255 + ima[l][5]\n",
"\n",
"sys.stdout.write(\"\\nSum of 3 Numbers : %3ld\"%(c))\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Number : 5\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Number : 4\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Number : 3\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Sum of 3 Numbers : 12"
]
}
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 7.9, Page number: 193
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#Accept any string upto 15 characters. Display the elements of string with their\n",
"#element numbers in a separate column.\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"#name = ['0' for i in range(0,15)]\n",
"name = raw_input(\"Enter your name : \")\n",
"\n",
"#Result\n",
"sys.stdout.write(\"Element no. & Character\\n\")\n",
"for i in range(0,len(name)): #There is no null termination character for string in python\n",
" sys.stdout.write(\"\\n%d \\t %c\"%(i,name[i])) \n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter your name : amit\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Element no. & Character\n",
"\n",
"0 \t a\n",
"1 \t m\n",
"2 \t i\n",
"3 \t t"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 7.10, Page number: 194
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Display names of days of a week \n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"day = [0 for i in range(0,7)]\n",
"\n",
"sys.stdout.write(\"Enter numbers between 1 to 7 : \")\n",
"for i in range(0,6+1):\n",
" day[i] = int(raw_input(\"\"))\n",
"\n",
"#Result\n",
"#There is no switch statement in python. use functions and dictionary or if statement instead.\n",
"for i in range(0,6+1):\n",
" if day[i] == 1:\n",
" sys.stdout.write(\"\\n%dst day of week is Sunday\"%(day[i]))\n",
" else:\n",
" if day[i] == 2:\n",
" sys.stdout.write(\"\\n%dnd day of week is Monday\"%(day[i]))\n",
" else:\n",
" if day[i] == 3:\n",
" sys.stdout.write(\"\\n%drd day of week is Tuesday\"%(day[i]))\n",
" else:\n",
" if day[i] == 4:\n",
" sys.stdout.write(\"\\n%dth day of week is Wednesday\"%(day[i]))\n",
" else:\n",
" if day[i] == 5:\n",
" sys.stdout.write(\"\\n%dth day of week is Thursday\"%(day[i]))\n",
" else:\n",
" if day[i] == 6:\n",
" sys.stdout.write(\"\\n%dth day of week is Friday\"%(day[i]))\n",
" else:\n",
" if day[i] == 7:\n",
" sys.stdout.write(\"\\n%dth day of week is Saturday\"%(day[i]))\n",
" else:\n",
" sys.stdout.write(\"\\n%dth is invalid day.\"%(day[i]))\n",
" "
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter numbers between 1 to 7 : "
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"1\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": [
"4\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"5\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"7\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"8\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"1st day of week is Sunday\n",
"3rd day of week is Tuesday\n",
"2nd day of week is Monday\n",
"4th day of week is Wednesday\n",
"5th day of week is Thursday\n",
"7th day of week is Saturday\n",
"8th is invalid day."
]
}
],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 7.11, Page number: 195
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Display the contents of two arrays where the 1st array should contain the string and 2nd numerical numbers.\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"city = ['N','A','N','D','E','D']\n",
"pin = [4,3,1,6,0,3]\n",
"\n",
"#Result\n",
"for i in city:\n",
" sys.stdout.write(\"%c\"%(i))\n",
"\n",
"sys.stdout.write(\" - \")\n",
"for i in pin:\n",
" sys.stdout.write(\"%d\"%(i))\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"NANDED - 431603"
]
}
],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 7.12, Page number: 195
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Display the number of days of different months of year.\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"month = [31,28,31,30,31,30,31,31,30,31,30,31]\n",
"\n",
"#Result\n",
"for i in range(0,11+1):\n",
" sys.stdout.write(\"\\nMonth [%d] of a year contains %d days.\"%(i+1,month[i]))\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Month [1] of a year contains 31 days.\n",
"Month [2] of a year contains 28 days.\n",
"Month [3] of a year contains 31 days.\n",
"Month [4] of a year contains 30 days.\n",
"Month [5] of a year contains 31 days.\n",
"Month [6] of a year contains 30 days.\n",
"Month [7] of a year contains 31 days.\n",
"Month [8] of a year contains 31 days.\n",
"Month [9] of a year contains 30 days.\n",
"Month [10] of a year contains 31 days.\n",
"Month [11] of a year contains 30 days.\n",
"Month [12] of a year contains 31 days."
]
}
],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 7.13, Page number: 197
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Display the number of days of given month of a year.\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"month = [1,3,5,7,8,10,12,4,6,9,11,2]\n",
"\n",
"mn = int(raw_input(\"Enter Number of Month : \"))\n",
"\n",
"for i in range(0,11+1):\n",
" if mn == month[i]: #There is no goto statement in python\n",
" if i+1 == 12:\n",
" sys.stdout.write(\"Month (%d) Contains 28 days.\"%(month[i]))\n",
" if i+1 < 8:\n",
" sys.stdout.write(\"Month (%d) Contains 31 days.\"%(month[i]))\n",
" if i+1 > 7 and i+1 != 12:\n",
" sys.stdout.write(\"Month (%d) Contains 30 days.\"%(month[i]))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Number of Month : 2\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Month (2) Contains 28 days."
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 7.14, Page number: 198
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Find the average sales of an item out of 12 months sale.\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"#Since sum is a built in function in python, sum1 is used instead of sum.\n",
"sum1 = 0\n",
"avg = 0\n",
"\n",
"sys.stdout.write(\"Enter Month No.- Sale of an Item/month\\n\")\n",
"\n",
"item = [0 for i in range(0,11+1)]\n",
"for sale in range(0,11+1):\n",
" item[sale] = int(raw_input(\"\\t%d = \"%(sale+1)))\n",
"\n",
"#Calculation\n",
"for sale in range(0,11+1):\n",
" sum1 = sum1 + item[sale]\n",
"\n",
"avg = sum1/12.0\n",
"\n",
"#Result\n",
"sys.stdout.write(\"\\nAverage Sale if an item/month = %f\"%(avg))\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Month No.- Sale of an Item/month\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"\t1 = 125\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"\t2 = 225\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"\t3 = 325\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"\t4 = 425\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"\t5 = 525\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"\t6 = 625\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"\t7 = 725\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"\t8 = 825\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"\t9 = 925\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"\t10 = 500\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"\t11 = 600\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"\t12 = 700\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Average Sale if an item/month = 543.750000"
]
}
],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 7.15, Page number: 199
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Find the similar elements in an array and find the occurrence of similar\n",
"#numbers for number of times.\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"k = 0\n",
"j = 1\n",
"\n",
"item = [0 for i in range(0,4+1)]\n",
"sys.stdout.write(\"\\nEnter Numbers.\\n\")\n",
"for i in range(0,4+1):\n",
" item[i] = int(raw_input(\"%d = \"%(i)))\n",
"\n",
"#Calculation & Result\n",
"for j in range(0,4+1):\n",
" for i in range(j+1,4+1):\n",
" if item[j] == item[i]:\n",
" sys.stdout.write(\"\\n%d %d Elements are same.\"%(j,i))\n",
" k += 1\n",
"\n",
"if k == 10:\n",
" sys.stdout.write(\"\\nAll elements are same\")\n",
"else:\n",
" if k == 0:\n",
" sys.stdout.write(\"\\nNo Elements are same.\")\n",
" else:\n",
" sys.stdout.write(\"\\nElements Contains Double Numbers.\")"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Enter Numbers.\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"0 = 5\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"1 = 8\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"2 = 3\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"3 = 5\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"4 = 9\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"0 3 Elements are same.\n",
"Elements Contains Double Numbers."
]
}
],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 7.16, Page number: 200
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Calculate and display total cost of 4 models of Pentium PCs.\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"pccode = [1,2,3,4]\n",
"t = 0\n",
"price = [25000,30000,35000,40000]\n",
"stock = [25,20,15,20]\n",
"\n",
"#Result\n",
"sys.stdout.write(\"\\nStock & Total Cost Details\")\n",
"sys.stdout.write(\"\\n========================================================\")\n",
"sys.stdout.write(\"\\nModel\\t\\tQty.\\tRate(Rs.)\\tTotal Value\")\n",
"sys.stdout.write(\"\\n========================================================\")\n",
"\n",
"for i in range(0,3+1):\n",
" sys.stdout.write(\"\\nPentium%d\\t%d\\t%8ld %15ld\"%(pccode[i],stock[i],price[i],price[i]*stock[i]))\n",
" t = t + price[i] * stock[i]\n",
"\n",
"sys.stdout.write(\"\\n========================================================\")\n",
"sys.stdout.write(\"\\nTotal Value of All PCs in Rs. %ld\"%(t))\n",
"sys.stdout.write(\"\\n========================================================\")"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Stock & Total Cost Details\n",
"========================================================\n",
"Model\t\tQty.\tRate(Rs.)\tTotal Value\n",
"========================================================\n",
"Pentium1\t25\t 25000 625000\n",
"Pentium2\t20\t 30000 600000\n",
"Pentium3\t15\t 35000 525000\n",
"Pentium4\t20\t 40000 800000\n",
"========================================================\n",
"Total Value of All PCs in Rs. 2550000\n",
"========================================================"
]
}
],
"prompt_number": 18
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 7.17, Page number: 201
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Display the given message by using putc() and stdout() functions.\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"msg = \"C is Easy\"\n",
"i = 0\n",
"\n",
"#Result\n",
"#There is no putc function and null termination in python.\n",
"while i < len(msg):\n",
" sys.stdout.write(\"%c\"%(msg[i]))\n",
" i += 1"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"C is Easy"
]
}
],
"prompt_number": 21
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 7.18, Page number: 202
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Read the text through keyboard and display it by using\n",
"#stdin and stdout streams.\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"ch = raw_input(\"Input a Text : \")\n",
"\n",
"#Result\n",
"sys.stdout.write(\"The Text Entered was\\n\")\n",
"\n",
"for i in ch:\n",
" sys.stdout.write(\"%c\"%(i))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Input a Text : Hello World\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"The Text Entered was\n",
"Hello World"
]
}
],
"prompt_number": 6
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 7.19, Page number: 202
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Sort the given strings alphabetically.\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"text = raw_input(\"Enter Text Below : \")\n",
"\n",
"#Result\n",
"sys.stdout.write(\"Sorted Text Below : \\n\")\n",
"for i in range(65,90+1):\n",
" for j in range(0,len(text)):\n",
" if text[j] == chr(i).upper() or text[j] == chr(i).lower():\n",
" sys.stdout.write(\"%c\"%(text[j]))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Text Below : Hello\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Sorted Text Below : \n",
"eHllo"
]
}
],
"prompt_number": 7
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 7.20, Page number: 203
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Arrange the numbers in increasing and decreasing order\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"sys.stdout.write(\"Enter ten numbers : \")\n",
"a = [0 for i in range(0,10)]\n",
"\n",
"for i in range(0,10):\n",
" a[i] = int(raw_input(\"\"))\n",
"sum1 = 0\n",
"\n",
"#Calculation\n",
"sum1 = sum(a)\n",
"\n",
"#Result\n",
"sys.stdout.write(\"Numbers In Ascending Order : \")\n",
"for i in range(0,sum1):\n",
" for j in range(0,10):\n",
" if i == a[j]:\n",
" sys.stdout.write(\"%3d\"%(a[j]))\n",
" \n",
"sys.stdout.write(\"\\nNumbers In Descending Order : \")\n",
"for i in range(sum1,0,-1):\n",
" for j in range(0,10):\n",
" if i == a[j]:\n",
" sys.stdout.write(\"%3d\"%(a[j]))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter ten numbers : "
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"5\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": [
"4\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"7\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"9\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"10\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"12\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"9\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"3\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Numbers In Ascending Order : 1 2 3 4 5 7 9 9 10 12\n",
"Numbers In Descending Order : 12 10 9 9 7 5 4 3 2 1"
]
}
],
"prompt_number": 8
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 7.21, Page number: 205
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Sorting in ascending order by comparison method.\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"\n",
"n = int(raw_input(\"Enter how many numbers to sort : \"))\n",
"\n",
"num = [0 for i in range(0,n)]\n",
"for i in range(0,n):\n",
" num[i] = int(raw_input(\"Enter numbers # %2d : \"%(i+1)))\n",
"\n",
"#Result\n",
"sys.stdout.write(\"The Numbers Entered through keyboard\\n\")\n",
"for i in range(0,n):\n",
" sys.stdout.write(\"%4d\"%(num[i]))\n",
"for i in range(0,n-1):\n",
" for j in range(i+1,n):\n",
" if num[i] > num[j]:\n",
" temp = num[i]\n",
" num[i] = num[j]\n",
" num[j] = temp\n",
"\n",
"sys.stdout.write(\"\\nThe Numbers in ascending order\\n\")\n",
"for i in num:\n",
" sys.stdout.write(\"%4d\"%(i))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter how many numbers to sort : 5\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter numbers # 1 : 8\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter numbers # 2 : 3\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter numbers # 3 : 2\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter numbers # 4 : 1\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter numbers # 5 : 9\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"The Numbers Entered through keyboard\n",
" 8 3 2 1 9\n",
"The Numbers in ascending order\n",
" 1 2 3 8 9"
]
}
],
"prompt_number": 9
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 7.22, Page number: 206
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Evaluate the series contains sum of square of\n",
"#numbers from 1 to n. \n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"s = 0\n",
"sqr = [0 for i in range(0,15)]\n",
"\n",
"n = int(raw_input(\"Enter value of n : \"))\n",
"\n",
"for i in range(0,n):\n",
" sqr[i] = pow(i+1,2)\n",
" \n",
"for i in range(0,n):\n",
" sys.stdout.write(\"%d\\n\"%(sqr[i]))\n",
" s = s + sqr[i]\n",
" \n",
"sys.stdout.write(\"Sum of square : %d\"%(s))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter value of n : 4\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"1\n",
"4\n",
"9\n",
"16\n",
"Sum of square : 30"
]
}
],
"prompt_number": 10
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 7.23, Page number: 207
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Display two dimensional array elements together with their addresses\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"a = [[0 for i in range(0,3)]for i in range(0,3)]\n",
"c = 1\n",
"\n",
"for i in range(0,3):\n",
" for j in range(0,3):\n",
" a[i][j] = c\n",
" c += 1\n",
" \n",
"#Result\n",
"sys.stdout.write(\"Array Elements and addresses.\\n\")\n",
"sys.stdout.write(\" Col-0 Col-1 Col-2\\n\")\n",
"sys.stdout.write(\"========================================================\")\n",
"sys.stdout.write(\"\\nRow0\")\n",
"\n",
"for i in range(0,3):\n",
" for j in range(0,3):\n",
" sys.stdout.write(\" %d [%5d]\"%(a[i][j],id(a[i][j])))\n",
" sys.stdout.write(\"\\nRow%d\"%(i+1))\n",
"\n",
"sys.stdout.write(\"\\r\")"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Array Elements and addresses.\n",
" Col-0 Col-1 Col-2\n",
"========================================================\n",
"Row0 1 [6215912] 2 [6215900] 3 [6215888]\n",
"Row1 4 [6215876] 5 [6215864] 6 [6215852]\n",
"Row2 7 [6215840] 8 [6215828] 9 [6215816]\n",
"Row3\r"
]
}
],
"prompt_number": 16
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 7.24, Page number: 208
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Display the elements of two-dimensional array.\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"a = [[1,2,3],[4,5,6],[7,8,9]]\n",
" \n",
"#Result\n",
"sys.stdout.write(\"Elements of An Array.\\n\")\n",
"for i in range(0,3):\n",
" for j in range(0,3):\n",
" sys.stdout.write(\"%5d\"%(a[i][j]))\n",
" sys.stdout.write(\"\\n\")"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Elements of An Array.\n",
" 1 2 3\n",
" 4 5 6\n",
" 7 8 9\n"
]
}
],
"prompt_number": 36
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 7.25, Page number: 209
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Read codenos and balance and count the number of codenos, \n",
"#which are having the balance less than 1000.\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"j = 0\n",
"bal = [[0 for i in range(0,5)]for i in range(0,5)]\n",
"\n",
"for i in range(0,5):\n",
" bal[i][1] = int(raw_input(\"Enter Code No & Balance : \"))\n",
" bal[i][2] = int(raw_input(\"Enter Code No & Balance : \"))\n",
" \n",
"sys.stdout.write(\"Your Entered Data : \")\n",
"for i in range(0,5):\n",
" sys.stdout.write(\"\\n%d %d\"%(bal[i][1],bal[i][2]))\n",
" if bal[i][2] < 1000:\n",
" j += 1\n",
" \n",
"sys.stdout.write(\"\\nBalance Less than 1000 are %d\"%(j))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Code No & Balance : 1\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Code No & Balance : 900\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Code No & Balance : 2\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Code No & Balance : 800\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Code No & Balance : 3\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Code No & Balance : 1200\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Code No & Balance : 4\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Code No & Balance : 550\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Code No & Balance : 5\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Code No & Balance : 600\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Your Entered Data : \n",
"1 900\n",
"2 800\n",
"3 1200\n",
"4 550\n",
"5 600\n",
"Balance Less than 1000 are 4"
]
}
],
"prompt_number": 11
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 7.26, Page number: 210
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Accept three elements in a one dimensional array and compute addition,square\n",
"#and cube. Display the results in two dimensional array.\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"a = [[0 for i in range(0,3)]for i in range(0,3)]\n",
"b = [0 for i in range(0,3)]\n",
"j = 0\n",
"\n",
"for i in range(0,3):\n",
" b[i] = int(raw_input(\"Enter Three Numbers : \"))\n",
"\n",
"#Calculation\n",
"for i in range(0,3):\n",
" a[i][j] = b[i]*2\n",
" a[i][j+1] = pow(b[i],2)\n",
" a[i][j+2] = pow(b[i],3)\n",
" \n",
"#Result\n",
"sys.stdout.write(\"Number\\tAddition\\tSquare\\t\\tCube\\n\")\n",
"for i in range(0,3):\n",
" sys.stdout.write(\"\\n%d\"%(b[i]))\n",
" for j in range(0,3):\n",
" sys.stdout.write(\"\\t%4d\\t\"%(a[i][j]))\n",
" sys.stdout.write(\"\\n\")\n",
" "
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Three Numbers : 5\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Three Numbers : 6\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Three Numbers : 7\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Number\tAddition\tSquare\t\tCube\n",
"\n",
"5\t 10\t\t 25\t\t 125\t\n",
"\n",
"6\t 12\t\t 36\t\t 216\t\n",
"\n",
"7\t 14\t\t 49\t\t 343\t\n"
]
}
],
"prompt_number": 12
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 7.27, Page number: 211
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Read and display matrix\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"a = [[0 for i in range(0,10)]for i in range(0,10)]\n",
"row = int(raw_input(\"Enter Order of matrix upto (10x10) A : \"))\n",
"col = int(raw_input(\"Enter Order of matrix upto (10x10) A : \"))\n",
"\n",
"for i in range(0,row):\n",
" for j in range(0,col):\n",
" a[i][j] = int(raw_input(\"Enter Elements of matrix A : \"))\n",
"\n",
"#Result\n",
"sys.stdout.write(\"\\nThe Matrix is : \\n\")\n",
"for i in range(0,row):\n",
" for j in range(0,col):\n",
" sys.stdout.write(\"%6d\"%(a[i][j]))\n",
" sys.stdout.write(\"\\n\")"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Order of matrix upto (10x10) A : 3\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Order of matrix upto (10x10) A : 3\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of matrix A : 3\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of matrix A : 5\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of matrix A : 8\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of matrix A : 4\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of matrix A : 8\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of matrix A : 5\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of matrix A : 8\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of matrix A : 5\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of matrix A : 4\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"The Matrix is : \n",
" 3 5 8\n",
" 4 8 5\n",
" 8 5 4\n"
]
}
],
"prompt_number": 14
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 7.28, Page number: 212
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Transpose of matrix\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\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",
"row1 = int(raw_input(\"Enter Order of matrix upto (10x10) A : \"))\n",
"col1 = int(raw_input(\"Enter Order of matrix upto (10x10) A : \"))\n",
"\n",
"for i in range(0,row1):\n",
" for j in range(0,col1):\n",
" a[i][j] = int(raw_input(\"Enter Elements of matrix A : \"))\n",
"row2 = col1\n",
"col2 = row1\n",
"\n",
"#Calculation\n",
"for i in range(0,row1):\n",
" for j in range(0,col1):\n",
" b[j][i] = a[i][j]\n",
" \n",
"#Result\n",
"sys.stdout.write(\"\\nThe Matrix is : \\n\")\n",
"for i in range(0,row2):\n",
" for j in range(0,col2):\n",
" sys.stdout.write(\"%4d\"%(b[i][j]))\n",
" sys.stdout.write(\"\\n\")"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Order of matrix upto (10x10) A : 3\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Order of matrix upto (10x10) A : 3\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of matrix A : 3\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of matrix A : 5\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of matrix A : 8\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of matrix A : 4\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of matrix A : 8\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of matrix A : 5\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of matrix A : 8\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of matrix A : 5\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of matrix A : 4\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"The Matrix is : \n",
" 3 4 8\n",
" 5 8 5\n",
" 8 5 4\n"
]
}
],
"prompt_number": 15
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 7.29, Page number: 213
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Transpose of 3x3 matrix\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"mat = [[0 for i in range(0,3)]for i in range(0,3)]\n",
"for i in range(0,3):\n",
" for j in range(0,3):\n",
" mat[i][j] = int(raw_input(\"Enter Elements of matrix \"))\n",
"\n",
"#Result\n",
"sys.stdout.write(\"Transpose of the matrix\\n\")\n",
"for i in range(0,3):\n",
" for j in range(0,3):\n",
" sys.stdout.write(\"\\t%d\"%(mat[j][i]))\n",
" sys.stdout.write(\"\\n\")"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of matrix 5\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of matrix 8\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of matrix 9\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of matrix 2\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of matrix 5\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of matrix 4\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of matrix 7\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of matrix 5\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of matrix 2\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Transpose of the matrix\n",
"\t5\t2\t7\n",
"\t8\t5\t5\n",
"\t9\t4\t2\n"
]
}
],
"prompt_number": 16
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 7.30, Page number: 214
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Addition and subtraction of two matrices \n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\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",
"r1 = int(raw_input(\"Enter Order of Matrix A & B upto 10x10\"))\n",
"c1 = int(raw_input(\"Enter Order of Matrix A & B upto 10x10\"))\n",
"\n",
"for i in range(0,r1):\n",
" for j in range(0,c1):\n",
" a[i][j] = int(raw_input(\"Enter Elements of Matrix A:\"))\n",
"\n",
"for i in range(0,r1):\n",
" for j in range(0,c1):\n",
" b[i][j] = int(raw_input(\"Enter Elements of Matrix B:\"))\n",
"\n",
"#Calculation and Result\n",
"sys.stdout.write(\"\\nMatrix Addition\\n\")\n",
"for i in range(0,r1):\n",
" for j in range(0,c1):\n",
" sys.stdout.write(\"%5d\"%(a[i][j]+b[i][j]))\n",
" sys.stdout.write(\"\\n\")\n",
"\n",
"sys.stdout.write(\"\\nMatrix Subtraction\\n\")\n",
"for i in range(0,r1):\n",
" for j in range(0,c1):\n",
" sys.stdout.write(\"%5d\"%(a[i][j]-b[i][j]))\n",
" sys.stdout.write(\"\\n\")\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Order of Matrix A & B upto 10x103\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Order of Matrix A & B upto 10x103\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of Matrix A:4\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of Matrix A:5\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of Matrix A:8\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of Matrix A:2\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of Matrix A:9\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of Matrix A:8\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of Matrix A:2\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of Matrix A:9\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of Matrix A:4\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of Matrix B:1\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of Matrix B:3\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of Matrix B:5\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of Matrix B:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of Matrix B:5\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of Matrix B:4\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of Matrix B:6\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of Matrix B:7\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of Matrix B:2\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Matrix Addition\n",
" 5 8 13\n",
" 2 14 12\n",
" 8 16 6\n",
"\n",
"Matrix Subtraction\n",
" 3 2 3\n",
" 2 4 4\n",
" -4 2 2\n"
]
}
],
"prompt_number": 17
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 7.31, Page number: 216
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Multiplication of two matrices \n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\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",
"r1 = int(raw_input(\"Enter Order of Matrix A & B upto 10x10\"))\n",
"c1 = int(raw_input(\"Enter Order of Matrix A & B upto 10x10\"))\n",
"\n",
"for i in range(0,r1):\n",
" for j in range(0,c1):\n",
" a[i][j] = int(raw_input(\"Enter Elements of Matrix A:\"))\n",
"\n",
"for i in range(0,r1):\n",
" for j in range(0,c1):\n",
" b[i][j] = int(raw_input(\"Enter Elements of Matrix B:\"))\n",
"\n",
"#Calculation and Result\n",
"sys.stdout.write(\"\\nMultiplication of corresponding elements\\n\")\n",
"for i in range(0,r1):\n",
" for j in range(0,c1):\n",
" sys.stdout.write(\"%5d\"%(a[i][j]*b[i][j]))\n",
" sys.stdout.write(\"\\n\")\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Order of Matrix A & B upto 10x103\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Order of Matrix A & B upto 10x103\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of Matrix A:4\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of Matrix A:5\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of Matrix A:8\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of Matrix A:2\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of Matrix A:9\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of Matrix A:8\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of Matrix A:2\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of Matrix A:9\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of Matrix A:4\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of Matrix B:1\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of Matrix B:3\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of Matrix B:5\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of Matrix B:0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of Matrix B:5\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of Matrix B:4\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of Matrix B:6\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of Matrix B:7\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Elements of Matrix B:2\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Multiplication of corresponding elements\n",
" 4 15 40\n",
" 0 45 32\n",
" 12 63 8\n"
]
}
],
"prompt_number": 18
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 7.32, Page number: 217
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Read the Quantity & Price of various Pentium models using an array and\n",
"#Compute the total cost of all models.\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"pc = [[0 for i in range(0,4)]for i in range(0,4)]\n",
"t = 0\n",
"\n",
"for i in range(0,4):\n",
" for j in range(0,2):\n",
" pc[i][j] = int(raw_input(\"Enter Qty. & Price for Pentium %d : \"%(i+1)))\n",
"\n",
"#Result\n",
"sys.stdout.write(\"=====================================================\")\n",
"sys.stdout.write(\"\\nModel\\tQty.\\tRate(Rs.)\\tTotal Value\")\n",
"sys.stdout.write(\"\\n=====================================================\")\n",
"for i in range(0,4):\n",
" sys.stdout.write(\"\\nPentium %d %2ld %6ld %15ld\"%(i+1,pc[i][0],pc[i][1],pc[i][0]*pc[i][1]))\n",
" t = t + pc[i][0] * pc[i][1]\n",
"\n",
"sys.stdout.write(\"\\n=====================================================\")\n",
"sys.stdout.write(\"\\nTotal Value of All PCs in Rs. %ld\"%(t))\n",
"sys.stdout.write(\"\\n=====================================================\")"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Qty. & Price for Pentium 1 : 25\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Qty. & Price for Pentium 1 : 25000\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Qty. & Price for Pentium 2 : 20\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Qty. & Price for Pentium 2 : 40000\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Qty. & Price for Pentium 3 : 15\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Qty. & Price for Pentium 3 : 35000\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Qty. & Price for Pentium 4 : 20\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Qty. & Price for Pentium 4 : 40000\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"=====================================================\n",
"Model\tQty.\tRate(Rs.)\tTotal Value\n",
"=====================================================\n",
"Pentium 1 25 25000 625000\n",
"Pentium 2 20 40000 800000\n",
"Pentium 3 15 35000 525000\n",
"Pentium 4 20 40000 800000\n",
"=====================================================\n",
"Total Value of All PCs in Rs. 2750000\n",
"====================================================="
]
}
],
"prompt_number": 20
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#working of three dimensional array.\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"array_3d = [[[0 for i in range(0,3)]for i in range(0,3)]for i in range(0,3)]\n",
"\n",
"for a in range(0,3):\n",
" for b in range(0,3):\n",
" for c in range(0,3):\n",
" array_3d[a][b][c] = a+b+c\n",
"\n",
"#Result\n",
"for a in range(0,3):\n",
" sys.stdout.write(\"\\n\")\n",
" for b in range(0,3):\n",
" for c in range(0,3):\n",
" sys.stdout.write(\"%3d\"%(array_3d[a][b][c]))\n",
" sys.stdout.write(\"\\n\")"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
" 0 1 2\n",
" 1 2 3\n",
" 2 3 4\n",
"\n",
" 1 2 3\n",
" 2 3 4\n",
" 3 4 5\n",
"\n",
" 2 3 4\n",
" 3 4 5\n",
" 4 5 6\n"
]
}
],
"prompt_number": 54
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 7.38, Page number: 225
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#working of four dimensional array\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"array_4d = [[[[0 for i in range(0,2)]for i in range(0,2)]for i in range(0,2)]for i in range(0,2)]\n",
"\n",
"for a in range(0,2):\n",
" for b in range(0,2):\n",
" for c in range(0,2):\n",
" for d in range(0,2):\n",
" array_4d[a][b][c][d] = a+b+c+d\n",
" \n",
"#Result\n",
"for a in range(0,2):\n",
" sys.stdout.write(\"\\n\")\n",
" for b in range(0,2):\n",
" for c in range(0,2):\n",
" for d in range(0,2):\n",
" sys.stdout.write(\"%3d\"%(array_4d[a][b][c][d]))\n",
" sys.stdout.write(\"\\n\")"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
" 0 1\n",
" 1 2\n",
" 1 2\n",
" 2 3\n",
"\n",
" 1 2\n",
" 2 3\n",
" 2 3\n",
" 3 4\n"
]
}
],
"prompt_number": 56
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 7.39, Page number: 226
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Read the quantity & rate of certain items using multidimensional\n",
"#array. Calculate total cost by multiplying quantity & rate and offer 2%\n",
"#discount on it & display the net amount.\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"m = [[[[0 for i in range(0,2)]for i in range(0,2)]for i in range(0,2)]for i in range(0,2)]\n",
"\n",
"for a in range(0,2):\n",
" for b in range(0,2):\n",
" for c in range(0,2):\n",
" for d in range(0,1):\n",
" if a == 0:\n",
" m[a][b][c][d] = int(raw_input(\"Enter Quantity & Rate\\na=%d b=%d c=%d d=%d\"%(a,b,c,d)))\n",
" m[a][b][c][d+1] = int(raw_input(\"Enter Quantity & Rate\\na=%d b=%d c=%d d=%d\"%(a,b,c,d)))\n",
" else:\n",
" m[a][b][c][d] = m[a-1][b][c][d] * m[a-1][b][c][d+1]\n",
" m[a][b][c][d+1] = m[a-1][b][c][d] * m[a-1][b][c][d+1] *2/100\n",
" \n",
"#Result\n",
"sys.stdout.write(\"\\n============================================================\\n\")\n",
"sys.stdout.write(\"Quantity\\tRate\\tAmoount\\tDiscount(@2%)\\tNet Amount\\n\")\n",
"sys.stdout.write(\"============================================================\\n\")\n",
"\n",
"for a in range(0,1):\n",
" for b in range(0,2):\n",
" for c in range(0,2):\n",
" for d in range(0,1):\n",
" sys.stdout.write(\"\\n%10ld %10ld\"%(m[a][b][c][d],m[a][b][c][d+1]))\n",
" sys.stdout.write(\"%8ld.00 %6ld.00 %12ld.00\"%(m[a+1][b][c][d],m[a+1][b][c][d+1],m[a+1][b][c][d] - m[a+1][b][c][d+1]))\n",
" \n",
"sys.stdout.write(\"\\n============================================================\")\n",
" "
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Quantity & Rate\n",
"a=0 b=0 c=0 d=025\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Quantity & Rate\n",
"a=0 b=0 c=0 d=050\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Quantity & Rate\n",
"a=0 b=0 c=1 d=030\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Quantity & Rate\n",
"a=0 b=0 c=1 d=060\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Quantity & Rate\n",
"a=0 b=1 c=0 d=035\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Quantity & Rate\n",
"a=0 b=1 c=0 d=070\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Quantity & Rate\n",
"a=0 b=1 c=1 d=040\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Quantity & Rate\n",
"a=0 b=1 c=1 d=075\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"============================================================\n",
"Quantity\tRate\tAmoount\tDiscount(@2%)\tNet Amount\n",
"============================================================\n",
"\n",
" 25 50 1250.00 25.00 1225.00\n",
" 30 60 1800.00 36.00 1764.00\n",
" 35 70 2450.00 49.00 2401.00\n",
" 40 75 3000.00 60.00 2940.00\n",
"============================================================"
]
}
],
"prompt_number": 22
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 7.40, Page number: 227
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Read string from a character array.\n",
"\n",
"import sys\n",
"\n",
"#Variable Initialization\n",
"in1 = raw_input(\"\") #Since in is a keyword in python, in1 is used\n",
"\n",
"#Result\n",
"#There is no sscanf() function in python\n",
"out = in1\n",
"sys.stdout.write(\"%s\\n\"%(out))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"HELLO\n"
]
}
],
"prompt_number": 60
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 7.41, Page number: 228
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Read integers in character array, convert and assign them to integer\n",
"#variable.\n",
"\n",
"import sys\n",
"\n",
"#Variable initialization\n",
"in1 = raw_input(\"Enter Integers : \")\n",
"\n",
"#Result\n",
"sys.stdout.write(\"Value of int x : %s\"%(in1))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter Integers : 123\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Value of int x : 123"
]
}
],
"prompt_number": 23
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"