summaryrefslogtreecommitdiff
path: root/C++_By_Example/Chapter4.ipynb
diff options
context:
space:
mode:
authorhardythe12015-04-07 15:58:05 +0530
committerhardythe12015-04-07 15:58:05 +0530
commit92cca121f959c6616e3da431c1e2d23c4fa5e886 (patch)
tree205e68d0ce598ac5caca7de839a2934d746cce86 /C++_By_Example/Chapter4.ipynb
parentb14c13fcc6bb6d01c468805d612acb353ec168ac (diff)
downloadPython-Textbook-Companions-92cca121f959c6616e3da431c1e2d23c4fa5e886.tar.gz
Python-Textbook-Companions-92cca121f959c6616e3da431c1e2d23c4fa5e886.tar.bz2
Python-Textbook-Companions-92cca121f959c6616e3da431c1e2d23c4fa5e886.zip
added books
Diffstat (limited to 'C++_By_Example/Chapter4.ipynb')
-rwxr-xr-xC++_By_Example/Chapter4.ipynb1123
1 files changed, 1123 insertions, 0 deletions
diff --git a/C++_By_Example/Chapter4.ipynb b/C++_By_Example/Chapter4.ipynb
new file mode 100755
index 00000000..1c0cecfc
--- /dev/null
+++ b/C++_By_Example/Chapter4.ipynb
@@ -0,0 +1,1123 @@
+{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:fc5913d17c154f422ddcc1d489a3c0e7639fda20decc6c699e5ba0954066b17c"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter4, Variable Scope and Modular Programming"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C16FUN1, Page number:338"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Function Definition\n",
+ "def next_fun(): \n",
+ " print \"Inside next_fun()\"\n",
+ " \n",
+ "def third_fun(): \n",
+ " print \"Inside third_fun()\"\n",
+ "\n",
+ "def main(): \n",
+ " print \"First function called main()\"\n",
+ " #Function Call\n",
+ " next_fun() \n",
+ " third_fun() \n",
+ " print \"main() is completed\"\n",
+ "\n",
+ "#Function Call\n",
+ "main() "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "First function called main()\n",
+ "Inside next_fun()\n",
+ "Inside third_fun()\n",
+ "main() is completed\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C16FUN2, Page number:347"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Function Calls\n",
+ "def name_print():\n",
+ " print \"C++ is Fun!\\tC++ is Fun!\\tC++ is Fun!\"\n",
+ " print \" C++ i s F u n ! \\t C++ i s F u n ! \\t C++ i s F u n ! \"\n",
+ " reverse_print() \n",
+ " \n",
+ "def reverse_print():\n",
+ " print \"!nuF si ++C\\t!nuF si ++C\\t!nuF si ++C\"\n",
+ "\n",
+ "def one_per_line():\n",
+ " print \"C++\\n i\\n s\\n F\\n u\\n n\\n !\"\n",
+ "\n",
+ "\n",
+ "def main():\n",
+ " for ctr in range(1,6,1):\n",
+ " name_print() #Calls function five times.\n",
+ " one_per_line() #Calls the program's last function once \n",
+ "main()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "C++ is Fun!\tC++ is Fun!\tC++ is Fun!\n",
+ " C++ i s F u n ! \t C++ i s F u n ! \t C++ i s F u n ! \n",
+ "!nuF si ++C\t!nuF si ++C\t!nuF si ++C\n",
+ "C++ is Fun!\tC++ is Fun!\tC++ is Fun!\n",
+ " C++ i s F u n ! \t C++ i s F u n ! \t C++ i s F u n ! \n",
+ "!nuF si ++C\t!nuF si ++C\t!nuF si ++C\n",
+ "C++ is Fun!\tC++ is Fun!\tC++ is Fun!\n",
+ " C++ i s F u n ! \t C++ i s F u n ! \t C++ i s F u n ! \n",
+ "!nuF si ++C\t!nuF si ++C\t!nuF si ++C\n",
+ "C++ is Fun!\tC++ is Fun!\tC++ is Fun!\n",
+ " C++ i s F u n ! \t C++ i s F u n ! \t C++ i s F u n ! \n",
+ "!nuF si ++C\t!nuF si ++C\t!nuF si ++C\n",
+ "C++ is Fun!\tC++ is Fun!\tC++ is Fun!\n",
+ " C++ i s F u n ! \t C++ i s F u n ! \t C++ i s F u n ! \n",
+ "!nuF si ++C\t!nuF si ++C\t!nuF si ++C\n",
+ "C++\n",
+ " i\n",
+ " s\n",
+ " F\n",
+ " u\n",
+ " n\n",
+ " !\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C17GLO, Page number:356"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def do_fun():\n",
+ " global sales #global variable\n",
+ " global profit #global variable \n",
+ " sales = 20000.00\n",
+ " profit=5000.00\n",
+ " print \"The sales in the second function are: \",sales\n",
+ " print \"The profit in the second function is: \",profit\n",
+ " third_fun() #Call third function to show that globals are visible\n",
+ "\n",
+ "def third_fun():\n",
+ " print \"\\nIn the third function:\"\n",
+ " print \"The sales in the third function are\",sales\n",
+ " print \"The profit in the third function is \",profit\n",
+ "\n",
+ "def main():\n",
+ " print \"No variable defined in main()\\n\"\n",
+ " do_fun()\n",
+ "\n",
+ "main()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "No variable defined in main()\n",
+ "\n",
+ "The sales in the second function are: 20000.0\n",
+ "The profit in the second function is: 5000.0\n",
+ "\n",
+ "In the third function:\n",
+ "The sales in the third function are 20000.0\n",
+ "The profit in the third function is 5000.0\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C17GLLO, page number:358"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def pr_again():\n",
+ " j=5 #Local to only pr_again().\n",
+ " print j,\",\",z,\",\",i\n",
+ "\n",
+ "global i\n",
+ "i=0\n",
+ "\n",
+ "def main():\n",
+ " p=9.0 #Local to main() only\n",
+ " print i,\",\",p\n",
+ " pr_again() #Calls next function.\n",
+ "\n",
+ "global z\n",
+ "z=9.0\n",
+ "main() \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "0 , 9.0\n",
+ "5 , 9.0 , 0\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C17LOC1, Page number:359"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def main():\n",
+ " age=input(\"What is your age? \") #Variable age is local to main()\n",
+ " get_age()\n",
+ " print \"main()'s age is still\",age\n",
+ " \n",
+ "def get_age():\n",
+ " age=input(\"What is your age again? \") #A different age. This one is local to get_age().\n",
+ "\n",
+ "main() \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "What is your age? 28\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "What is your age again? 56\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "main()'s age is still 28\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C17LOC2, Page number:360"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def main():\n",
+ " for ctr in range(0,11,1): #Loop counter\n",
+ " print \"main()'s ctr is \",ctr,\"\\n\"\n",
+ " do_fun() #Call second function\n",
+ "\n",
+ "def do_fun():\n",
+ " for ctr in range(10,0,-1):\n",
+ " print \"do_fun()'s ctr is \",ctr,\"\\n\"\n",
+ "\n",
+ "main()\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "main()'s ctr is 0 \n",
+ "\n",
+ "main()'s ctr is 1 \n",
+ "\n",
+ "main()'s ctr is 2 \n",
+ "\n",
+ "main()'s ctr is 3 \n",
+ "\n",
+ "main()'s ctr is 4 \n",
+ "\n",
+ "main()'s ctr is 5 \n",
+ "\n",
+ "main()'s ctr is 6 \n",
+ "\n",
+ "main()'s ctr is 7 \n",
+ "\n",
+ "main()'s ctr is 8 \n",
+ "\n",
+ "main()'s ctr is 9 \n",
+ "\n",
+ "main()'s ctr is 10 \n",
+ "\n",
+ "do_fun()'s ctr is 10 \n",
+ "\n",
+ "do_fun()'s ctr is 9 \n",
+ "\n",
+ "do_fun()'s ctr is 8 \n",
+ "\n",
+ "do_fun()'s ctr is 7 \n",
+ "\n",
+ "do_fun()'s ctr is 6 \n",
+ "\n",
+ "do_fun()'s ctr is 5 \n",
+ "\n",
+ "do_fun()'s ctr is 4 \n",
+ "\n",
+ "do_fun()'s ctr is 3 \n",
+ "\n",
+ "do_fun()'s ctr is 2 \n",
+ "\n",
+ "do_fun()'s ctr is 1 \n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C17MULI, Page number:362"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def main():\n",
+ " i=10\n",
+ "\n",
+ " i=20\n",
+ " print i,\" \",i,\"\\n\"\n",
+ "\n",
+ " i=30\n",
+ " print i,\" \",i,\" \",i,\"\\n\"\n",
+ " \n",
+ " i=10\n",
+ " print i,\" \",i,\" \",i \n",
+ "main()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "20 20 \n",
+ "\n",
+ "30 30 30 \n",
+ "\n",
+ "10 10 10\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C17LOC3, Page number:367"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def pr_init(initial):\n",
+ " print \"Your initial is \",initial\n",
+ "\n",
+ "def pr_other(age,salary):\n",
+ " print \"You look young for\",age,\"and \",'%.2f' %salary,\"is a lot of money\"\n",
+ "\n",
+ "initial=raw_input(\"What is your initial?\")\n",
+ "age=input(\"What is your age?\")\n",
+ "salary=input(\"What is your salary?\")\n",
+ "pr_init(initial) #call pr_init() and pass it initial\n",
+ "pr_other(age,salary) #call pr_other and pass age and salary\n",
+ "\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "What is your initial?Jerry\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "What is your age?30\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "What is your salary?50000\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Your initial is Jerry\n",
+ "You look young for 30 and 50000.00 is a lot of money\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C17LOC4, Page number:368"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def compute_sale(gallons):\n",
+ " #local variable\n",
+ " price_per=12.45 \n",
+ " x=price_per*float(gallons) #type casting gallons because it was integer\n",
+ " print \"The total is \",'%.2f' %x\n",
+ "\n",
+ "def main():\n",
+ " print \"Richard's Paint Service\"\n",
+ " gallons=input(\"How many gallons of paint did you buy?\")\n",
+ " compute_sale(gallons) #Function Call\n",
+ " \n",
+ "main()\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Richard's Paint Service\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "How many gallons of paint did you buy?20\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The total is 249.00\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C17STA2, Page number:372"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def main():\n",
+ " for ctr in range(1,26,1):\n",
+ " triple_it(ctr)\n",
+ "\n",
+ "def triple_it(ctr):\n",
+ " total=0\n",
+ " ans=ctr*3\n",
+ " total+=ans\n",
+ " print \"The number \",ctr,\"multiplied by 3 is \",ans\n",
+ "\n",
+ " if total>300:\n",
+ " print \"The total of triple numbers is over 300\"\n",
+ "\n",
+ "main()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The number 1 multiplied by 3 is 3\n",
+ "The number 2 multiplied by 3 is 6\n",
+ "The number 3 multiplied by 3 is 9\n",
+ "The number 4 multiplied by 3 is 12\n",
+ "The number 5 multiplied by 3 is 15\n",
+ "The number 6 multiplied by 3 is 18\n",
+ "The number 7 multiplied by 3 is 21\n",
+ "The number 8 multiplied by 3 is 24\n",
+ "The number 9 multiplied by 3 is 27\n",
+ "The number 10 multiplied by 3 is 30\n",
+ "The number 11 multiplied by 3 is 33\n",
+ "The number 12 multiplied by 3 is 36\n",
+ "The number 13 multiplied by 3 is 39\n",
+ "The number 14 multiplied by 3 is 42\n",
+ "The number 15 multiplied by 3 is 45\n",
+ "The number 16 multiplied by 3 is 48\n",
+ "The number 17 multiplied by 3 is 51\n",
+ "The number 18 multiplied by 3 is 54\n",
+ "The number 19 multiplied by 3 is 57\n",
+ "The number 20 multiplied by 3 is 60\n",
+ "The number 21 multiplied by 3 is 63\n",
+ "The number 22 multiplied by 3 is 66\n",
+ "The number 23 multiplied by 3 is 69\n",
+ "The number 24 multiplied by 3 is 72\n",
+ "The number 25 multiplied by 3 is 75\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C18PASS1, Page number:381"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def moon(weight): \n",
+ " weight/=6 \n",
+ " print \"You weigh only \",weight,\"pounds on the moon !\"\n",
+ "\n",
+ "def main(): \n",
+ " weight=input(\"How many pounds do you weigh? \")\n",
+ " moon(weight) #call the moon() function and pass weight\n",
+ "\n",
+ "main()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "How many pounds do you weigh? 120\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "You weigh only 20 pounds on the moon !\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C18PASS3, Page number:383"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def main():\n",
+ " Igrade=raw_input(\"What letter grade do you want?\")\n",
+ " average=input(\"What is your current test average\")\n",
+ " tests=input(\"How many tests do you have left?\")\n",
+ " check_grade(Igrade,average,tests) #// Calls function and passes three variables by value\n",
+ "\n",
+ "def check_grade(Igrade,average,tests):\n",
+ " if tests==0:\n",
+ " print \"You will get your current grade of \",Igrade\n",
+ " else:\n",
+ " if tests==1:\n",
+ " print \"You still have time to bring up your average of\",'%.1f' %average,\"up . Study hard !\"\n",
+ " else :\n",
+ " print \"Relax. You still have plenty of time.\"\n",
+ "\n",
+ " \n",
+ "main()\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "What letter grade do you want?A\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "What is your current test average1\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "How many tests do you have left?3\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Relax. You still have plenty of time.\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C19AVG, Page number:398"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def calc_av(num1,num2,num3):\n",
+ " local_avg=(num1+num2+num3) / 3 #Holds the average for these numbers\n",
+ " return local_avg\n",
+ "\n",
+ "print \"please type three numbers (such as 23 54 85) \"\n",
+ "num1=input()\n",
+ "num2=input()\n",
+ "num3=input()\n",
+ "\n",
+ "avg=calc_av(num1,num2,num3) #call function and pass the numbers\n",
+ "\n",
+ "print \"\\n\\nThe average is \",avg #Print the return value"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "please type three numbers (such as 23 54 85) \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "30\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "40\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "50\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "\n",
+ "The average is 40\n"
+ ]
+ }
+ ],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C19DOUB, Page number:401"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def doub(number):\n",
+ " d_num=number*2 #Doubles the number.\n",
+ " return d_num #Returns the result.\n",
+ "\n",
+ "number=input(\"What number do you want doubled? \")\n",
+ "d_number= doub(number) #Assigns return value.\n",
+ "print number,\" doubled is \",d_number"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "What number do you want doubled? 5\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "5 doubled is 10\n"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C19SUMD, Page number:403"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def sums(num):\n",
+ " sumd=0\n",
+ " if num<=0:\n",
+ " sumd=num\n",
+ " else:\n",
+ " for ctr in range(1,num+1,1):\n",
+ " sumd=sumd+ctr\n",
+ " return sumd\n",
+ "\n",
+ "num=input(\"Please type a number: \")\n",
+ "sumd= sums(num)\n",
+ "print \"The sum of the digits is \" , sumd\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Please type a number: 6\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The sum of the digits is 21\n"
+ ]
+ }
+ ],
+ "prompt_number": 13
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C19MINMX, Page number:404"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def maximum(num1,num2): \n",
+ " if num1>num2:\n",
+ " maxi=num1\n",
+ " else:\n",
+ " maxi=num2\n",
+ " return maxi\n",
+ "\n",
+ "def minimum(num1,num2):\n",
+ " if num1>num2:\n",
+ " mini=num2\n",
+ " else:\n",
+ " mini=num1\n",
+ " return mini\n",
+ "\n",
+ "print \"Please type two numbers ( such as 46 75 ) \"\n",
+ "num1 = input()\n",
+ "num2 = input()\n",
+ "\n",
+ "maxi=maximum(num1,num2) #Assign the return value of each function to variables\n",
+ "mini=minimum(num1,num2) \n",
+ "\n",
+ "print \"The minimum number is \",mini\n",
+ "print \"The maximum number is \", maxi"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Please type two numbers ( such as 46 75 ) \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "72\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "55\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The minimum number is 55\n",
+ "The maximum number is 72\n"
+ ]
+ }
+ ],
+ "prompt_number": 14
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C19PRO1, Page number:409"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "tax_rate=0.07 #Assume seven percent tax rate\n",
+ "total_sale=input(\"What is the sale amount? \")\n",
+ "total_sale+=tax_rate*total_sale\n",
+ "print \"The total sale is \",'%.2f' %total_sale\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "What is the sale amount? 4000\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The total sale is 4280.00\n"
+ ]
+ }
+ ],
+ "prompt_number": 15
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C19ASC, Page number:410"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def ascii(num):\n",
+ " asc_char=chr(num) #Type cast to a character\n",
+ " return asc_char\n",
+ "\n",
+ "\n",
+ "num=input(\"Enter an ASCII number? \")\n",
+ "asc_char=ascii(num) #Number is passed to the function ascii()\n",
+ "print \"The ASCII character for \",num,\"is \",asc_char\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter an ASCII number? 67\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The ASCII character for 67 is C\n"
+ ]
+ }
+ ],
+ "prompt_number": 16
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C19NPAY, Page number:411"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def netpayfun(hours,rate,taxrate):\n",
+ " gross_pay=hours*rate\n",
+ " taxes=taxrate*gross_pay\n",
+ " net_pay=gross_pay-taxes\n",
+ " return net_pay\n",
+ "\n",
+ "net_pay=netpayfun(40.0,3.50,0.20)\n",
+ "print \"The pay for 40 hours at $3.50/hr., and a 20% tax rate is $ \",net_pay\n",
+ "\n",
+ "net_pay=netpayfun(50.0,10.00,0.30)\n",
+ "print \"The pay for 40 hours at $10.00/hr., and a 30% tax rate is $ \",net_pay\n",
+ "\n",
+ "net_pay=netpayfun(10.0,5.00,0.10)\n",
+ "print \"The pay for 40 hours at $5.00/hr., and a 10% tax rate is $ \",net_pay\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The pay for 40 hours at $3.50/hr., and a 20% tax rate is $ 112.0\n",
+ "The pay for 40 hours at $10.00/hr., and a 30% tax rate is $ 350.0\n",
+ "The pay for 40 hours at $5.00/hr., and a 10% tax rate is $ 45.0\n"
+ ]
+ }
+ ],
+ "prompt_number": 17
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C20OVF1, Page number:423"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "i=-15\n",
+ "x=-64.53\n",
+ "\n",
+ "ians=abs(i) #abs() function is a built in function that returns a positive value \n",
+ "print \"Integer absolute value of -15 is \",ians\n",
+ "\n",
+ "fans=abs(x)\n",
+ "print \"Float absolute value of -64.53 is \",'%.2f' %fans"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Integer absolute value of -15 is 15\n",
+ "Float absolute value of -64.53 is 64.53\n"
+ ]
+ }
+ ],
+ "prompt_number": 18
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C20OVF2, Page number:424"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#function definition\n",
+ "def output(x):\n",
+ " if isinstance(x,int):\n",
+ " print x\n",
+ " else:\n",
+ " if isinstance(x,float):\n",
+ " print '%.2f' %x\n",
+ " else:\n",
+ " print x\n",
+ " \n",
+ "#Variable Decleration\n",
+ "name=\"C++ By Example makes C++ easy!\"\n",
+ "Ivalue=2543\n",
+ "fvalue=39.4321\n",
+ "\n",
+ "#calling function\n",
+ "output(name)\n",
+ "output(Ivalue)\n",
+ "output(fvalue)\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "C++ By Example makes C++ easy!\n",
+ "2543\n",
+ "39.43\n"
+ ]
+ }
+ ],
+ "prompt_number": 19
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+} \ No newline at end of file