"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"letter='heavenly feeling'\n",
"letter=list(letter)\n",
"\n",
"for count in letter:\n",
" print count.upper(),\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"H E A V E N L Y F E E L I N G\n"
]
}
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 9.8, Page number: 9.6
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"\n",
"Sum=0.0\n",
"List=[]\n",
"n=5\n",
"for count in range(0,n):\n",
" x=count+1\n",
" print \"\\ni = %d x = %d\" %(count+1,x),\n",
" List.append(count)\n",
" Sum+=List[count]\n",
"\n",
"avg=Sum/n\n",
"print \"\\n\\nThe average is %5.2f\\n\\n\" %avg\n",
"for count in range(0,n):\n",
" d=List[count]-avg\n",
" print \"i=%d x=%5.2f d=%5.2f\" %(count+1,List[count],d)\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"i = 1 x = 1 \n",
"i = 2 x = 2 \n",
"i = 3 x = 3 \n",
"i = 4 x = 4 \n",
"i = 5 x = 5 \n",
"\n",
"The average is 2.00\n",
"\n",
"\n",
"i=1 x= 0.00 d=-2.00\n",
"i=2 x= 1.00 d=-1.00\n",
"i=3 x= 2.00 d= 0.00\n",
"i=4 x= 3.00 d= 1.00\n",
"i=5 x= 4.00 d= 2.00\n"
]
}
],
"prompt_number": 6
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"def modify(a):\n",
" print \"From the function after modifying the values: \"\n",
"\n",
" for count in range(0,3):\n",
" a[count]=-9\n",
" print \"a[%d] = %d \" %(count,a[count])\n",
"\n",
" return\n",
"\n",
"a=[]\n",
"print \"From main, before calling the function: \"\n",
"for count in range(0,3):\n",
" a.append(count+1)\n",
" print \"a[%d] = %d \" %(count,a[count])\n",
"\n",
"modify(a)\n",
"print \"From the main, after calling the function: \"\n",
"for count in range(0,3):\n",
" print \"a[%d] = %d \" %(count,a[count])\n",
"\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"From main, before calling the function: \n",
"a[0] = 1 \n",
"a[1] = 2 \n",
"a[2] = 3 \n",
"From the function after modifying the values: \n",
"a[0] = -9 \n",
"a[1] = -9 \n",
"a[2] = -9 \n",
"From the main, after calling the function: \n",
"a[0] = -9 \n",
"a[1] = -9 \n",
"a[2] = -9 \n"
]
}
],
"prompt_number": 8
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 9.12, Page number: 9.12
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"\n",
"a=1\n",
"def modify(b,c):\n",
"\n",
" print \"From the function, after modifying the value : \"\n",
" global a\n",
" a=-999\n",
" b=-999\n",
" print \"a = %d b = %d\" %(a,b)\n",
" for count in range(0,3):\n",
" c[count]=-9\n",
" print \"c[%d] = %d\" %(count,c[count])\n",
"\n",
" return\n",
"\n",
"\n",
"\n",
"b=2\n",
"c=[]\n",
"\n",
"print \"From main, before calling the function: \"\n",
"print \"a = %d b = %d\" %(a,b)\n",
"\n",
"for count in range(0,3):\n",
" c.append(10*(count+1))\n",
" print \"c[%d] = %d\" %(count,c[count])\n",
"\n",
"modify(b,c)\n",
"print \"From main, after calling the function:\"\n",
"print \"a = %d b = %d\" %(a,b)\n",
"\n",
"for count in range(0,3):\n",
" print \"c[%d] = %d\" %(count,c[count])\n",
"\n",
"\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"From main, before calling the function: \n",
"a = 1 b = 2\n",
"c[0] = 10\n",
"c[1] = 20\n",
"c[2] = 30\n",
"From the function, after modifying the value : \n",
"a = -999 b = -999\n",
"c[0] = -9\n",
"c[1] = -9\n",
"c[2] = -9\n",
"From main, after calling the function:\n",
"a = -999 b = 2\n",
"c[0] = -9\n",
"c[1] = -9\n",
"c[2] = -9\n"
]
}
],
"prompt_number": 9
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 9.13, Page number: 9.13
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"\n",
"def reorder(n,x):\n",
" for item in range(0,n-1):\n",
" for i in range(item+1,n):\n",
" if x[i]Example 9.14, Page number: 9.16
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"def countwords(english):\n",
" words=1\n",
" for count in range(0,len(english)-1):\n",
" if english[count]==' ' and english[count+1]!=' ':\n",
" words+=1\n",
" \n",
" return words\n",
"\n",
"def convert(words,english,piglatin):\n",
" m1=0\n",
" for n in range(1,words+1):\n",
" count=m1\n",
" while english[count]!=' ':\n",
" m2=count\n",
" count+=1\n",
"\n",
" for count in range(m1,m2):\n",
" piglatin.append(english[count+1])\n",
" piglatin.append(english[m1])\n",
" piglatin.append('a')\n",
" piglatin.append(' ')\n",
"\n",
" m1=m2+2\n",
"\n",
" return\n",
"\n",
"def writeoutput(piglatin):\n",
" piglatin=''.join(piglatin)\n",
" print piglatin\n",
" return\n",
"\n",
"def main(english):\n",
" english=list(english)\n",
" piglatin=[]\n",
" english.append(' ')\n",
"\n",
" words=countwords(english)\n",
" convert(words,english,piglatin)\n",
" writeoutput(piglatin)\n",
" \n",
" return\n",
"\n",
"print '\\nC is a popular structured programming language'\n",
"main('C is a popular structured programming language')\n",
"print '\\nbaseball is the great American pastime.'\n",
"main('baseball is the great American pastime.')\n",
"print '\\nthough there are many who prefer football'\n",
"main('though there are many who prefer football')\n",
"print '\\nplease do not sneeza in the computer room'\n",
"main('please do not sneeza in the computer room')\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"C is a popular structured programming language\n",
"Ca sia aa opularpa tructuredsa rogrammingpa anguagela \n",
"\n",
"baseball is the great American pastime.\n",
"aseballba sia heta reatga mericanAa astime.pa \n",
"\n",
"though there are many who prefer football\n",
"houghta hereta reaa anyma howa referpa ootballfa \n",
"\n",
"please do not sneeza in the computer room\n",
"leasepa oda otna neezasa nia heta omputerca oomra \n"
]
}
],
"prompt_number": 24
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"