"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# lowercase to uppercase text conversion using FOR loop\n",
"\n",
"\n",
"letter=\"Fourscore and seven years ago aou fathers brought forth...\"\n",
"letter=list(letter)\n",
"n=len(letter)\n",
"\n",
"for count in range(0,n):\n",
" \n",
" letter[count]=letter[count].upper()\n",
"\n",
"letter=''.join(letter)\n",
"print letter\n",
"\n",
" \n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"FOURSCORE AND SEVEN YEARS AGO AOU FATHERS BROUGHT FORTH...\n"
]
}
],
"prompt_number": 8
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.17, Page number: 6.17
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Averaging a List of Numbers using an FOR loop\n",
"\n",
"\n",
"n=6\n",
"Sum=0.0\n",
"\n",
"for count in range(1,n+1):\n",
" Sum=Sum+count\n",
" \n",
"average=Sum/n\n",
"print \"the average is \",average\n",
"\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"the average is 3.5\n"
]
}
],
"prompt_number": 10
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.18, Page number: 6.19
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Repeated averaging of a list of numbers\n",
"\n",
"\n",
"\n",
"numbers=[[1.5,2.5,6.2,3.0],[4,-2,7],[5.4,8.0,2.2,1.7,-3.9]]\n",
"loopcount=3\n",
"loop=0\n",
"\n",
"while loopExample 6.19, Page number: 6.20
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# coverting several lines of text to upper case\n",
"\n",
"text=['Now is the time for all good men to come to aid..','Fourscore and seven years ago our fathers brought forth...','*']\n",
"\n",
"\n",
"loop=0\n",
"\n",
"while True:\n",
" letter=text[loop]\n",
" if letter=='*':\n",
" print \"\\nGood bye\"\n",
" break\n",
"\n",
" print '\\n',letter\n",
" letter=list(letter)\n",
" n=len(letter)\n",
"\n",
" for count in range(0,n):\n",
" letter[count]=letter[count].upper()\n",
"\n",
" letter=''.join(letter)\n",
" print letter\n",
" loop+=1\n",
" \n",
"\n",
"\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Now is the time for all good men to come to aid..\n",
"NOW IS THE TIME FOR ALL GOOD MEN TO COME TO AID..\n",
"\n",
"Fourscore and seven years ago our fathers brought forth...\n",
"FOURSCORE AND SEVEN YEARS AGO OUR FATHERS BROUGHT FORTH...\n",
"\n",
"Good bye\n"
]
}
],
"prompt_number": 6
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Calculating Depreciation\n",
"\n",
"choice=0\n",
"\n",
"def main(choice,val,n):\n",
" \n",
" if choice==1:\n",
"\n",
" print \"Straight Line Method\\n\\n\"\n",
" val=float(val)\n",
" deprec=val/n\n",
" for year in range(1,n+1):\n",
" val=val-deprec\n",
" print \"End of year %2d Depreciation: %7.2f Current value: %8.2f\"\\\n",
" %(year,deprec,val)\n",
"\n",
"\n",
" elif choice==2:\n",
" print \"Double Declining Balance Method\\n\\n\"\n",
" val=float(val)\n",
" for year in range(1,n+1):\n",
" deprec=2*val/n\n",
" val=val-deprec\n",
" print \"End of year %2d Depreciation: %7.2f Current value: %8.2f\"\\\n",
" %(year,deprec,val)\n",
"\n",
" elif choice==3:\n",
" print \"Sum of the years' -Digit Method\\n\\n\"\n",
" val=float(val)\n",
" tag=val\n",
"\n",
" for year in range(1,n+1):\n",
" deprec=(n-year+1)*tag/(n*(n+1)/2)\n",
" val=val-deprec\n",
" print \"End of year %2d Depreciation: %7.2f Current value: %8.2f\"\\\n",
" %(year,deprec,val)\n",
"\n",
" else:\n",
" print \"incorrect data entry...\"\n",
"\n",
" return\n",
"\n",
"print \"\\n Method: (1-SSL 2-DDB 3-SYD) \"\n",
"print \"choice: 1 \\nval: 8000 \\nNumber of years: 10\"\n",
"main(1,8000,10)\n",
"print \"\\n Method: (1-SSL 2-DDB 3-SYD) \"\n",
"print \"choice: 2 \\nval: 8000 \\nNumber of years: 10\"\n",
"main(2,8000,10)\n",
"print \"\\n Method: (1-SSL 2-DDB 3-SYD) \"\n",
"print \"choice: 3 \\nval: 8000 \\nNumber of years: 10\"\n",
"main(3,8000,10)\n",
"print \"\\n Method: (1-SSL 2-DDB 3-SYD) \"\n",
"print \"choice: 5 \\nval: 8000 \\nNumber of years: 10\"\n",
"main(5,8000,10)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
" Method: (1-SSL 2-DDB 3-SYD) \n",
"choice: 1 \n",
"val: 8000 \n",
"Number of years: 10\n",
"Straight Line Method\n",
"\n",
"\n",
"End of year 1 Depreciation: 800.00 Current value: 7200.00\n",
"End of year 2 Depreciation: 800.00 Current value: 6400.00\n",
"End of year 3 Depreciation: 800.00 Current value: 5600.00\n",
"End of year 4 Depreciation: 800.00 Current value: 4800.00\n",
"End of year 5 Depreciation: 800.00 Current value: 4000.00\n",
"End of year 6 Depreciation: 800.00 Current value: 3200.00\n",
"End of year 7 Depreciation: 800.00 Current value: 2400.00\n",
"End of year 8 Depreciation: 800.00 Current value: 1600.00\n",
"End of year 9 Depreciation: 800.00 Current value: 800.00\n",
"End of year 10 Depreciation: 800.00 Current value: 0.00\n",
"\n",
" Method: (1-SSL 2-DDB 3-SYD) \n",
"choice: 2 \n",
"val: 8000 \n",
"Number of years: 10\n",
"Double Declining Balance Method\n",
"\n",
"\n",
"End of year 1 Depreciation: 1600.00 Current value: 6400.00\n",
"End of year 2 Depreciation: 1280.00 Current value: 5120.00\n",
"End of year 3 Depreciation: 1024.00 Current value: 4096.00\n",
"End of year 4 Depreciation: 819.20 Current value: 3276.80\n",
"End of year 5 Depreciation: 655.36 Current value: 2621.44\n",
"End of year 6 Depreciation: 524.29 Current value: 2097.15\n",
"End of year 7 Depreciation: 419.43 Current value: 1677.72\n",
"End of year 8 Depreciation: 335.54 Current value: 1342.18\n",
"End of year 9 Depreciation: 268.44 Current value: 1073.74\n",
"End of year 10 Depreciation: 214.75 Current value: 858.99\n",
"\n",
" Method: (1-SSL 2-DDB 3-SYD) \n",
"choice: 3 \n",
"val: 8000 \n",
"Number of years: 10\n",
"Sum of the years' -Digit Method\n",
"\n",
"\n",
"End of year 1 Depreciation: 1454.55 Current value: 6545.45\n",
"End of year 2 Depreciation: 1309.09 Current value: 5236.36\n",
"End of year 3 Depreciation: 1163.64 Current value: 4072.73\n",
"End of year 4 Depreciation: 1018.18 Current value: 3054.55\n",
"End of year 5 Depreciation: 872.73 Current value: 2181.82\n",
"End of year 6 Depreciation: 727.27 Current value: 1454.55\n",
"End of year 7 Depreciation: 581.82 Current value: 872.73\n",
"End of year 8 Depreciation: 436.36 Current value: 436.36\n",
"End of year 9 Depreciation: 290.91 Current value: 145.45\n",
"End of year 10 Depreciation: 145.45 Current value: 0.00\n",
"\n",
" Method: (1-SSL 2-DDB 3-SYD) \n",
"choice: 5 \n",
"val: 8000 \n",
"Number of years: 10\n",
"incorrect data entry...\n"
]
}
],
"prompt_number": 23
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.31, Page number: 6.41
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Averaging list of Nonnegative Numbers\n",
"\n",
"navg=0\n",
"Sum=0.0\n",
"n=6\n",
"\n",
"for count in range(-6,n+1):\n",
" x=count\n",
" if(x<0):\n",
" continue\n",
" Sum=Sum+x\n",
" navg=navg+1\n",
"\n",
"\n",
"average=Sum/navg\n",
"print \"The average is \",average\n",
"\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"The average is 3.0\n"
]
}
],
"prompt_number": 24
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.32, Page number: 6.42
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Searching for Palindromes\n",
"\n",
"\n",
"\n",
"def main(letter):\n",
" flag=True\n",
"\n",
" letter=list(letter)\n",
"\n",
" tag=len(letter)-1\n",
" countback=tag\n",
"\n",
" for count in range(0,(tag/2)+1):\n",
" if letter[count]!=letter[countback]:\n",
" flag=False\n",
" break\n",
" countback=countback-1\n",
"\n",
" letter=''.join(letter)\n",
" if flag:\n",
" print \"\\n %s IS a palindrome\" %letter\n",
" else:\n",
" print \"\\n %s is NOT a palindrome\" %letter\n",
" \n",
" return\n",
"\n",
"main('TOOT')\n",
"main('FALSE')\n",
"main('PULLUP')\n",
"main('ABLE WAS I ERE I SAW ELBA')\n",
"\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
" TOOT IS a palindrome\n",
"\n",
" FALSE is NOT a palindrome\n",
"\n",
" PULLUP IS a palindrome\n",
"\n",
" ABLE WAS I ERE I SAW ELBA IS a palindrome\n"
]
}
],
"prompt_number": 25
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 6.34, Page number: 6.47
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# coverting several lines of text to upper case\n",
"\n",
"text=['Now is the time for all good men to come to aid..','Fourscore and seven years ago our fathers brought forth.$$..','*']\n",
"\n",
"\n",
"loop,flag=0,False\n",
"\n",
"while True:\n",
" letter=text[loop]\n",
" if letter=='*' or flag:\n",
" print \"\\nGood bye\"\n",
" break\n",
"\n",
" print '\\n',letter\n",
" letter=list(letter)\n",
" n=len(letter)\n",
"\n",
" for count in range(0,n):\n",
" letter[count]=letter[count].upper()\n",
"\n",
" letter2=''.join(letter)\n",
" print letter2\n",
"\n",
" for count in range(0,n-1):\n",
" if letter[count]=='$' and letter[count+1]=='$':\n",
" print 'BREAK CONDITION DETECTED - TERMINATE EXECUTION'\n",
" flag=True\n",
" break\n",
" \n",
" loop+=1\n",
" \n",
"\n",
"\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Now is the time for all good men to come to aid..\n",
"NOW IS THE TIME FOR ALL GOOD MEN TO COME TO AID..\n",
"\n",
"Fourscore and seven years ago our fathers brought forth.$$..\n",
"FOURSCORE AND SEVEN YEARS AGO OUR FATHERS BROUGHT FORTH.$$..\n",
"BREAK CONDITION DETECTED - TERMINATE EXECUTION\n",
"\n",
"Good bye\n"
]
}
],
"prompt_number": 7
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}