PK I018 18 Let us C/chapter-9.ipynb{
"metadata": {
"name": "",
"signature": "sha256:4f09be4d67c9b4aa6b1f97f3788fa85904e9dfc6d52d9f340d60a59f47f71089"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Chapter 9: Puppetting On Strings "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Print String , Page number: 329"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#Variable declaration\n",
"name= \"Klinsman\" # character array or string\n",
"i = 0\n",
"\n",
"#while loop for printing\n",
"while i <= 7 :\n",
" print name[i] \n",
" i = i + 1\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"K\n",
"l\n",
"i\n",
"n\n",
"s\n",
"m\n",
"a\n",
"n\n"
]
}
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Print String, Page number: 330"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#Variable declaration\n",
"name= \"Klinsman\\0\" # character array or string\n",
"i = 0\n",
"\n",
"#while loop for printing\n",
"while (name[i] != '\\0') :\n",
" print name[i]\n",
" i = i + 1\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"K\n",
"l\n",
"i\n",
"n\n",
"s\n",
"m\n",
"a\n",
"n\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Print String Using Pointer, Page number: 330"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#Variable declaration\n",
"name = \"Klinsman\\0\" # string or character array\n",
"i = 0\n",
"ptr = name[i] # store base address of string\n",
"\n",
"#Result\n",
"while ptr != '\\0':\n",
" print ptr\n",
" i = i+1\n",
" ptr = name[i]\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"K\n",
"l\n",
"i\n",
"n\n",
"s\n",
"m\n",
"a\n",
"n\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"strlen Function, Page number: 336"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"\n",
"#Variable declaration\n",
"arr = \"Bamboozled\" # character array or string\n",
"\n",
"#string length function\n",
"len1 = len(arr) \n",
"len2 = len( \"Humpty Dumpty\" )\n",
"\n",
"#Result\n",
"print \"string = %s length = %d\" %( arr, len1 )\n",
"print \"string = %s length = %d\" %(\"Humpty Dumpty\", len2 )\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"string = Bamboozled length = 10\n",
"string = Humpty Dumpty length = 13\n"
]
}
],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"xstrlen Function, Page number: 337"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#Function definition\n",
"def xstrlen(s):\n",
" length = 0\n",
" while ( s[length] != '\\0' ):\n",
" length += 1\n",
" return length \n",
"\n",
"#Variable declaration\n",
"arr = \"Bamboozled\\0\" # character array or string\n",
"\n",
"#Function calls\n",
"len1 = xstrlen(arr) \n",
"len2 = xstrlen( \"Humpty Dumpty\\0\" )\n",
"\n",
"#Result\n",
"print \"string = %s length = %d\" %( arr, len1 )\n",
"print \"string = %s length = %d\" %(\"Humpty Dumpty\\0\", len2 )\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"string = Bamboozled\u0000 length = 10\n",
"string = Humpty Dumpty\u0000 length = 13\n"
]
}
],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"strcpy Function, Page number: 338"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"\n",
"#Variable declaration\n",
"source = \"Sayonara\" \n",
"target = [] \n",
"\n",
"#strcpy function\n",
"import copy\n",
"target = copy.copy(source)\n",
"\n",
"#Result\n",
"print \"source string = \", source \n",
"print \"target string = \", target \n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"source string = Sayonara\n",
"target string = Sayonara\n"
]
}
],
"prompt_number": 6
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"xstrcpy Function, Page number: 339"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#Function definition\n",
"def xstrcpy (t,s):\n",
" i = 0\n",
" while ( s[i] != '\\0' ):\n",
" t = t + s[i]\n",
" i = i + 1\n",
" return t\n",
" \n",
"#Variable declaration\n",
"source = \"Sayonara\\0\" \n",
"target = ''\n",
"\n",
"target = xstrcpy ( target, source ) # function call\n",
"\n",
"#Result\n",
"print \"source string = \", source \n",
"print \"target string = \", target "
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"source string = Sayonara\u0000\n",
"target string = Sayonara\n"
]
}
],
"prompt_number": 7
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"strcat Function, Page number: 342"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#Variable declaration\n",
"source = \"Folks!\" \n",
"target = \"Hello\"\n",
" \n",
"target = target + source # string concatenation\n",
"#Result\n",
"print \"source string = \", source \n",
"print \"target string = \", target \n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"source string = Folks!\n",
"target string = HelloFolks!\n"
]
}
],
"prompt_number": 8
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"strcmp Function, Page number: 343"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#Variable declaration\n",
"string1 = \"Jerry\\0\" \n",
"string2 = \"Ferry\\0\"\n",
"\n",
"#Function definition\n",
"def strcmp (string1 , string2):\n",
" if (string1 == string2):\n",
" v = 0 #If the two strings are identical, strcmp returns a value zero\n",
" return v\n",
" else:\n",
" n = 0\n",
" while ( string1[n]):\n",
" if ( string1[n] == string2[n]):\n",
" n = n + 1\n",
" continue\n",
" else:\n",
" v = ord(string1[n]) - ord(string2[n]) #returns the numeric difference between the ASCII values of the first non-matching pairs of characters\n",
" return v\n",
" return v\n",
"\n",
"#Function call\n",
"i = strcmp ( string1, \"Jerry\\0\" ) \n",
"j = strcmp ( string1, string2 ) \n",
"k = strcmp ( string1, \"Jerry boy\\0\" )\n",
"\n",
"#Result\n",
"print i,j,k"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"0 4 -32\n"
]
}
],
"prompt_number": 9
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Two Dimensional Array of Characters, Page number: 344"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"\n",
"\n",
"#Function definition\n",
"def strcmp (string1 , string2):\n",
" if (string1 == string2):\n",
" v = 0 #If the two strings are identical, strcmp returns a value zero\n",
" return v\n",
" else:\n",
" n = 0\n",
" while ( string1[n]):\n",
" if ( string1[n] == string2[n]):\n",
" n = n + 1\n",
" continue\n",
" else:\n",
" v = ord(string1[n]) - ord(string2[n]) #returns the numeric difference between the ASCII values of the first non-matching pairs of characters\n",
" return v\n",
" return v\n",
"\n",
"#Variable declaration\n",
"FOUND = 1\n",
"NOTFOUND = 0\n",
"masterlist =[\"akshay\",\"parag\",\"raman\",\"srinivas\",\"gopal\",\"rajesh\"]\n",
"yourname = []\n",
"flag = NOTFOUND\n",
"\n",
"#Input from user\n",
"#yourname = raw_input(\"Enter your name: \")\n",
"yourname = \"Akshatha\"\n",
"\n",
"#Checking in the master list\n",
"for i in range(0,6):\n",
" a = strcmp ( masterlist[i], yourname )\n",
" if a == 0:\n",
" print \"Welcome, you can enter the palace\" \n",
" flag = FOUND\n",
" break\n",
" \n",
"if flag == NOTFOUND :\n",
" print \"Sorry, you are a trespasser\" "
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Sorry, you are a trespasser\n"
]
}
],
"prompt_number": 10
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Exchange Names using 2-D Array of Characters, Page number: 348"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#Variable declaration\n",
"names = [\"akshay\\0\\0\\0\",\"parag\\0\\0\\0\\0\",\"raman\\0\\0\\0\\0\",\"srinivas\\0\",\"gopal\\0\\0\\0\\0\",\"rajesh\\0\\0\\0\"]\n",
"\n",
"#Initial condition\n",
"print \"Original: \", names[2], names[3] \n",
"\n",
"#Exchanging names\n",
"for i in range(0,9):\n",
" t = names[3][i]\n",
" names[3] = names[3][0:i] + names[2][i] + names[3][i+1:]\n",
" names[2] = names[2][0:i] + t + names[2][i+1:]\n",
" \n",
"\n",
"#Result \n",
"print \"New: \", names[2], names[3] "
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Original: raman\u0000\u0000\u0000\u0000 srinivas\u0000\n",
"New: srinivas\u0000 raman\u0000\u0000\u0000\u0000\n"
]
}
],
"prompt_number": 11
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Exchange Names using Array of Pointers to Strings, Page number: 349"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#Variable declaration\n",
"names = [\"akshay\\0\\0\\0\",\"parag\\0\\0\\0\\0\",\"raman\\0\\0\\0\\0\",\"srinivas\\0\",\"gopal\\0\\0\\0\\0\",\"rajesh\\0\\0\\0\"]\n",
"\n",
"#Initial condition\n",
"print \"Original: \", names[2], names[3] \n",
"\n",
"#Exchanging names\n",
"temp = names[2] \n",
"names[2] = names[3] \n",
"names[3] = temp \n",
"\n",
"#Result \n",
"print \"New: \", names[2], names[3] \n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Original: raman\u0000\u0000\u0000\u0000 srinivas\u0000\n",
"New: srinivas\u0000 raman\u0000\u0000\u0000\u0000\n"
]
}
],
"prompt_number": 12
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Solution, Page number: 351"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"import copy\n",
"\n",
"#Variable declaration\n",
"names = []\n",
"n=[\"John\",\"Max\",\"Jim\",\"Tony\",\"Tom\",\"Harry\"]\n",
"\n",
"for i in range(0,6):\n",
" n1 = n[i]\n",
" p = copy.copy(n1)\n",
" names.append(p) \n",
"\n",
"for i in range(0,6):\n",
" print names[i] \n",
"\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"John\n",
"Max\n",
"Jim\n",
"Tony\n",
"Tom\n",
"Harry\n"
]
}
],
"prompt_number": 15
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}PK IR R Let us C/chapter-8.ipynb{
"metadata": {
"name": "chapter-8.ipynb"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Chapter 8: Arrays "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Average Marks, Page number: 272"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#Variable declaration\n",
"s = 0\n",
"marks = [] # array declaration\n",
"\n",
"#for i in range(0,30):\n",
" # marks.append(int(raw_input(\"Enter marks: \" ))) # store data in array\n",
"marks = [89,85,57,25,90,45,87,48,98,12,39,66,75,30,87,100,5,78,56,99,84,0,39,79,93,61,87,45,90,56] \n",
"print \"Enter marks: \"\n",
"for i in range(0,30):\n",
" print marks[i]\n",
"\n",
"for i in range(0,30):\n",
" s = s + marks[i] # read data from array\n",
"\n",
"#Calculation\n",
"avg = s / 30 #Average formula\n",
"\n",
"#Result\n",
"print \"Average marks = \", avg \n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter marks: \n",
"89\n",
"85\n",
"57\n",
"25\n",
"90\n",
"45\n",
"87\n",
"48\n",
"98\n",
"12\n",
"39\n",
"66\n",
"75\n",
"30\n",
"87\n",
"100\n",
"5\n",
"78\n",
"56\n",
"99\n",
"84\n",
"0\n",
"39\n",
"79\n",
"93\n",
"61\n",
"87\n",
"45\n",
"90\n",
"56\n",
"Average marks = 63\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Call By Value, Page number: 277"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#Funcion definition\n",
"def display(m):\n",
" print m \n",
" \n",
"#Variable declaration\n",
"marks = [ 55, 65, 75, 56, 78, 78, 90 ] #array\n",
"\n",
"for i in range(0,7):\n",
" display(marks[i]) #function call \n"
],
"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": "markdown",
"metadata": {},
"source": [
"Call By Reference , Page number: 278"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#Funcion definition\n",
"def display(n):\n",
" print n #return\n",
" \n",
"#Variable declaration\n",
"marks = [ 55, 65, 75, 56, 78, 78, 90 ] #array\n",
"\n",
"for i in range(0,7):\n",
" display(marks[i]) #function call\n",
"\n"
],
"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": 7
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Pointer Arithmetic, Page number: 279"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#Variable declaration\n",
"i = 3\n",
"j = 1.5\n",
"k = 'c'\n",
"\n",
"print \"Value of i = \", i \n",
"print \"Value of j = \", j \n",
"print \"Value of k = \", k \n",
"\n",
"#addresses of the variables\n",
"x = id(i)\n",
"y = id(j)\n",
"z = id(k)\n",
"\n",
"print \"Original address in x = \", x \n",
"print \"Original address in y = \", y \n",
"print \"Original address in z = \", z \n",
"\n",
"x += 2\n",
"y += 4\n",
"z += 1\n",
"\n",
"print \"New address in x = \", x \n",
"print \"New address in y = \", y \n",
"print \"New address in z = \", z \n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Value of i = 3\n",
"Value of j = 1.5\n",
"Value of k = c\n",
"Original address in x = 32529512\n",
"Original address in y = 105587440\n",
"Original address in z = 32744192\n",
"New address in x = 32529514\n",
"New address in y = 105587444\n",
"New address in z = 32744193\n"
]
}
],
"prompt_number": 8
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Pointer Subtraction , Page number: 281"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#Variable declaration\n",
"arr = [ 10, 20, 30, 45, 67, 56, 74 ]\n",
"i = id(arr[1]) #address \n",
"j = id(arr[5]) #Address\n",
"\n",
"#Result\n",
"print j - i, arr[5] - arr[1] \n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"1128 36\n"
]
}
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Pointer Comparison, Page number: 282"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#Variable declaration\n",
"arr = [ 10, 20, 36, 72, 45, 36 ]\n",
"j = id(arr[ 4 ])\n",
"k = id( arr[0 + 4] )\n",
"\n",
"#Result \n",
"if j == k : #comparison\n",
" print \"The two pointers point to the same location\" \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"
]
}
],
"prompt_number": 10
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Memory Locations, Page number: 283"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#Variable declaration\n",
"num = [ 24, 34, 12, 44, 56, 17]\n",
"\n",
"#Result\n",
"for i in range(0,6):\n",
" print \"element no. \", i \n",
" print \"address = \", id(num[i]) \n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"element no. 0\n",
"address = 32529008\n",
"element no. 1\n",
"address = 32528768\n",
"element no. 2\n",
"address = 32529296\n",
"element no. 3\n",
"address = 32530520\n",
"element no. 4\n",
"address = 32530232\n",
"element no. 5\n",
"address = 32529176\n"
]
}
],
"prompt_number": 11
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Accessing Array Elements , Page number: 284"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#Variable declaration\n",
"num = [ 24, 34, 12, 44, 56, 17]\n",
"\n",
"#Result\n",
"for i in range(0,6):\n",
" print \"address = \", id(num[i]) \n",
" print \"element = \", num[i] \n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"address = 32529008\n",
"element = 24\n",
"address = 32528768\n",
"element = 34\n",
"address = 32529296\n",
"element = 12\n",
"address = 32530520\n",
"element = 44\n",
"address = 32530232\n",
"element = 56\n",
"address = 32529176\n",
"element = 17\n"
]
}
],
"prompt_number": 12
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Accessing Array Elements Using Pointers, Page number: 284"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#Variable declaration\n",
"num = [ 24, 34, 12, 44, 56, 17]\n",
"j = id(num[0]) # assign address of zeroth element\n",
"\n",
"#Result\n",
"for i in range(0,6):\n",
" print \"address = \", j \n",
" print \"element = \", num[i] \n",
" j = id(num[(i+1)%6]) # increment pointer to point to next location \n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"address = 32529008\n",
"element = 24\n",
"address = 32528768\n",
"element = 34\n",
"address = 32529296\n",
"element = 12\n",
"address = 32530520\n",
"element = 44\n",
"address = 32530232\n",
"element = 56\n",
"address = 32529176\n",
"element = 17\n"
]
}
],
"prompt_number": 13
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Passing An Array to a Function , Page number: 286"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#Variable declaration\n",
"num = [ 24, 34, 12, 44, 56, 17 ]\n",
"\n",
"#Function definition:\n",
"def display ( j,n ):\n",
" for i in range(0,n):\n",
" print \"element = \", j\n",
" j = num[(i+1)%n] #increment pointer to point to next element \n",
"\n",
"display ( num[0], 6 ) #function call\n",
"\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"element = 24\n",
"element = 34\n",
"element = 12\n",
"element = 44\n",
"element = 56\n",
"element = 17\n"
]
}
],
"prompt_number": 14
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Accessing Array Elements in Different Ways, Page number: 288"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#Variable declaration\n",
"num = [ 24, 34, 12, 44, 56, 17]\n",
"\n",
"for i in range(0,6):\n",
" print \"address = \", id(num[i])\n",
" print \"element = \", num[i], num[(0+ i)]\n",
" print num[(i+0)], num[i] \n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"address = 32529008\n",
"element = 24 24\n",
"24 24\n",
"address = 32528768\n",
"element = 34 34\n",
"34 34\n",
"address = 32529296\n",
"element = 12 12\n",
"12 12\n",
"address = 32530520\n",
"element = 44 44\n",
"44 44\n",
"address = 32530232\n",
"element = 56 56\n",
"56 56\n",
"address = 32529176\n",
"element = 17 17\n",
"17 17\n"
]
}
],
"prompt_number": 15
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Two Dimensional Array , Page number: 289"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#Variable declaration\n",
"stud = [] #array\n",
"\n",
"#for i in range(0,4):\n",
" # stud.append((raw_input(\"Enter roll no and marks: \").split())) # storing data in the 2D array\n",
"\n",
"stud = [\"1 38\",\"2 78\",\"3 93\",\"4 48\"]\n",
"for i in range (0,4):\n",
" print \"Enter roll no and marks: \"\n",
" print stud[i]\n",
" \n",
"#Result\n",
"for i in range(0,4):\n",
" print stud[i][0],stud[i][1:] \n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter roll no and marks: \n",
"1 38\n",
"Enter roll no and marks: \n",
"2 78\n",
"Enter roll no and marks: \n",
"3 93\n",
"Enter roll no and marks: \n",
"4 48\n",
"1 38\n",
"2 78\n",
"3 93\n",
"4 48\n"
]
}
],
"prompt_number": 19
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"2-D Array Demo , Page number: 293"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#Variable declaration\n",
"s = [ [1234, 56 ], [ 1212, 33], [ 1434, 80 ], [ 1312, 78 ]]\n",
"\n",
"#Result\n",
"for i in range(0,4):\n",
" print \"Address of %d th 1-D array = %u\"%( i, id(s[i]) )\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Address of 0 th 1-D array = 134002248\n",
"Address of 1 th 1-D array = 134654472\n",
"Address of 2 th 1-D array = 134791816\n",
"Address of 3 th 1-D array = 134792008\n"
]
}
],
"prompt_number": 21
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Accessing 2-D Array Elements, Page number: 295"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#Variable declaration\n",
"s = [ [ 1234, 56 ], [ 1212, 33 ], [ 1434, 80 ], [ 1312, 78 ] ]\n",
"\n",
"for i in range(0,4):\n",
" for j in range(0,2):\n",
" print s[i][j] \n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"1234\n",
"56\n",
"1212\n",
"33\n",
"1434\n",
"80\n",
"1312\n",
"78\n"
]
}
],
"prompt_number": 22
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Pointer To An Array , Page number: 296"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#Variable declaration\n",
"s = [ [ 1234, 56 ], [ 1212, 33 ], [ 1434, 80 ], [ 1312, 78 ]]\n",
"\n",
"#Result\n",
"for i in range(0,4):\n",
" p = s[i]\n",
" pint = p\n",
" print \"\\n\" \n",
" for j in range(0,2):\n",
" print pint[j] \n"
],
"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": 23
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Passing 2-D Array To a Function , Page number: 297"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#Variable declaration\n",
"a = [[1, 2, 3, 4] , [5, 6, 7, 8] , [9, 0, 1, 6 ]]\n",
"\n",
"#Function definitions\n",
"def display ( q, row, col ):\n",
" for i in range(0,row):\n",
" for j in range(0,col):\n",
" print q [ (i * col)%3][j]\n",
" print \"\\n\" \n",
" print \"\\n\" \n",
"\n",
"\n",
"def show ( q, row, col ):\n",
" for i in range(0,row):\n",
" p = q[i]\n",
" for j in range(0,col):\n",
" print p[j]\n",
" print \"\\n\" \n",
" print \"\\n\" \n",
"\n",
"def Print ( q, row, col ):\n",
" for i in range(0,row):\n",
" for j in range(0,col):\n",
" print q[i][j]\n",
" print \"\\n\" \n",
" print \"\\n\" \n",
" \n",
"#function calls\n",
"display ( a, 3, 4 ) \n",
"show ( a, 3, 4 ) \n",
"Print ( a, 3, 4 )\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"1\n",
"2\n",
"3\n",
"4\n",
"\n",
"\n",
"5\n",
"6\n",
"7\n",
"8\n",
"\n",
"\n",
"9\n",
"0\n",
"1\n",
"6\n",
"\n",
"\n",
"\n",
"\n",
"1\n",
"2\n",
"3\n",
"4\n",
"\n",
"\n",
"5\n",
"6\n",
"7\n",
"8\n",
"\n",
"\n",
"9\n",
"0\n",
"1\n",
"6\n",
"\n",
"\n",
"\n",
"\n",
"1\n",
"2\n",
"3\n",
"4\n",
"\n",
"\n",
"5\n",
"6\n",
"7\n",
"8\n",
"\n",
"\n",
"9\n",
"0\n",
"1\n",
"6\n",
"\n",
"\n",
"\n",
"\n"
]
}
],
"prompt_number": 25
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Array of Pointers , Page number: 300"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"from ctypes import *\n",
"\n",
"#Variable declaration\n",
"arr = [] # array of integer pointers \n",
"i = c_int(31)\n",
"j = c_int(5)\n",
"k = c_int(19)\n",
"l = c_int(71)\n",
"arr.append(pointer(i))\n",
"arr.append(pointer(j))\n",
"arr.append(pointer(k))\n",
"arr.append(pointer(l))\n",
"\n",
"for m in range(0,4):\n",
" print arr[m].contents "
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"c_long(31)\n",
"c_long(5)\n",
"c_long(19)\n",
"c_long(71)\n"
]
}
],
"prompt_number": 26
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Address Array , Page number: 301"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#Variable declaration\n",
"a = [ 0, 1, 2, 3, 4 ]\n",
"p = [ a[0], a[1], a[2], a[3], a[4] ]\n",
"\n",
"#Result\n",
"print p, id(p), id(id(p ) )\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[0, 1, 2, 3, 4] 134794120 134005648\n"
]
}
],
"prompt_number": 29
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}PK Ieo o Let us C/chapter-7.ipynb{
"metadata": {
"name": "chapter-7..ipynb"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Chapter 7: The C Preprocessor"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Macro Expansion, Page number: 244"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#Macro declaration\n",
"UPPER = 25\n",
"\n",
"for i in range(1,UPPER+1): #macro expansion\n",
" print(i)"
],
"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",
"11\n",
"12\n",
"13\n",
"14\n",
"15\n",
"16\n",
"17\n",
"18\n",
"19\n",
"20\n",
"21\n",
"22\n",
"23\n",
"24\n",
"25\n"
]
}
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Macro Definition, Page number: 244"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#Macro declaration\n",
"PI = 3.1415\n",
"\n",
"#Variable declaration\n",
"r = 6.25 \n",
"\n",
"#Calculation\n",
"area = PI * r * r\n",
"\n",
"#Result\n",
"print \"Area of circle = \", area \n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Area of circle = 122.71484375\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Macros with Arguments, Page number: 248"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#Macro declaration\n",
"def AREA(x): #define AREA(x) ( 3.14 * x * x )\n",
" return(3.14 * x * x )\n",
"\n",
"#Variable declaration\n",
"r1 = 6.25\n",
"r2 = 2.5\n",
"\n",
"#Result\n",
"a = AREA(r1)\n",
"print \"Area of circle = \", a \n",
"a = AREA(r2) \n",
"print \"Area of circle = \", a\n",
" \n",
"\n",
"\n",
"\n",
"\n",
"\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Area of circle = 122.65625\n",
"Area of circle = 19.625\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Macros with Arguments, Page number: 249"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#Macro declaration\n",
"def ISDIGIT(y): #define ISDIGIT(y) ( y >= 48 && y <= 57 )\n",
" return( y >= 48 and y <= 57 )\n",
"\n",
"#Input from user\n",
"#ch = raw_input(\"Enter any digit \")\n",
"ch = 'a'\n",
"\n",
"#Result\n",
"if ISDIGIT ( ch ):\n",
" print \"You entered a digit\" \n",
"else:\n",
" print \"Illegal input\" \n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Illegal input\n"
]
}
],
"prompt_number": 7
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}PK I)ωk3 k3 Let us C/chapter-6.ipynb{
"metadata": {
"name": "",
"signature": "sha256:840b5b41a5ba5f473d9fd4f85395b35887d7b6a2fb119c03c9559a162f751aa0"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Chapter 6: Data Types Revisted "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"char Example, Page number: 218"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#Variable declaration\n",
"ch = 291\n",
"\n",
"#Result\n",
"print \"%d %c\" %( ch, (ch%128) ) \n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"291 #\n"
]
}
],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Range of char, Page number: 218"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"for ch in range(0,256):\n",
" print \"%d %c\" %(ch, ch%128 )\n",
"\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"0 \u0000\n",
"1 \u0001\n",
"2 \u0002\n",
"3 \u0003\n",
"4 \u0004\n",
"5 \u0005\n",
"6 \u0006\n",
"7 \u0007\n",
"8 \b\n",
"9 \t\n",
"10 \n",
"\n",
"11 \u000b",
"\n",
"12 \f",
"\n",
"13 \r\n",
"14 \u000e\n",
"15 \u000f\n",
"16 \u0010\n",
"17 \u0011\n",
"18 \u0012\n",
"19 \u0013\n",
"20 \u0014\n",
"21 \u0015\n",
"22 \u0016\n",
"23 \u0017\n",
"24 \u0018\n",
"25 \u0019\n",
"26 \u001a\n",
"27 \u001b\n",
"28 \u001c",
"\n",
"29 \u001d",
"\n",
"30 \u001e",
"\n",
"31 \u001f\n",
"32 \n",
"33 !\n",
"34 \"\n",
"35 #\n",
"36 $\n",
"37 %\n",
"38 &\n",
"39 '\n",
"40 (\n",
"41 )\n",
"42 *\n",
"43 +\n",
"44 ,\n",
"45 -\n",
"46 .\n",
"47 /\n",
"48 0\n",
"49 1\n",
"50 2\n",
"51 3\n",
"52 4\n",
"53 5\n",
"54 6\n",
"55 7\n",
"56 8\n",
"57 9\n",
"58 :\n",
"59 ;\n",
"60 <\n",
"61 =\n",
"62 >\n",
"63 ?\n",
"64 @\n",
"65 A\n",
"66 B\n",
"67 C\n",
"68 D\n",
"69 E\n",
"70 F\n",
"71 G\n",
"72 H\n",
"73 I\n",
"74 J\n",
"75 K\n",
"76 L\n",
"77 M\n",
"78 N\n",
"79 O\n",
"80 P\n",
"81 Q\n",
"82 R\n",
"83 S\n",
"84 T\n",
"85 U\n",
"86 V\n",
"87 W\n",
"88 X\n",
"89 Y\n",
"90 Z\n",
"91 [\n",
"92 \\\n",
"93 ]\n",
"94 ^\n",
"95 _\n",
"96 `\n",
"97 a\n",
"98 b\n",
"99 c\n",
"100 d\n",
"101 e\n",
"102 f\n",
"103 g\n",
"104 h\n",
"105 i\n",
"106 j\n",
"107 k\n",
"108 l\n",
"109 m\n",
"110 n\n",
"111 o\n",
"112 p\n",
"113 q\n",
"114 r\n",
"115 s\n",
"116 t\n",
"117 u\n",
"118 v\n",
"119 w\n",
"120 x\n",
"121 y\n",
"122 z\n",
"123 {\n",
"124 |\n",
"125 }\n",
"126 ~\n",
"127 \u007f\n",
"128 \u0000\n",
"129 \u0001\n",
"130 \u0002\n",
"131 \u0003\n",
"132 \u0004\n",
"133 \u0005\n",
"134 \u0006\n",
"135 \u0007\n",
"136 \b\n",
"137 \t\n",
"138 \n",
"\n",
"139 \u000b",
"\n",
"140 \f",
"\n",
"141 \r\n",
"142 \u000e\n",
"143 \u000f\n",
"144 \u0010\n",
"145 \u0011\n",
"146 \u0012\n",
"147 \u0013\n",
"148 \u0014\n",
"149 \u0015\n",
"150 \u0016\n",
"151 \u0017\n",
"152 \u0018\n",
"153 \u0019\n",
"154 \u001a\n",
"155 \u001b\n",
"156 \u001c",
"\n",
"157 \u001d",
"\n",
"158 \u001e",
"\n",
"159 \u001f\n",
"160 \n",
"161 !\n",
"162 \"\n",
"163 #\n",
"164 $\n",
"165 %\n",
"166 &\n",
"167 '\n",
"168 (\n",
"169 )\n",
"170 *\n",
"171 +\n",
"172 ,\n",
"173 -\n",
"174 .\n",
"175 /\n",
"176 0\n",
"177 1\n",
"178 2\n",
"179 3\n",
"180 4\n",
"181 5\n",
"182 6\n",
"183 7\n",
"184 8\n",
"185 9\n",
"186 :\n",
"187 ;\n",
"188 <\n",
"189 =\n",
"190 >\n",
"191 ?\n",
"192 @\n",
"193 A\n",
"194 B\n",
"195 C\n",
"196 D\n",
"197 E\n",
"198 F\n",
"199 G\n",
"200 H\n",
"201 I\n",
"202 J\n",
"203 K\n",
"204 L\n",
"205 M\n",
"206 N\n",
"207 O\n",
"208 P\n",
"209 Q\n",
"210 R\n",
"211 S\n",
"212 T\n",
"213 U\n",
"214 V\n",
"215 W\n",
"216 X\n",
"217 Y\n",
"218 Z\n",
"219 [\n",
"220 \\\n",
"221 ]\n",
"222 ^\n",
"223 _\n",
"224 `\n",
"225 a\n",
"226 b\n",
"227 c\n",
"228 d\n",
"229 e\n",
"230 f\n",
"231 g\n",
"232 h\n",
"233 i\n",
"234 j\n",
"235 k\n",
"236 l\n",
"237 m\n",
"238 n\n",
"239 o\n",
"240 p\n",
"241 q\n",
"242 r\n",
"243 s\n",
"244 t\n",
"245 u\n",
"246 v\n",
"247 w\n",
"248 x\n",
"249 y\n",
"250 z\n",
"251 {\n",
"252 |\n",
"253 }\n",
"254 ~\n",
"255 \u007f\n"
]
}
],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Scope and Life of Automatic Variable, Page number: 225\n"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#Variable declaration\n",
"i = 1 #auto int\n",
"\n",
"def b2 (i):\n",
" print i \n",
"\n",
"def b3 (i):\n",
" print i \n",
"\n",
"\n",
"def b1 (i):\n",
" print i \n",
" b2(i)\n",
" b3(i)\n",
"\n",
"\n",
"#Result\n",
"b1(i)\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"1\n",
"1\n",
"1\n"
]
}
],
"prompt_number": 8
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Scope and Life of Automatic Variable, Page number: 226\n"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#Variable declaration\n",
"i = 1 #auto int\n",
"\n",
"def b2 (i):\n",
" i = 2 # auto int \n",
" print i \n",
"\n",
"def b3 (i):\n",
" i = 3 #auto int \n",
" print i \n",
"\n",
"\n",
"def b1 (i):\n",
" b3(i)\n",
" b2(i)\n",
" print i \n",
"\n",
"\n",
"#Result\n",
"b1(i)\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"3\n",
"2\n",
"1\n"
]
}
],
"prompt_number": 9
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Different Data Types, Page number: 220"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"c = 'a'\n",
"d = 'b'\n",
"print \"%c %c\" %( c, d )\n",
"\n",
"i = 333\n",
"j = 288\n",
"print \"%d %u\"%( i, j ) \n",
"\n",
"\n",
"k = 2\n",
"l = 1\n",
"print \"%d %u \" %(k, l ) \n",
"\n",
"m = 73277727727\n",
"n = 189189819891\n",
"print \"%ld %lu\"%( m, n ) \n",
"\n",
"\n",
"#float, double, long double\n",
"from decimal import Decimal \n",
"#x,y,z = raw_input ( \"float double long double: \").split()\n",
"x = 72.12\n",
"y = Decimal(8282910.0109010)\n",
"z = Decimal(29189999111.128918918)\n",
"print x, y, z "
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"a b\n",
"333 288\n",
"2 1 \n",
"73277727727 189189819891\n",
"72.12 8282910.0109010003507137298583984375 29189999111.128917694091796875\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Auto Increment, Page number: 228"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#function definition \n",
"def increment( ):\n",
" i = 1 #auto int\n",
" print i \n",
" i = i + 1 \n",
"\n",
"#function calls\n",
"increment( )\n",
"increment( )\n",
"increment( )\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"1\n",
"1\n",
"1\n"
]
}
],
"prompt_number": 6
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Static Increment, Page number: 228"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#function definition \n",
"def increment(i = [1]): #static int \n",
" print i[0] \n",
" i[0] += 1 \n",
"\n",
"#function calls\n",
"increment()\n",
"increment()\n",
"increment()\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"1\n",
"2\n",
"3\n"
]
}
],
"prompt_number": 7
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Static Storage Class, Page number: 229"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"\n",
"#function definition\n",
"def fun( ):\n",
" k = 35 \n",
" return (k)\n",
"\n",
"j = fun( ) #function call\n",
"print j #result\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"35\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"External Storage Class, Page number: 231"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#Variable declaration \n",
"i = [0] # external variable\n",
"\n",
"#Function definitions \n",
"def increment(i = [0]):\n",
" i[0] += 1\n",
" print \"on incrementing i = \", i[0] \n",
"\n",
"\n",
"def decrement(i = [2]):\n",
" i[0] -= 1\n",
" print \"on decrementing i = \", i[0] \n",
" \n",
"\n",
"\n",
"print \"i = \", i[0]\n",
"#function calls\n",
"increment() \n",
"increment() \n",
"decrement() \n",
"decrement() "
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"i = 0\n",
"on incrementing i = 1\n",
"on incrementing i = 2\n",
"on decrementing i = 1\n",
"on decrementing i = 0\n"
]
}
],
"prompt_number": 10
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}PK I9B B Let us C/chapter-5.ipynb{
"metadata": {
"name": "",
"signature": "sha256:ff4108fd8429f13da3024f33c6733cc054cfdd0248ac7308786b7df9d4793937"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Chapter 5: Functions & Pointers "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Message Function, Page number: 159"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"# function definition\n",
"def message():\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",
"Cry, and you stop the monotony!\n"
]
}
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Calling More Than One Funnction , Page number: 159"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"# function definitions\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",
"\n",
"print \"I am in main\" \n",
"#function calls\n",
"italy()\n",
"brazil()\n",
"argentina()\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"I am in main\n",
"I am in italy\n",
"I am in brazil\n",
"I am in argentina\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Function Calling Function, Page number: 161"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"# function definitions\n",
"def argentina():\n",
" print \"I am in argentina\" \n",
"\n",
"def brazil():\n",
" print \"I am in brazil\" \n",
" argentina() #function call\n",
"\n",
"def italy():\n",
" print \"I am in italy\" \n",
" brazil() #function call\n",
" print \"I am back in italy\" \n",
" \n",
"print \"I am in main\" \n",
"#function call\n",
"italy()\n",
"print \"I am finally back in main\" "
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"I am in main\n",
"I am in italy\n",
"I am in brazil\n",
"I am in argentina\n",
"I am back in italy\n",
"I am finally back in main\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Calsum Function , Page number: 166"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"# function definition\n",
"def calsum ( x, y, z ):\n",
" d = x + y + z\n",
" return d\n",
"\n",
"#Input from user\n",
"#a,b,c = raw_input (\"Enter any three numbers: \").split()\n",
"print \"Enter any three numbers: \"\n",
"a = 10\n",
"b = 20\n",
"c = 30\n",
"print a,b,c\n",
"\n",
"#function call\n",
"s = calsum(a,b,c) \n",
"\n",
"#Result\n",
"print \"Sum = \", s \n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter any three numbers: \n",
"10 20 30\n",
"Sum = 60\n"
]
}
],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Return Statement , Page number: 169"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"\n",
"# function definition\n",
"def fun():\n",
" #Input from user\n",
" #ch = raw_input (\"Enter any alphabet: \")\n",
" print \"Enter any alphabet: \"\n",
" ch = 'a'\n",
" print ch\n",
" ch = ord(ch)\n",
" if ch >= 65 and ch <= 90:\n",
" return ascii(ch)\n",
" else:\n",
" return ascii( ch + 32 )\n",
"\n",
"#function call\n",
"ch = fun()\n",
"\n",
"#Result\n",
"print ch\n"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Formal Arguments, Page number: 170"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"# function definition\n",
"def fun(b):\n",
" b = 60\n",
" print b \n",
"\n",
"a = 30\n",
"fun(a) #function call\n",
"print a"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"60\n",
"30\n"
]
}
],
"prompt_number": 9
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Scope Rule of Functions , Page number: 171"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"# function definition\n",
"def display(j):\n",
" k = 35\n",
" print j\n",
" print k\n",
"\n",
"i = 20 \n",
"display(i) #function call\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"20\n",
"35\n"
]
}
],
"prompt_number": 10
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Square Function , Page number: 176"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"# function definition\n",
"def square(x):\n",
" y = x * x\n",
" return y\n",
"\n",
"#Input from user\n",
"#a = raw_input(\"Enter any number: \")\n",
"print \"Enter any number: \"\n",
"a = 4.5\n",
"print a\n",
"\n",
"b = square(a) #function call\n",
"\n",
"#Result\n",
"print \"Square of %f is %f\" %( a, b )\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter any number: \n",
"4.5\n",
"Square of 4.500000 is 20.250000\n"
]
}
],
"prompt_number": 11
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Void Function , Page number: 177"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"# function definition\n",
"def gospel(): # void function\n",
" print \"Viruses are electronic bandits...\" \n",
" print \"who eat nuggets of information...\" \n",
" print \"and chunks of bytes...\" \n",
" print \"when you least expect...\" \n",
"\n",
"gospel() # function call"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Viruses are electronic bandits...\n",
"who eat nuggets of information...\n",
"and chunks of bytes...\n",
"when you least expect...\n"
]
}
],
"prompt_number": 12
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Address of a Variable , Page number: 180"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#Variable declaration\n",
"i = 3\n",
"\n",
"#Result\n",
"print \"Address of i = \" , id(i) #printing address\n",
"print \"Value of i = \", i \n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Address of i = 30301288\n",
"Value of i = 3\n"
]
}
],
"prompt_number": 13
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Pointer Relations , Page number: 182"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#Variable declaration\n",
"i = 3 \n",
"j = id(i) # address of variable 'i'\n",
"\n",
"#Result\n",
"print \"Address of i = \", id(i) \n",
"print \"Address of i = \", j \n",
"print \"Address of j = \", id(j)\n",
"print \"Value of j = \", j \n",
"print \"Value of i = \", i \n",
"print \"Value of i = \", i \n",
"print \"Value of i = \", i \n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Address of i = 30301288\n",
"Address of i = 30301288\n",
"Address of j = 134133200\n",
"Value of j = 30301288\n",
"Value of i = 3\n",
"Value of i = 3\n",
"Value of i = 3\n"
]
}
],
"prompt_number": 14
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Pointer to Another Pointer , Page number: 184"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#Variable declaration\n",
"i = 3\n",
"j = id(i) # address of i\n",
"k = id(j) # address of j\n",
"\n",
"#Result\n",
"print \"Address of i = \", id(i) \n",
"print \"Address of i = \", j \n",
"print \"Address of i = \", j \n",
"print \"Address of j = \", id(j) \n",
"print \"Address of j = \", k \n",
"print \"Address of k = \", id(k)\n",
"print \"Value of j = \", j \n",
"print \"Value of k = \", k \n",
"print \"Value of i = \", i \n",
"print \"Value of i = \", i \n",
"print \"Value of i = \", i \n",
"print \"Value of i = \", i "
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Address of i = 30301288\n",
"Address of i = 30301288\n",
"Address of i = 30301288\n",
"Address of j = 134132944\n",
"Address of j = 134132944\n",
"Address of k = 134133200\n",
"Value of j = 30301288\n",
"Value of k = 134132944\n",
"Value of i = 3\n",
"Value of i = 3\n",
"Value of i = 3\n",
"Value of i = 3\n"
]
}
],
"prompt_number": 15
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Call By Value , Page number: 186"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"# function definition\n",
"def swapv (x,y):\n",
" x,y=y,x\n",
" print \"x = %d y = %d\" %( x, y )\n",
"\n",
"#Variable declaration\n",
"a = 10\n",
"b = 20\n",
"\n",
"swapv ( a, b ) # function call\n",
"\n",
"#Result\n",
"print \"a = %d b = %d\" %( a, b )\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"x = 20 y = 10\n",
"a = 10 b = 20\n"
]
}
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Call By Reference , Page number: 187"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"# function definition\n",
"def swapv (a,b):\n",
" a,b=b,a\n",
" return a,b\n",
"\n",
"#Variable declaration\n",
"a = 10\n",
"b = 20\n",
"\n",
"a,b = swapv ( a, b ) # function call\n",
"\n",
"#Result\n",
"print \"a = %d b = %d\" %( a, b )"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"a = 20 b = 10\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Area And Perimeter of a Circle , Page number: 188"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"print \"Enter radius of a circle: \"\n",
"radius = 5\n",
"print radius\n",
"\n",
"#Function definition\n",
"def areaperi ( r ):\n",
" a = 3.14 * r * r\n",
" p = 2 * 3.14 * r \n",
" return a,p\n",
"\n",
"area,perimeter = areaperi ( radius ) #function call\n",
"\n",
"\n",
"#Result\n",
"print \"Area = \", area \n",
"print \"Perimeter = \", perimeter "
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter radius of a circle: \n",
"5\n",
"Area = 78.5\n",
"Perimeter = 31.4\n"
]
}
],
"prompt_number": 18
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Iterative Factorial Function , Page number: 190"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"# function definition\n",
"def factorial(x):\n",
" f = 1\n",
" for i in range(x,0,-1 ):\n",
" f = f * i\n",
" return f\n",
"\n",
"\n",
"print \"Enter any number: \"\n",
"a = 6\n",
"print a\n",
"\n",
"fact = factorial(a) #function call\n",
"\n",
"#Result\n",
"print \"Factorial value = \", fact\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter any number: \n",
"6\n",
"Factorial value = 720\n"
]
}
],
"prompt_number": 19
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Recursive Factorial Function, Page number: 191"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"# function definition\n",
"def rec(x):\n",
" if x == 1:\n",
" return 1\n",
" else:\n",
" f = x * rec ( x - 1 ) # calling the function\n",
" return f\n",
"\n",
"\n",
"print \"Enter any number: \"\n",
"a = 5\n",
"print a\n",
"\n",
"fact = rec(a) #function call\n",
"\n",
"#Result\n",
"print \"Factorial value = \", fact \n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter any number: \n",
"5\n",
"Factorial value = 120\n"
]
}
],
"prompt_number": 20
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Recursion Stack , Page number: 195"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#Variable declaration\n",
"a = 5\n",
"b = 2\n",
"\n",
"#Function definition\n",
"def add ( i, j ):\n",
" s = i + j\n",
" return s\n",
"\n",
"c = add ( a, b ) # function call\n",
"\n",
"#Result\n",
"print \"sum = \", c "
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"sum = 7\n"
]
}
],
"prompt_number": 21
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}PK I/ Let us C/chapter-4.ipynb{
"metadata": {
"name": "",
"signature": "sha256:7c22324d39011030a3b150073070e5711999d5a4173c27c085bd0e0ed1d76d18"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Chapter 4: The Case Control Structure"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Switch - Case, Page number: 137"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#Variable declaration\n",
"i = 2\n",
"\n",
"#Switch case statements\n",
"if i == 1: # case 1\n",
" print \"I am in case 1\"\n",
"else:\n",
" print \"I am in case 2\"# case 2\n",
" print \"I am in case 3\"#case 3\n",
" print \"I am in default\"# default\n",
" \n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"I am in case 2\n",
"I am in case 3\n",
"I am in default\n"
]
}
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Switch - Case, Page number: 138"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#Variable declaration\n",
"i = 2\n",
"\n",
"#Switch case statements\n",
"if i == 1: # case 1\n",
" print \"I am in case 1\"\n",
"elif i == 2: # case 2\n",
" print \"I am in case 2\"\n",
"elif i == 3: #case 3\n",
" print \"I am in case 3\"\n",
"else: # default\n",
" print \"I am in default\""
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"I am in case 2\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The Tips and Traps a), Page number: 140"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#Variable declaration\n",
"i = 22\n",
"\n",
"#Switch case statements\n",
"if i == 121: # case 121\n",
" print \"I am in case 121\"\n",
"elif i == 7: # case 7\n",
" print \"I am in case 7\"\n",
"elif i == 22: #case 22\n",
" print \"I am in case 22\"\n",
"else: # default\n",
" print \"I am in default\""
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"I am in case 22\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The Tips and Traps b), Page number: 140"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#Variable declaration\n",
"c = 'x'\n",
"\n",
"#Switch case statements\n",
"if c == 'v': # case 'v'\n",
" print \"I am in case v\"\n",
"elif c == 'a': # case 'a'\n",
" print \"I am in case a\"\n",
"elif c == 'x': #case 'x'\n",
" print \"I am in case x\"\n",
"else: # default\n",
" print \"I am in default\"\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"I am in case x\n"
]
}
],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The Tips and Traps c), Page number: 141"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"ch = 'a'\n",
"\n",
"#Switch case statements\n",
"if ch == 'a' or ch == 'A' : # case 'a' and case 'A'\n",
" print \"a as in ashar\"\n",
"elif ch == 'b'or ch == 'B': # case 'b' and case 'B'\n",
" print \"b as in brain\"\n",
"elif ch == 'c'or ch == 'C': # case 'c' and case 'C'\n",
" print \"c as in cookie\"\n",
"else: # default\n",
" print (\"wish you knew what are alphabets\")\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"a as in ashar\n"
]
}
],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The Tips and Traps e) , Page number: 143"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"\n",
"#Input from user\n",
"#i = raw_input(\"Enter value of i \")\n",
"i = 1\n",
"\n",
"#Switch case statements\n",
"#print \"Hello\"\n",
"if i == 1 : # case 1\n",
" j = 10\n",
"elif i == 2 :# case 2\n",
" j = 20\n",
"\n"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Goto , Page number: 146"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"goals = 3\n",
"\n",
"if goals <= 5 : #goto\n",
" print \"To err is human!\" #label sos\n",
"else:\n",
" print \"About time soccer players learnt C\" \n",
" print \"and said goodbye! adieu! to soccer\" \n",
" \n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"To err is human!\n"
]
}
],
"prompt_number": 6
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Goto , Page number: 148"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#nested for loops\n",
"for i in range(1,4):\n",
" for j in range(1,4):\n",
" for k in range(1,4):\n",
" if ( i == 3 and j == 3 and k == 3 ): #goto\n",
" print \"Out of the loop at last!\" # label out\n",
" break\n",
" else:\n",
" print i, j, k "
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"1 1 1\n",
"1 1 2\n",
"1 1 3\n",
"1 2 1\n",
"1 2 2\n",
"1 2 3\n",
"1 3 1\n",
"1 3 2\n",
"1 3 3\n",
"2 1 1\n",
"2 1 2\n",
"2 1 3\n",
"2 2 1\n",
"2 2 2\n",
"2 2 3\n",
"2 3 1\n",
"2 3 2\n",
"2 3 3\n",
"3 1 1\n",
"3 1 2\n",
"3 1 3\n",
"3 2 1\n",
"3 2 2\n",
"3 2 3\n",
"3 3 1\n",
"3 3 2\n",
"Out of the loop at last!\n"
]
}
],
"prompt_number": 8
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}PK I5a5 a5 Let us C/chapter-3.ipynb{
"metadata": {
"name": "chapter-3.ipynb"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Chapter 3: The Loop Control Structure "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Simple Interest using While Loop, Page number: 99\n"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#Variable declaration\n",
"count = 1\n",
"pr = [1000,2000,3500]\n",
"yr = [5,5,5]\n",
"intr = [13.5,13.5,3.5]\n",
"\n",
"# while loop\n",
"while count <= 3:\n",
" #Input from the user\n",
" #p,n,r = raw_input(\"Enter values of p, n and r : \").split()\n",
" p = pr[count-1] # principle\n",
" n = yr[count-1] # number of years\n",
" r = intr[count-1]# rate of interest\n",
"\n",
" #Calculation\n",
" si = p * n * r / 100 ; #formula for simple interest\n",
"\n",
" #Result\n",
" print \"Simple interest = Rs.\",si \n",
"\n",
" #Increment count\n",
" count = count + 1\n",
" \n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Simple interest = Rs. 675.0\n",
"Simple interest = Rs. 1350.0\n",
"Simple interest = Rs. 612.5\n"
]
}
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Simple Interest using For Loop , Page number: 109\n"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"pr = [1000,2000,3500]\n",
"yr = [5,5,5]\n",
"intr = [13.5,13.5,3.5]\n",
"\n",
"#for loop\n",
"for count in range(1, 4):\n",
" #Input from the user\n",
" #p,n,r = raw_input(\"Enter values of p, n and r : \").split()\n",
" p = pr[count-1] # principle\n",
" n = yr[count-1] # number of years\n",
" r = intr[count-1]# rate of interest\n",
" \n",
" #Calculation\n",
" si = p * n * r / 100 ; #formula for simple interest\n",
"\n",
" #Result\n",
" print \"Simple interest = Rs.\",si "
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Simple interest = Rs. 675.0\n",
"Simple interest = Rs. 1350.0\n",
"Simple interest = Rs. 612.5\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Nested For Loops , Page number: 114\n"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#nested for loops\n",
"for r in range(1,4): #outer loop\n",
" for c in range(1,3): #inner loop\n",
" s = r + c #find the sum\n",
" print \"r = %d c = %d sum = %d\" % (r, c, s) #Display result\n",
" \n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"r = 1 c = 1 sum = 2\n",
"r = 1 c = 2 sum = 3\n",
"r = 2 c = 1 sum = 3\n",
"r = 2 c = 2 sum = 4\n",
"r = 3 c = 1 sum = 4\n",
"r = 3 c = 2 sum = 5\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Do While Loop , Page number: 116"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#do while loop\n",
"while True:\n",
" #num = raw_input(\"Enter a number: \")\n",
" num = 11\n",
" print \"square of %d is %d\"%(num, num * num )\n",
" print \"Want to enter another number y/n: \" \n",
" another = 'n'\n",
" print another\n",
" if another == 'y':\n",
" continue\n",
" else:\n",
" break\n",
" "
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"square of 11 is 121\n",
"Want to enter another number y/n: \n",
"n\n"
]
}
],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Do While using For Loop, Page number: 117"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#Variable declaration\n",
"another = 'y'\n",
"\n",
"#do while loop\n",
"import sys\n",
"for i in range(1,10000): #infinte loop\n",
" #num = raw_input(\"Enter a number: \")\n",
" num = 11\n",
" print \"square of %d is %d\"%(num, num * num )\n",
" print \"Want to enter another number y/n: \" \n",
" another = 'n'\n",
" print another\n",
" if another == 'y':\n",
" continue\n",
" else:\n",
" break\n",
" \n",
"\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"square of 11 is 121\n",
"Want to enter another number y/n: \n",
"n\n"
]
}
],
"prompt_number": 7
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Do While using While Loop, Page number: 117"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#Variable declaration\n",
"another = 'y'\n",
"\n",
"#do while loop\n",
"while another == 'y':\n",
" #num = raw_input(\"Enter a number: \")\n",
" num = 11\n",
" print \"square of %d is %d\"%(num, num * num )\n",
" print \"Want to enter another number y/n: \" \n",
" another = 'n'\n",
" print another\n",
" \n",
" \n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"square of 11 is 121\n",
"Want to enter another number y/n: \n",
"n\n"
]
}
],
"prompt_number": 8
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Prime Number, Page number: 118\n"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#Input from user\n",
"#num = raw_input(\"Enter a number: \")\n",
"num = 11\n",
"\n",
"#Variable declaration\n",
"i = 2\n",
"\n",
"#while loop\n",
"while i <=(num - 1):\n",
" if num % i == 0:\n",
" print \"Not a prime number\" #Display if not prime number\n",
" break\n",
" i += 1\n",
"\n",
"#Display if prime number\n",
"if i == num:\n",
" print \"Prime number\" "
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Prime number\n"
]
}
],
"prompt_number": 9
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Break Statement , Page number: 119"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#Variable declaration\n",
"i = 1\n",
"j = 1\n",
"\n",
"#while loops\n",
"while i <= 100 : #outer loop\n",
" i = i+1\n",
" while j <= 200 : #inner loop\n",
" j = j+1\n",
" if j == 150:\n",
" break #break statement in inner loop\n",
" else:\n",
" print i, j \n",
" \n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"2 2\n",
"2 3\n",
"2 4\n",
"2 5\n",
"2 6\n",
"2 7\n",
"2 8\n",
"2 9\n",
"2 10\n",
"2 11\n",
"2 12\n",
"2 13\n",
"2 14\n",
"2 15\n",
"2 16\n",
"2 17\n",
"2 18\n",
"2 19\n",
"2 20\n",
"2 21\n",
"2 22\n",
"2 23\n",
"2 24\n",
"2 25\n",
"2 26\n",
"2 27\n",
"2 28\n",
"2 29\n",
"2 30\n",
"2 31\n",
"2 32\n",
"2 33\n",
"2 34\n",
"2 35\n",
"2 36\n",
"2 37\n",
"2 38\n",
"2 39\n",
"2 40\n",
"2 41\n",
"2 42\n",
"2 43\n",
"2 44\n",
"2 45\n",
"2 46\n",
"2 47\n",
"2 48\n",
"2 49\n",
"2 50\n",
"2 51\n",
"2 52\n",
"2 53\n",
"2 54\n",
"2 55\n",
"2 56\n",
"2 57\n",
"2 58\n",
"2 59\n",
"2 60\n",
"2 61\n",
"2 62\n",
"2 63\n",
"2 64\n",
"2 65\n",
"2 66\n",
"2 67\n",
"2 68\n",
"2 69\n",
"2 70\n",
"2 71\n",
"2 72\n",
"2 73\n",
"2 74\n",
"2 75\n",
"2 76\n",
"2 77\n",
"2 78\n",
"2 79\n",
"2 80\n",
"2 81\n",
"2 82\n",
"2 83\n",
"2 84\n",
"2 85\n",
"2 86\n",
"2 87\n",
"2 88\n",
"2 89\n",
"2 90\n",
"2 91\n",
"2 92\n",
"2 93\n",
"2 94\n",
"2 95\n",
"2 96\n",
"2 97\n",
"2 98\n",
"2 99\n",
"2 100\n",
"2 101\n",
"2 102\n",
"2 103\n",
"2 104\n",
"2 105\n",
"2 106\n",
"2 107\n",
"2 108\n",
"2 109\n",
"2 110\n",
"2 111\n",
"2 112\n",
"2 113\n",
"2 114\n",
"2 115\n",
"2 116\n",
"2 117\n",
"2 118\n",
"2 119\n",
"2 120\n",
"2 121\n",
"2 122\n",
"2 123\n",
"2 124\n",
"2 125\n",
"2 126\n",
"2 127\n",
"2 128\n",
"2 129\n",
"2 130\n",
"2 131\n",
"2 132\n",
"2 133\n",
"2 134\n",
"2 135\n",
"2 136\n",
"2 137\n",
"2 138\n",
"2 139\n",
"2 140\n",
"2 141\n",
"2 142\n",
"2 143\n",
"2 144\n",
"2 145\n",
"2 146\n",
"2 147\n",
"2 148\n",
"2 149\n",
"3 151\n",
"3 152\n",
"3 153\n",
"3 154\n",
"3 155\n",
"3 156\n",
"3 157\n",
"3 158\n",
"3 159\n",
"3 160\n",
"3 161\n",
"3 162\n",
"3 163\n",
"3 164\n",
"3 165\n",
"3 166\n",
"3 167\n",
"3 168\n",
"3 169\n",
"3 170\n",
"3 171\n",
"3 172\n",
"3 173\n",
"3 174\n",
"3 175\n",
"3 176\n",
"3 177\n",
"3 178\n",
"3 179\n",
"3 180\n",
"3 181\n",
"3 182\n",
"3 183\n",
"3 184\n",
"3 185\n",
"3 186\n",
"3 187\n",
"3 188\n",
"3 189\n",
"3 190\n",
"3 191\n",
"3 192\n",
"3 193\n",
"3 194\n",
"3 195\n",
"3 196\n",
"3 197\n",
"3 198\n",
"3 199\n",
"3 200\n",
"3 201\n"
]
}
],
"prompt_number": 11
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Continue Statement , Page number: 120\n"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#for loops\n",
"for i in range(1,3):\n",
" for j in range(1,3):\n",
" if i==j :\n",
" continue # continue statement\n",
" print i , j\n",
" \n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"1 2\n",
"2 1\n"
]
}
],
"prompt_number": 12
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}PK I%+4 +4 Let us C/chapter-2.ipynb{
"metadata": {
"name": "",
"signature": "sha256:2995e92c707106426a9edd38d59fc05e81394353c5d1739975459edb98accea9"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Chapter 2: The Decision Control Structure "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If Demo , Page number: 52"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"print \"Enter a number less than 10: \"\n",
"num = 8\n",
"print num\n",
"\n",
"#if statement\n",
"if num <= 10:\n",
" print(\"What an obedient servant you are !\") #display result\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter a number less than 10: \n",
"8\n",
"What an obedient servant you are !\n"
]
}
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Example 2.1 , Page number: 53"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"dis = 0 #Initial Discount (%0)\n",
"\n",
"#Input from the user\n",
"#qty,rate = raw_input(\"Enter quantity and rate: \").split()\n",
"print \"Enter quantity and rate: \"\n",
"qty = 1200 # Quantity of item\n",
"rate = 15.50 # Rate of item (Rs)\n",
"print qty , rate\n",
"\n",
"#discount of 10% if quantity > 1000\n",
"if qty > 1000:\n",
" dis = 10\n",
"\n",
"#Calculation\n",
"tot = (qty * rate) - (qty * rate * dis / 100 ) # total expenses (Rs)\n",
"\n",
"#Result\n",
"print \"Total expenses = Rs. \", tot \n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter quantity and rate: \n",
"1200 15.5\n",
"Total expenses = Rs. 16740.0\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Example 2.2, Page number: 57"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#cy,yoj = raw_input(\"Enter current year and year of joining: \").split() \n",
"print \"Enter current year and year of joining: \"\n",
"cy = 2013 # Current year\n",
"yoj = 1990 # Year of joining\n",
"print cy, yoj \n",
"#Calculation\n",
"yr_of_ser = cy - yoj # number of years of service\n",
"\n",
"#Assign bonus if years of service > 3\n",
"if yr_of_ser > 3:\n",
" bonus = 2500 # Bonus of Rs. 2500\n",
" print \"Bonus = Rs.\", bonus #display result\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter current year and year of joining: \n",
"2013 1990\n",
"Bonus = Rs. 2500\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Example 2.3 , Page number: 58"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"print \"Enter basic salary: \"\n",
"bs = 2561.1 #Basic salary (Rs)\n",
"print bs\n",
"\n",
"#Calculation\n",
"if bs < 1500: # if basic salary is less than Rs.1500\n",
" hra = bs * 10 / 100 # HRA (Rs)\n",
" da = bs * 90 / 100 #DA (Rs)\n",
"else: #if basic salary is greater than or equal to Rs.1500\n",
" hra = 500 # HRA (Rs)\n",
" da = bs * 98 / 100 # DA (Rs)\n",
"\n",
"gs = bs + hra + da # gross salary (Rs)\n",
"\n",
"#Result\n",
"print \"gross salary = Rs. \", gs \n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter basic salary: \n",
"2561.1\n",
"gross salary = Rs. 5570.978\n"
]
}
],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Nested If-else , Page number: 61"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"print \"Enter either 1 or 2: \"\n",
"i = 1\n",
"print i\n",
"\n",
"#nested if-else\n",
"if i == 1 :\n",
" print \"You would go to heaven !\" \n",
"else:\n",
" if i == 2 :\n",
" print \"Hell was created with you in mind\" \n",
" else:\n",
" print \"How about mother earth !\" "
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter either 1 or 2: \n",
"1\n",
"You would go to heaven !\n"
]
}
],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Example 2.4 (Method 1), Page number: 64"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"print \"Enter marks in five subjects: \"\n",
"m1 = 88 #Marks in 1st subject\n",
"m2 = 92 #Marks in 2nd subject\n",
"m3 = 87 #Marks in 3rd subject\n",
"m4 = 66 #Marks in 4th subject\n",
"m5 = 56 #Marks in 5th subject\n",
"print m1,m2,m3,m4,m5\n",
"\n",
"#Calculation\n",
"per = ( m1 + m2 + m3 + m4 + m5 ) / 5 #Percentage\n",
"\n",
"#check for different cases and display appropriate result\n",
"if per >= 60:\n",
" print \"First division\"\n",
"else:\n",
" if per >= 50:\n",
" print \"Second division\"\n",
" else:\n",
" if per >= 40:\n",
" print \"Third division\"\n",
" else:\n",
" print \"Fail\"\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
" Enter marks in five subjects: \n",
"88 92 87 66 56\n",
"First division\n"
]
}
],
"prompt_number": 7
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Example 2.4 (Method 2), Page number: 65"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"print \"Enter marks in five subjects: \"\n",
"m1 = 88 #Marks in 1st subject\n",
"m2 = 92 #Marks in 2nd subject\n",
"m3 = 87 #Marks in 3rd subject\n",
"m4 = 66 #Marks in 4th subject\n",
"m5 = 56 #Marks in 5th subject\n",
"print m1,m2,m3,m4,m5\n",
"\n",
"#Calculation\n",
"per = ( m1 + m2 + m3 + m4 + m5 ) / 5 #Percentage\n",
"\n",
"#check for different cases and display appropriate result\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",
"88 92 87 66 56\n",
"First division\n"
]
}
],
"prompt_number": 8
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Example 2.4 (Method 3), Page number: 67"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"print \"Enter marks in five subjects: \"\n",
"m1 = 88 #Marks in 1st subject\n",
"m2 = 92 #Marks in 2nd subject\n",
"m3 = 87 #Marks in 3rd subject\n",
"m4 = 66 #Marks in 4th subject\n",
"m5 = 56 #Marks in 5th subject\n",
"print m1,m2,m3,m4,m5\n",
"\n",
"#Calculation\n",
"per = ( m1 + m2 + m3 + m4 + m5 ) / 5 #Percentage\n",
"\n",
"#check for different cases and display appropriate result\n",
"if per >= 60:\n",
" print\"First division\"\n",
"elif per >= 50:\n",
" print\"Second division\"\n",
"elif per >= 40:\n",
" print\"Third division\"\n",
"else:\n",
" print\"Fail\"\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter marks in five subjects: \n",
"88 92 87 66 56\n",
"First division\n"
]
}
],
"prompt_number": 9
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Example 2.5 (Method 1) , Page number: 68"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"print \"Enter age, sex, marital status: \"\n",
"age = 43 # Age of driver (years)\n",
"sex = 'M'\n",
"ms = 'M'\n",
"print age,sex,ms\n",
"#check for different cases and display appropriate result\n",
"if ms == 'M':\n",
" print(\"Driver is insured\")\n",
"else:\n",
" if sex == 'M':\n",
" if age > 30:\n",
" print (\"Driver is insured\")\n",
" else:\n",
" print (\"Driver is not insured\")\n",
" else:\n",
" if age > 25:\n",
" print (\"Driver is insured\")\n",
" else:\n",
" print (\"Driver is not insured\")\n",
" \n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter age, sex, marital status: \n",
"43 M M\n",
"Driver is insured\n"
]
}
],
"prompt_number": 11
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Example 2.5 (Method 2) , Page number: 69"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"print \"Enter age, sex, marital status: \"\n",
"age = 43 # Age of driver (years)\n",
"sex = 'M'\n",
"ms = 'M'\n",
"print age,sex,ms\n",
"\n",
"#check for different cases and display appropriate result\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",
"else:\n",
" print\"Driver is not insured\"\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter age, sex, marital status: \n",
"43 M M\n",
"Driver is insured\n"
]
}
],
"prompt_number": 12
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Example 2.6, Page number: 71"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"print \"Enter Gender, Years of Service and Qualifications ( 0 = G, 1 = PG ):\"\n",
"g = 'f'\n",
"yos = 8 # Years of service(years)\n",
"qual = 1 # Qualification ( 0=G, 1=PG)\n",
"print g,yos,qual\n",
"\n",
"# Assign salary depending upon the conditions\n",
"if (g == 'm') and (yos >= 10) and (qual == 1):\n",
" sal = 15000 #salary\n",
"elif ((g == 'm' and yos >= 10 and qual == 0) or ( g == 'm' and yos < 10 and qual == 1 )):\n",
" sal = 10000 #salary\n",
"elif ( g == 'm' and yos < 10 and qual == 0 ):\n",
" sal = 7000 #salary\n",
"elif ( g == 'f' and yos >= 10 and qual == 1 ):\n",
" sal = 12000 #salary\n",
"elif ( g == 'f' and yos >= 10 and qual == 0 ):\n",
" sal = 9000 #salary\n",
"elif ( g == 'f' and yos < 10 and qual == 1 ):\n",
" sal = 10000 #salary\n",
"elif ( g == 'f' and yos < 10 and qual == 0 ):\n",
" sal = 6000 #salary\n",
"\n",
"#Result\n",
"print \"Salary 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",
"f 8 1\n",
"Salary of Employee = 10000\n"
]
}
],
"prompt_number": 13
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}PK I"x` ` Let us C/chapter-1.ipynb{
"metadata": {
"name": "",
"signature": "sha256:67e127b30e1e4aca343f9987e4ae454c0b4254a7be37191ad1b156a6da5403fc"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Chapter 1: Getting Started"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First C Program, Page number: 14\n",
" "
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"p = 1000 #principle\n",
"n = 3 # number of years\n",
"r = 8.5 # rate of interest\n",
"\n",
"#Calculation\n",
"si = p * n * r / 100 ; #formula for simple interest\n",
"\n",
"#Result\n",
"print ( si )\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"255.0\n"
]
}
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Simple Interest, Page number: 21"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"p = 100 # principle\n",
"n = 5 # number of years\n",
"r = 15.5 # rate of interest\n",
"\n",
"#Calculation\n",
"si = p * n * r / 100 ; #formula for simple interest\n",
"\n",
"#Result\n",
"print ( si )\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"77.5\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Just for fun, Page number: 22"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"num = 11\n",
"\n",
"#Result\n",
"print \"Now I am letting you on a secret...\" \n",
"print \"You have just entered the number\", num \n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Now I am letting you on a secret...\n",
"You have just entered the number 11\n"
]
}
],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Example 1.1, Page number: 32"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"i1 = 2 * 3 # operation *\n",
"i2 = i1 / 4 # operation /\n",
"i3 = 4 / 4 # operation /\n",
"i4 = 5 / 8 # operation /\n",
"i5 = i2 + i3 # operation +\n",
"i6 = i5 + 8 # operation +\n",
"i7 = i6 - 2 # operation -\n",
"i8 = i7 + i4 # operation +\n",
"i = i8\n",
"\n",
"#Result\n",
"print \"i = \", i"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"i = 8\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Example 1.2, Page number: 33"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"kk1 = 3 / 2# operation /\n",
"kk2 = kk1 * 4 # operation *\n",
"kk3 = 3 / 8 # operation /\n",
"kk4 = kk2 + kk3 # operation +\n",
"kk5 = kk4 + 3 # operation +\n",
"kk = kk5\n",
"\n",
"#Result\n",
"print \"kk = \", kk\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"kk = 7\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}PK I
m0/ / Let us C/chapter-10.ipynb{
"metadata": {
"name": "",
"signature": "sha256:adc18fea588596d0bfa5f1f11f3697194c0b52d2d9eb299f10c4a99dfea56e31"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Chapter 10: Structures "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Arrays , Page number: 365"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"name = []\n",
"price = []\n",
"pages = []\n",
"\n",
"n =[\"A\",\"C\",\"F\"]\n",
"p=[100.00,256.50,233.70]\n",
"pg=[354,682,512]\n",
"\n",
"print \"Enter names, prices and no. of pages of 3 books \" \n",
"\n",
"for i in range(0,3):\n",
" #n,p,pg = raw_input(\"\").split()\n",
" print n[i],p[i],pg[i]\n",
" name.append(n[i])\n",
" price.append(p[i])\n",
" pages.append(pg[i])\n",
"\n",
"print \"And this is what you entered\" \n",
"\n",
"for i in range(0,3):\n",
" print 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",
"A 100.0 354\n",
"C 256.5 682\n",
"F 233.7 512\n",
"And this is what you entered\n",
"A 100.0 354\n",
"C 256.5 682\n",
"F 233.7 512\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Structure Example , Page number: 366"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"from collections import namedtuple\n",
"#Structure defintion\n",
"struct_book = namedtuple(\"struct_book\", \"name price pages\")\n",
"\n",
"#Input from user\n",
"print \"Enter names, prices & no. of pages of 3 books\" \n",
"#b1n,b1p,b1pg = raw_input(\"\").split()\n",
"#b2n,b2p,b2pg = raw_input(\"\").split()\n",
"#b3n,b3p,b3pg = raw_input(\"\").split()\n",
"n =[\"A\",\"C\",\"F\"]\n",
"p=[100.00,256.50,233.70]\n",
"pg=[354,682,512]\n",
"print n[0],p[0],pg[0]\n",
"print n[1],p[1],pg[1]\n",
"print n[2],p[2],pg[2]\n",
"#Structures for 3 books\n",
"b1 = struct_book(n[0], p[0], pg[0])\n",
"b2 = struct_book(n[1], p[1], pg[1])\n",
"b3 = struct_book(n[2], p[2], pg[2])\n",
"\n",
"\n",
"#Result\n",
"print \"And this is what you entered\" \n",
"print b1.name, b1.price, b1.pages \n",
"print b2.name, b2.price, b2.pages \n",
"print b3.name, b3.price, b3.pages \n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter names, prices & no. of pages of 3 books\n",
"A 100.0 354\n",
"C 256.5 682\n",
"F 233.7 512\n",
"And this is what you entered\n",
"A 100.0 354\n",
"C 256.5 682\n",
"F 233.7 512\n"
]
}
],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Memory Map of Structures , Page number: 370"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"from collections import namedtuple\n",
"#Structure defintion\n",
"struct_book = namedtuple(\"struct_book\", \"name price pages\")\n",
"\n",
"#Structures for a book\n",
"b1 = struct_book('B', 130.00, 550)\n",
"\n",
"#Result\n",
"print \"Address of name = \", id(b1.name )\n",
"print \"Address of price = \", id(b1.price )\n",
"print \"Address of pages = \", id(b1.pages )"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Address of name = 31603728\n",
"Address of price = 108997488\n",
"Address of pages = 133808864\n"
]
}
],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Array of Structures , Page number: 371"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"from collections import namedtuple\n",
"#Structure defintion\n",
"struct_book = namedtuple(\"struct_book\", \"name price pages\")\n",
"\n",
"#Array of structures\n",
"b =[]\n",
"\n",
"n =[\"A\",\"C\",\"F\"]\n",
"p=[100.00,256.50,233.70]\n",
"pg=[354,682,512]\n",
"\n",
"#Storing data in the array\n",
"for i in range(0,3):\n",
" #bn, bp, bpg =raw_input( \"Enter name, price and pages: \" ).split()\n",
" print \"Enter name, price and pages: \"\n",
" print n[i],p[i],pg[i]\n",
" b.append(struct_book(n[i], p[i], pg[i]))\n",
"\n",
"#Result\n",
"for i in range(0,3):\n",
" print b[i].name, b[i].price, b[i].pages \n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter name, price and pages: \n",
"A 100.0 354\n",
"Enter name, price and pages: \n",
"C 256.5 682\n",
"Enter name, price and pages: \n",
"F 233.7 512\n",
"A 100.0 354\n",
"C 256.5 682\n",
"F 233.7 512\n"
]
}
],
"prompt_number": 6
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Copying Structures , Page number: 374"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"\n",
"from collections import namedtuple\n",
"#Structure defintion\n",
"struct_employee = namedtuple(\"struct_employee\", \"name age salary\")\n",
"\n",
"#Structures for 3 employees\n",
"e1 = struct_employee(\"Sanjay\", 30, 5500.50)\n",
"e2 = struct_employee(\" \",0,0)\n",
"e3 = struct_employee(\" \",0,0)\n",
"\n",
"#piece-meal copying \n",
"import copy\n",
"e2 = e2._replace(name = e1.name)\n",
"e2 = e2._replace(age = e1.age)\n",
"e2 = e2._replace(salary = e1.salary)\n",
"\n",
"\n",
"#copying all elements at one go \n",
"e3 = e2\n",
"\n",
"#Result\n",
"print e1.name, e1.age, e1.salary \n",
"print e2.name, e2.age, e2.salary \n",
"print e3.name, e3.age, e3.salary \n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Sanjay 30 5500.5\n",
"Sanjay 30 5500.5\n",
"Sanjay 30 5500.5\n"
]
}
],
"prompt_number": 7
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Nested Structures , Page number: 375"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"from collections import namedtuple\n",
"#Structure defintions\n",
"struct_address = namedtuple(\"struct_address\", \"phone city pin\")\n",
"struct_emp = namedtuple(\"struct_emp\", \"name struct_address\") #nested structures\n",
"\n",
"#Structures for employee\n",
"a = struct_address(\"531046\", \"nagpur\", 10)\n",
"e = struct_emp(\"jeru\",a) #nested structure\n",
"\n",
"#Result\n",
"print \"name = %s phone = %s\" %( e.name, e.struct_address.phone )\n",
"print \"city = %s pin = %d\" %( e.struct_address.city, e.struct_address.pin )\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"name = jeru phone = 531046\n",
"city = nagpur pin = 10\n"
]
}
],
"prompt_number": 8
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Passing Individual Structure Elements to Functions, Page number: 377"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"from collections import namedtuple\n",
"#Structure defintions\n",
"struct_book = namedtuple(\"struct_book\", \"name author callno\")\n",
"\n",
"#Function definition\n",
"def display (s,t,n):\n",
" print s, t, n \n",
" \n",
"#Structures for book\n",
"b1 = struct_book(\"Let us C\", \"YPK\", 101)\n",
"\n",
"\n",
"display ( b1.name, b1.author, b1.callno ) #function call\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Let us C YPK 101\n"
]
}
],
"prompt_number": 9
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Passing Structure to a Function , Page number: 378"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"from collections import namedtuple\n",
"#Structure defintions\n",
"struct_book = namedtuple(\"struct_book\", \"name author callno\")\n",
"\n",
"#Function definition\n",
"def display (b):\n",
" print b.name, b.author, b.callno \n",
" \n",
"#Structures for book\n",
"b1 = struct_book(\"Let us C\", \"YPK\", 101)\n",
"\n",
"\n",
"display ( b1) #function call\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Let us C YPK 101\n"
]
}
],
"prompt_number": 10
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Structure Pointers , Page number: 379"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"from collections import namedtuple\n",
"#Structure defintions\n",
"struct_book = namedtuple(\"struct_book\", \"name author callno\")\n",
"\n",
"#Structure for book\n",
"b1 = struct_book(\"Let us C\", \"YPK\", 101)\n",
"ptr = id(b1) #structure pointer\n",
"\n",
"#Result\n",
"print b1.name, b1.author, b1.callno \n",
"print b1.name, b1.author, b1.callno \n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Let us C YPK 101\n",
"Let us C YPK 101\n"
]
}
],
"prompt_number": 11
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Passing Address of a Structure Variable , Page number: 380"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"#function definition\n",
"def display (b):\n",
" print b.name, b.author, b.callno \n",
" \n",
"from collections import namedtuple\n",
"#Structure defintions\n",
"struct_book = namedtuple(\"struct_book\", \"name author callno\")\n",
"\n",
"#Structure for book\n",
"b1 = struct_book(\"Let us C\", \"YPK\", 101)\n",
"\n",
"#function call\n",
"display ( b1 ) \n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Let us C YPK 101\n"
]
}
],
"prompt_number": 12
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}PK I2 Let us C/chapter-11.ipynb{
"metadata": {
"name": "",
"signature": "sha256:2f4d0660b7234129c8e49912c5b11a0b5ef4cb891c3d12ca4bfa31d6ee648bf9"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Chapter 11: Console Input/Output"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Printf Example, Page number: 397"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"avg = 346 \n",
"per = 69.2\n",
"\n",
"#Result\n",
"print \"Average = %d\\nPercentage = %f\" %(avg, per )\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Average = 346\n",
"Percentage = 69.200000\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Format Specifications, Page number: 399"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"weight = 63\n",
"\n",
"#Result\n",
"print \"weight is %d kg\" %( weight ) \n",
"print \"weight is %2d kg\"%( weight ) \n",
"print \"weight is %4d kg\" %( weight )\n",
"print \"weight is %6d kg\" %(weight ) \n",
"print \"weight is %-6d kg\" %( weight )\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"weight is 63 kg\n",
"weight is 63 kg\n",
"weight is 63 kg\n",
"weight is 63 kg\n",
"weight is 63 kg\n"
]
}
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"String Format Specifiers, Page number: 400"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"firstname1 = \"Sandy\" \n",
"surname1 = \"Malya\" \n",
"firstname2 = \"AjayKumar\" \n",
"surname2 = \"Gurubaxani\" \n",
"\n",
"#Result\n",
"print \"%20s%20s\" %( firstname1, surname1 )\n",
"print \"%20s%20s\" %(firstname2, surname2 ) \n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
" Sandy Malya\n",
" AjayKumar Gurubaxani\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Escape Sequences, Page number: 401"
]
},
{
"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"
]
}
],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Format Conversions, Page number: 403"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"ch = 'z' \n",
"i = 125 \n",
"a = 12.55 \n",
"s = \"hello there !\"\n",
"\n",
"#Result\n",
"print \"%c %d %f\" %( ch, ord(ch), ord(ch )) \n",
"print \"%s %d %f\"%( s, ord(s[1]), ord(s[1]) )\n",
"print \"%c %d %f\"%(i ,i, i ) \n",
"print \"%f %d\\n\"%( a, a )\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"z 122 122.000000\n",
"hello there ! 101 101.000000\n",
"} 125 125.000000\n",
"12.550000 12\n",
"\n"
]
}
],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Sprintf Function, Page number: 404"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"i = 10 \n",
"ch = 'A'\n",
"a = 3.14 \n",
"\n",
"\n",
"#Result\n",
"print \"%d %c %f\" %(i, ch, a ) \n",
"str = \"%d %c %f\" %(i, ch, a ) #sprintf\n",
"print \"%s\" %(str )\n",
"\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"10 A 3.140000\n",
"10 A 3.140000\n"
]
}
],
"prompt_number": 6
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Unformatted Input, Page number: 406\n"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"import msvcrt\n",
"\n",
"print \"Press any key to continue\" \n",
"#msvcrt.getch( ) # will not echo the character \n",
"print \"Type any character\" \n",
"#ch = msvcrt.getche( ) # will echo the character typed \n",
"ch = 'A'\n",
"print ch\n",
"#ch = input(\"Type any character\")#getchar( ) will echo character, must be followed by enter key \n",
"print \"Type any character\"\n",
"ch = 8\n",
"print ch\n",
"#ch = input( \"Continue Y/N\" ) #fgetchar( ) will echo character, must be followed by enter key\n",
"print \"Continue Y/N\" \n",
"ch = 'N'\n",
"print ch\n",
"\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Press any key to continue\n",
"Type any character\n",
"A\n",
"Type any character\n",
"8\n",
"Continue Y/N\n",
"N\n"
]
}
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Unformatted Output, Page number: 407"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"ch = 'A'\n",
"\n",
"#Result\n",
"print ch #putch\n",
"print ch #putchar\n",
"print ch #fputchar\n",
"print 'Z' #putch\n",
"print 'Z' #putchar\n",
"print 'Z' #fputchar\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"A\n",
"A\n",
"A\n",
"Z\n",
"Z\n",
"Z\n"
]
}
],
"prompt_number": 7
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Unformatted String Input/Output, Page number: 408\n"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"print \"Enter name\"\n",
"footballer = \"Jonty Rhodes\"\n",
"print footballer\n",
"\n",
"#Result\n",
"print \"Happy footballing!\" \n",
"print footballer "
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter name\n",
"Jonty Rhodes\n",
"Happy footballing!\n",
"Jonty Rhodes\n"
]
}
],
"prompt_number": 9
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}PK IeZ Let us C/chapter-12.ipynb{
"metadata": {
"name": "chapter-12.ipynb"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Chapter 12: File Input/Output "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Display Contents of a File , Page number: 417