summaryrefslogtreecommitdiff
path: root/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch4.ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch4.ipynb')
-rw-r--r--Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch4.ipynb839
1 files changed, 839 insertions, 0 deletions
diff --git a/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch4.ipynb b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch4.ipynb
new file mode 100644
index 00000000..d137f53e
--- /dev/null
+++ b/Engineering_Thermodynamics:_A_Computer_Approach_(SI_Units_Version)/ch4.ipynb
@@ -0,0 +1,839 @@
+{
+ "metadata": {
+ "name": "ch4"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''\nEXAMPLE 4.1 Using a while Loop to Compute a Sum of Consecutive Integers\nThis program computes the sum 1 + 2 + 3 + + n for an input integer n:\n'''\ni=1\nprint \"Enter a positive integer: \"\nn = int(raw_input())\ns=0\nwhile (i <= n):\n s += i\n i += 1\nprint \"The sum of the first %d integers is %d\" %(i,s)",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter a positive integer: \n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "5\n"
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "The sum of the first 6 integers is 15\n"
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''\nEXAMPLE 4.2 Using a while Loop to Compute a Sum of Reciprocals\nThis program computes the sum of reciprocals s = 1 + 1/2 + 1/3 + 1/n where n is\ninteger for which n is greater than s\n'''\n\nprint \"Enter a positive integer: \"\nbound = int(raw_input())\ns=0.0\ni=0\nwhile (s < bound):\n i += 1\n s += 1.0/i\n\nprint \"The sum of the first %d reciprocals is %f\" %(i,s)",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter a positive integer: \n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "5\n"
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "The sum of the first 83 reciprocals is 5.002068\n"
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''\nEXAMPLE 4.3 Using a while Loop to Repeat a Computation\nThis program prints the square root of each number input by the user. It uses a while loop to allow any\nnumber of computations in a single run of the program:\n'''\nimport math\nprint \"Enter a positive number: \"\nx = float(raw_input())\nwhile (x > 0):\n print \"sqrt(%d) = %f \"%(x,math.sqrt(x))\n print \"Enter another positive number (or 0 to quit): \"\n x = float(raw_input())\n",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter a positive number: \n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "5\n"
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "sqrt(5) = 2.236068 \nEnter another positive number (or 0 to quit): \n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "3\n"
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "sqrt(3) = 1.732051 \nEnter another positive number (or 0 to quit): \n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "0\n"
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''\nEXAMPLE 4.4 Using a break Statement to Terminate a Loop\nThis program has the same effect as the one in Example 4.1 on page 60:\n'''\ni=1\nprint \"Enter a positive integer: \";\nn = int(raw_input())\ns=0\nwhile(True):\n if (i > n):\n break # terminates the loop immediately\n s += i\n i += 1\nprint \"The sum of the first %d integers is %d\" %(n,s)",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter a positive integer: \n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "5\n"
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "The sum of the first 5 integers is 15\n"
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''\nEXAMPLE 4.5 The Fibonacci Numbers\n'''\n\nprint \"Enter a positive integer: \"\nbound = int(raw_input())\nprint \"Fibonacci numbers < %d:\\n0, 1\" % bound ,\nf0=0\nf1=1\nwhile (True):\n f2 = f0 + f1\n if (f2 > bound):\n break\n print \", %d\" % f2,\n f0 = f1\n f1 = f2\n ",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter a positive integer: \n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "10\n"
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Fibonacci numbers < 10:\n0, 1 , 1 , 2 , 3 , 5 , 8\n"
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''\nEXAMPLE 4.6 Using the exit(0) Function\nThe exit() function provides another way to terminate a loop. When it executes, it terminates the\nprogram itself:\n'''\nimport sys\nprint \"Enter a positive integer: \"\nbound = int(raw_input())\nprint \"Fibonacci numbers < %d:\\n0, 1\" % bound ,\nf0=0\nf1=1\nwhile (True):\n f2 = f0 + f1\n if (f2 > bound):\n sys.exit(0)\n print \", %d\" % f2,\n f0 = f1\n f1 = f2\n ",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter a positive integer: \n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "10\n"
+ },
+ {
+ "ename": "SystemExit",
+ "evalue": "0",
+ "output_type": "pyerr",
+ "traceback": [
+ "An exception has occurred, use %tb to see the full traceback.\n",
+ "\u001b[1;31mSystemExit\u001b[0m\u001b[1;31m:\u001b[0m 0\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Fibonacci numbers < 10:\n0, 1 , 1 , 2 , 3 , 5 , 8"
+ },
+ {
+ "output_type": "stream",
+ "stream": "stderr",
+ "text": "To exit: use 'exit', 'quit', or Ctrl-D.\n"
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''\nEXAMPLE 4.7 Aborting Infinite Loop\nWithout some termination mechanism, the loop will run forever. To abort its execution after it starts,\npress <Ctrl>+C (i.e., hold the Ctrl key down and press the C key on your keyboard):\n'''\n\nprint \"Enter a positive integer: \"\nbound = int(raw_input())\nprint \"Fibonacci numbers < %d:\\n0, 1\" % bound ,\nf0=0\nf1=1\n# Error : infinite loop !\nwhile (True):\n f2 = f0 + f1\n # By commenting the below if statement, it goes to infinite.\n if (f2 > bound):\n break\n print \", %d\" % f2,\n f0 = f1\n f1 = f2",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter a positive integer: \n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "10\n"
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Fibonacci numbers < 10:\n0, 1 , 1 , 2 , 3 , 5 , 8\n"
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''\nEXAMPLE 4.7 Aborting Infinite Loop\nWithout some termination mechanism, the loop will run forever. To abort its execution after it starts,\npress <Ctrl>+C (i.e., hold the Ctrl key down and press the C key on your keyboard):\n'''\n\nprint \"Enter a positive integer: \"\nbound = int(raw_input())\nprint \"Fibonacci numbers < %d:\\n0, 1\" % bound ,\nf0=0\nf1=1\n# Error : infinite loop !\nwhile (True):\n f2 = f0 + f1\n # By commenting the below if statement, it goes to infinite.\n if (f2 > bound):\n break\n print \", %d\" % f2,\n f0 = f1\n f1 = f2",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter a positive integer: \nFibonacci numbers < 10:\n0, 1 , 1 , 2 , 3 , 5 , 8\n"
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''\nEXAMPLE 4.8 Using a do..while Loop to Compute a Sum of Consecutive Integers\nThis program has the same effect as the one in Example 4.1 on page 60:\n'''\ni=0\nprint \"Enter a positive integer: \"\nn = int(raw_input())\ns=0\nwhile i<=n:\n s += i\n i += 1\nprint \"The sum of the first %d integers is %d\" %(n,s)\n",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter a positive integer: \n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "10\n"
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "The sum of the first 10 integers is 55\n"
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''\nEXAMPLE 4.9 The Factorial Numbers\n'''\n\nprint \"Enter a positive integer: \"\nbound = int(raw_input())\nprint \"Factorial numbers < %d:\\n1, 1\" %bound,\nf=1\ni=1\nwhile f < bound:\n i += 1\n f *= i\n print \", %d\" %f,\n",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter a positive integer: \n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "10\n"
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Factorial numbers < 10:\n1, 1 , 2 , 6 , 24\n"
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''\nEXAMPLE 4.10 Using a for Loop to Compute a Sum of Consecutive Integers\nThis program has the same effect as the one in Example 4.1 on page 60:\n'''\n\nprint \"Enter a positive integer: \"\nn = int(raw_input())\ns=0;\nfor i in range(0,n+1):\n s += i\nprint \"The sum of the first %d integers is %d\" %(n,s)\n",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter a positive integer: \n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "10\n"
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "The sum of the first 10 integers is 55\n"
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''\nEXAMPLE 4.11 Reusing for Loop Control Variable Names\nThis program has the same effect as the one in Example 4.1 on page 60:\n'''\nprint \"Enter a positive integer: \"\nn = int(raw_input())\ns=0\nfor i in range(1,n/2): # the scope of this i is this loop\n s += i\n\nfor i in range(n/2,n+1): # the scope of this i is this loop\n s += i\nprint \"The sum of the first %d integers is %d\" % (n,s)\n",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter a positive integer: \n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "10\n"
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "The sum of the first 10 integers is 55\n"
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''\nEXAMPLE 4.12 The Factorial Numbers Again\nThis program has the same effect as the one in Example 4.9 on page 65:\n'''\nprint \"Enter a positive integer: \"\nbound = int(raw_input())\n\nprint \"Factorial numbers that are <= %d:\\n1, 1\" %bound,\nf=1\nfor i in range(2,bound+1):\n f *= i\n print \", %d\" % f,\n",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter a positive integer: \n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "10\n"
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Factorial numbers that are <= 10:\n1, 1 , 2 , 6 , 24 , 120 , 720 , 5040 , 40320 , 362880 , 3628800\n"
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''\nEXAMPLE 4.13 Using a Descending for Loop\nThis program prints the first ten positive integers in reverse order:\n'''\n\nfor i in range(10,0,-1):\n print i,\n",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "10 9 8 7 6 5 4 3 2 1\n"
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''\nEXAMPLE 4.14 Using a for Loop with a Step Greater than One\nThis program determines whether an input number is prime:\n'''\nprime = True\nprint \"Enter a positive integer: \"\nn = int(raw_input())\nif (n < 2):\n print \"%d is not prime.\" %n\n prime = False\nelif (n < 4):\n print \"%d is prime.\" %n\n prime = False\nelif (n%2 == 0):\n print \"%d = 2* %d\" %(n,n/2)\n prime = False\nelse:\n for d in range(3,n/2+1):\n if (n%d == 0):\n print \"%d = %d * %d\" %(n,d,n/d)\n prime = False\nif prime: \n print \"%d is prime.\"%n\n",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter a positive integer: \n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "11\n"
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "11 is prime.\n"
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''\nEXAMPLE 4.15 Using a Sentinel to Control a for Loop\nThis program finds the maximum of a sequence of input numbers:\n'''\nprint \"Enter positive integers (0 to quit): \";\nn = int(raw_input())\nm = n\nwhile n > 0:\n n = int(raw_input())\n if n > m :\n m = n\n\nprint \"max = %d\" % m",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter positive integers (0 to quit): \n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "5\n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "19\n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "42\n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "1\n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "0\n"
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "max = 42\n"
+ }
+ ],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''\nEXAMPLE 4.16 Using a Loop Invariant to Prove that a for Loop is Correct\nThis program finds the minimum of a sequence of input numbers. It is similar to the program in\nExample 4.15:\n'''\n\nprint \"Enter positive integers (0 to quit): \";\nn = int(raw_input())\nm = n\nwhile n > 0: \n if n < m :\n m = n\n n = int(raw_input())\n\nprint \"min = %d\" % m\n\n",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter positive integers (0 to quit): \n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "5\n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "19\n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "42\n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "1\n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "0\n"
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "min = 1\n"
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''\nEXAMPLE 4.17\n'''\n\nm = 95\nn = 11\nwhile m%n > 0:\n print \"%d modulo %d = %d\" %(m,n,m%n)\n m -= 3\n n += 1\n",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "95 modulo 11 = 7\n92 modulo 12 = 8\n89 modulo 13 = 11\n86 modulo 14 = 2\n83 modulo 15 = 8\n"
+ }
+ ],
+ "prompt_number": 12
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''\nEXAMPLE 4.18 Nesting for Loops\nThis program prints a multiplication table:\n'''\n\nfor x in range(1,13):\n for y in range(1,13):\n print \"%4d\" % (x*y),\n print \"\"\n",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": " 1 2 3 4 5 6 7 8 9 10 11 12 \n 2 4 6 8 10 12 14 16 18 20 22 24 \n 3 6 9 12 15 18 21 24 27 30 33 36 \n 4 8 12 16 20 24 28 32 36 40 44 48 \n 5 10 15 20 25 30 35 40 45 50 55 60 \n 6 12 18 24 30 36 42 48 54 60 66 72 \n 7 14 21 28 35 42 49 56 63 70 77 84 \n 8 16 24 32 40 48 56 64 72 80 88 96 \n 9 18 27 36 45 54 63 72 81 90 99 108 \n 10 20 30 40 50 60 70 80 90 100 110 120 \n 11 22 33 44 55 66 77 88 99 110 121 132 \n 12 24 36 48 60 72 84 96 108 120 132 144 \n"
+ }
+ ],
+ "prompt_number": 13
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''\nEXAMPLE 4.19 Testing a Loop Invariant\n'''\nimport math\n# defines pow() and log()\n\nprint \"Enter a positive integer: \"\nn = int(raw_input())\nd=0 # the discrete binary logarithm of n\np2d=1 # = 2^d\ni = n\nwhile i > 1:\n # INVARIANT: 2^d <= n/i < 2*2^d\n p2d=math.pow(2,d) # = 2^d\n print \"%2d <= %2d\" %(p2d,2*p2d)\n i /= 2\n d += 1\n\np2d=math.pow(2,d) # = 2^d\nprint \"%2d <= %2d < %2d\" %(p2d,n,2*p2d)\nprint \" The discrete binary logarithm of is %d\" % d \nlgn = math.log(n)/math.log(2) # base 2 logarithm\nprint \"The continuous binary logarithm of is %f\" % lgn",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter a positive integer: \n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "17\n"
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": " 1 <= 2\n 2 <= 4\n 4 <= 8\n 8 <= 16\n16 <= 17 < 32\n The discrete binary logarithm of is 4\nThe continuous binary logarithm of is 4.087463\n"
+ }
+ ],
+ "prompt_number": 14
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''\nEXAMPLE 4.20 Using a break Statement to Terminate a Loop\nThis program has the same effect as the one in Example 4.1 on page 60. It uses a break statement to\ncontrol the loop:\n'''\ni=1\nprint \"Enter a positive integer: \"\nn = int(raw_input())\ns=0\nwhile (True):\n if (i > n):\n break\n s += i\n i += 1\n\nprint \"The sum of the first %d integers is %d\" %(i,s)\n",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter a positive integer: \n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "10\n"
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "The sum of the first 11 integers is 55\n"
+ }
+ ],
+ "prompt_number": 15
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''\nEXAMPLE 4.21 Controlling Input with a Sentinel\nThis program reads a sequence of positive integers, terminated by 0, and prints their average:\n'''\ncount=0\ns=0\nprint \"Enter positive integers (0 to quit):\" \nwhile True: # \"forever\"\n print \"\\t %d :\" %(count + 1),\n n = int(raw_input())\n if (n <= 0):\n break\n count += 1\n s += n\n\nprint \"The average of those %d positive numbers is \" %count,\nprint float(s)/count\n",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter positive integers (0 to quit):\n\t 1 :"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "12\n"
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": " \t 2 :"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "32\n"
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": " \t 3 :"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "11\n"
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": " \t 4 :"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "0\n"
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": " The average of those 3 positive numbers is 18.3333333333\n"
+ }
+ ],
+ "prompt_number": 16
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''\nEXAMPLE 4.22 Using a break Statement with Nested Loops\n'''\n\nfor x in range(1,13):\n for y in range(1,13):\n if y>x:\n break\n else:\n print '%4d' %(x*y),\n print ''\n",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": " 1 \n 2 4 \n 3 6 9 \n 4 8 12 16 \n 5 10 15 20 25 \n 6 12 18 24 30 36 \n 7 14 21 28 35 42 49 \n 8 16 24 32 40 48 56 64 \n 9 18 27 36 45 54 63 72 81 \n 10 20 30 40 50 60 70 80 90 100 \n 11 22 33 44 55 66 77 88 99 110 121 \n 12 24 36 48 60 72 84 96 108 120 132 144 \n"
+ }
+ ],
+ "prompt_number": 17
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''\nEXAMPLE 4.23 Using continue and break Statements\nThis little program illustrates the continue and break statements:\n'''\nwhile True:\n n = int(raw_input('Enter int : '))\n if (n%2 == 0):\n continue\n if (n%3 == 0):\n break\n print \"\\tBottom of loop.\\n\"\nprint \"\\tOutside of loop.\\n\"\n",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter int : 5\n"
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "\tBottom of loop.\n\n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter int : 4\n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter int : 6\n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter int : 9\n"
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "\tOutside of loop.\n\n"
+ }
+ ],
+ "prompt_number": 18
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''\nEXAMPLE 4.24 Using a goto Statement to Break Out of a Nest of Loops\nNote : Python has no goto facility.\n'''\nN=5\ndone=False\nfor i in range(N):\n for j in range(N):\n if done:\n break\n for k in range(N):\n if done:\n break\n if (i+j+k>N):\n done = True\n else:\n print i+j+k,\n print \" \",\n print \"* \"\n print \".\" \n done = False\n",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "0 1 2 3 4 * \n1 2 3 4 5 * \n2 3 4 5 * \n.\n1 2 3 4 5 * \n2 3 4 5 * \n.\n2 3 4 5 * \n.\n3 4 5 * \n.\n4 5 * \n.\n"
+ }
+ ],
+ "prompt_number": 19
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''\nEXAMPLE 4.25 Using a Flag to Break Out of a Nest of Loops\nThis program has the same output as that in Example 4.24:\n'''\nN=5\ndone=False\nfor i in range(N):\n for j in range(N):\n if done:\n break\n for k in range(N):\n if done:\n break\n if (i+j+k>N):\n done = True\n else:\n print i+j+k,\n print \" \",\n print \"* \"\n print \".\" \n done = False\n",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "0 1 2 3 4 * \n1 2 3 4 5 * \n2 3 4 5 * \n.\n1 2 3 4 5 * \n2 3 4 5 * \n.\n2 3 4 5 * \n.\n3 4 5 * \n.\n4 5 * \n.\n"
+ }
+ ],
+ "prompt_number": 20
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''\nEXAMPLE 4.26 Generating Pseudo-Random Numbers\nThis program uses the rand() function to generate pseudo-random numbers:\n'''\nimport random\n\n# prints pseudo-random numbers:\n\nfor i in range(0,8):\n print random.random()\n\n ",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "0.702115758628\n0.969460447904\n0.409934401112\n0.700339443791\n0.093528851602\n0.132172955687\n0.0162887279366\n0.943010713478\n"
+ }
+ ],
+ "prompt_number": 21
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''\nEXAMPLE 4.27 Setting the Seed Interactively\nThis program is the same as the one in Example 4.26 except that it allows the pseudo-random number\ngenerator's seed to be set interactively:\n'''\nimport random\n# prints pseudo-random numbers:\nprint \"Enter seed: \"\nseed = int(raw_input())\nrandom.seed(seed);\nfor i in range(0,8):\n print random.random()\n",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter seed: \n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "5\n"
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "0.62290169489\n0.741786989261\n0.795193565566\n0.942450283777\n0.73989857474\n0.922324996665\n0.0290052282836\n0.465622654378\n"
+ }
+ ],
+ "prompt_number": 22
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''\nEXAMPLE 4.28 Setting the Seed from the System Clock\nThis program is the same as the one in Example 4.27 except that it sets the pseudo-random number\ngenerator's seed from the system clock.\n'''\nimport random\nfor i in range(0,8):\n print random.random()\n",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "0.943356716998\n0.648974553137\n0.900900491751\n0.113205964653\n0.469069047782\n0.24657283262\n0.543760859236\n0.573941187928\n"
+ }
+ ],
+ "prompt_number": 23
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''\nEXAMPLE 4.29 Generating Pseudo-Random Numbers in Given Range\nThis program is the same as the one in Example 4.28 except that the pseudo-random numbers that it\ngenerates are restricted to given range:\n'''\nimport random\nprint \"Enter minimum and maximum: \"\nm = int(raw_input())\nn = int(raw_input())\n# lowest and highest numbers\nr = n - m + 1\n# number of numbers in range\nfor i in range(0,20):\n j = int(random.random()*100 % r + m)\n print j,\n print \" \",\n",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter minimum and maximum: \n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "5\n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "15\n"
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "6 15 10 8 15 9 7 7 11 6 5 15 14 15 15 15 11 13 14 6 \n"
+ }
+ ],
+ "prompt_number": 24
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "",
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+} \ No newline at end of file