{ "metadata": { "name": "", "signature": "sha256:507ed1502286b7e07eb64a51dd0502249f2763079a23aefef815ad5e65c327ad" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "code", "collapsed": false, "input": [ "\n", "import math\n", "\n", "# tests the sqrt() function:\n", "for i in range(0,6):\n", " print \"\\t %d \\t %f\" %(i,math.sqrt(i))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\t 0 \t 0.000000\n", "\t 1 \t 1.000000\n", "\t 2 \t 1.414214\n", "\t 3 \t 1.732051\n", "\t 4 \t 2.000000\n", "\t 5 \t 2.236068\n" ] } ], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "import math\n", "# tests the identity sin 2x = 2 sin x cos x:\n", "x = 0\n", "while x < 2:\n", " print \"%f \\t\\t %f \\t %f\" %(x,math.sin(2*x),2*math.sin(x)*math.cos(x))\n", " x += 0.2\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0.000000 \t\t 0.000000 \t 0.000000\n", "0.200000 \t\t 0.389418 \t 0.389418\n", "0.400000 \t\t 0.717356 \t 0.717356\n", "0.600000 \t\t 0.932039 \t 0.932039\n", "0.800000 \t\t 0.999574 \t 0.999574\n", "1.000000 \t\t 0.909297 \t 0.909297\n", "1.200000 \t\t 0.675463 \t 0.675463\n", "1.400000 \t\t 0.334988 \t 0.334988\n", "1.600000 \t\t -0.058374 \t -0.058374\n", "1.800000 \t\t -0.442520 \t -0.442520\n", "2.000000 \t\t -0.756802 \t -0.756802\n" ] } ], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "def cube(x):\n", " # returns cube of x:\n", " return x*x*x\n", "\n" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "def cube(x):\n", " # returns cube of x:\n", " return x*x*x\n", "\n", "# tests the cube() function:\n", "n=1\n", "while (n != 0):\n", " n = int(raw_input())\n", " print \"\\tcube( %d ) = %d\" %(n,cube(n))" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "4\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\tcube( 4 ) = 64\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "2\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\tcube( 2 ) = 8\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "9\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\tcube( 9 ) = 729\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "0\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\tcube( 0 ) = 0\n" ] } ], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def maximum(x,y):\n", " # returns larger of the two given integers:\n", " if (x < y):\n", " return y\n", " else:\n", " return x\n", "\n", "# tests the max() function:\n", "m = 1\n", "n = 1\n", "while m != 0: \n", " m = int(raw_input())\n", " n = int(raw_input())\n", " print \"\\tmax( %d , %d ) = %d\" %(m,n,maximum(m,n))\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "5\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "2\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\tmax( 5 , 2 ) = 5\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "0\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "3\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\tmax( 0 , 3 ) = 3\n" ] } ], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "def maximum(x,y):\n", " # returns larger of the two given integers:\n", " if (x < y):\n", " return y\n", " else:\n", " return x\n", "\n", "# tests the max() function:\n", "m = 1\n", "n = 1\n", "while m != 0: \n", " m = int(raw_input())\n", " n = int(raw_input())\n", " print \"\\tmax( %d , %d ) = %d\" %(m,n,maximum(m,n))\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "5\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "2\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\tmax( 5 , 2 ) = 5\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "0\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "3\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\tmax( 0 , 3 ) = 3\n" ] } ], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "# returns larger of the two given integers:\n", "\n", "m = 1\n", "n = 1\n", "while m!=0:\n", " m = int(raw_input())\n", " n = int(raw_input())\n", " print \"\\tmax(%d,%d) = %d\" %(m,n, max(m,n))\n" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "5\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "4\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\tmax(5,4) = 5\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "4\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "3\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\tmax(4,3) = 4\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "8\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "0\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\tmax(8,0) = 8\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "0\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\tmax(0,5) = 5\n" ] } ], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "def fact(n):\n", " if (n < 0):\n", " return 0\n", " f = 1\n", " while (n > 1):\n", " f *= n\n", " n -= 1\n", " return f\n", "\n", "for i in range(-1,6):\n", " print fact(i),\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0 1 1 2 6 24 120\n" ] } ], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def fact(n):\n", " if (n < 0):\n", " return 0\n", " f = 1\n", " while (n > 1):\n", " f *= n\n", " n -= 1\n", " return f\n", "\n", "\n", "def perm(n,k):\n", " # returns P(n,k), the number of permutations of k from n:\n", " if (n < 0 or k < 0 or k > n):\n", " return 0\n", " return fact(n)/fact(n-k)\n", "\n", "for i in range(-1,8):\n", " for j in range(-1,i+2):\n", " print perm(i,j),\n", " print ''\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0 0 \n", "0 1 0 \n", "0 1 1 0 \n", "0 1 2 2 0 \n", "0 1 3 6 6 0 \n", "0 1 4 12 24 24 0 \n", "0 1 5 20 60 120 120 0 \n", "0 1 6 30 120 360 720 720 0 \n", "0 1 7 42 210 840 2520 5040 5040 0 \n" ] } ], "prompt_number": 6 }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def printDate(m,d,y):\n", " # prints the given date in literal form:\n", " if (m < 1 or m > 12 or d < 1 or d > 31 or y < 0):\n", " print \"Error: parameter out of range.\\n\"\n", " return\n", " if m == 1:\n", " print \"January \",\n", " elif m ==2:\n", " print \"February \",\n", " elif m==3 :\n", " print \"March \",\n", " elif m==4:\n", " print \"April \",\n", " elif m==5:\n", " print \"May \",\n", " elif m==6:\n", " print \"June \",\n", " elif m==7:\n", " print \"July \",\n", " elif m==8:\n", " print \"August \",\n", " elif m==9:\n", " print \"September \",\n", " elif m==10:\n", " print \"October \",\n", " elif m==1:\n", " print \"November \",\n", " else:\n", " print \"December \",\n", " print d , \", \", y \n", "\n", "# tests the printDate() function:\n", "month = 1\n", "while month > 0:\n", " month = int(raw_input())\n", " day = int(raw_input())\n", " year = int(raw_input())\n", " printDate(month,day,year)\n" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "9\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "12\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "1989\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "September 12 , 1989\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "0\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "5\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "2001\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Error: parameter out of range.\n", "\n" ] } ], "prompt_number": 8 }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "import string\n", "def ispunct(s):\n", " return all(c in string.punctuation for c in s)\n", "def printCharCategory(c):\n", " # prints the category to which the given character belongs:\n", " print \"The character [\" + c + \"] is a \",\n", " if(c.isdigit()):\n", " print \"digit.\\n\"\n", " elif (c.islower()):\n", " print \"lower-case letter.\\n\"\n", " elif (c.isupper()): \n", " print \"capital letter.\\n\"\n", " elif (c.isspace()):\n", " print \"white space character.\\n\"\n", " elif (ord(c) >= 10 and ord(c) <= 15 or ord(c) == 0):\n", " print \"control character.\\n\"\n", " elif (ispunct(c)):\n", " print \"punctuation mark.\\n\"\n", " else:\n", " print \"Error.\\n\"\n", "\n", "# prints the category to which the given character belongs;\n", "# tests the printCharCategory() function:\n", "for c in range(128):\n", " printCharCategory(chr(c))\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " The character [\u0000] is a control character.\n", "\n", "The character [\u0001] is a Error.\n", "\n", "The character [\u0002] is a Error.\n", "\n", "The character [\u0003] is a Error.\n", "\n", "The character [\u0004] is a Error.\n", "\n", "The character [\u0005] is a Error.\n", "\n", "The character [\u0006] is a Error.\n", "\n", "The character [\u0007] is a Error.\n", "\n", "The character [\b] is a Error.\n", "\n", "The character [\t] is a white space character.\n", "\n", "The character [\n", "] is a white space character.\n", "\n", "The character [\u000b", "] is a white space character.\n", "\n", "The character [\f", "] is a white space character.\n", "\n", "The character [\r", "] is a white space character.\n", "\n", "The character [\u000e] is a control character.\n", "\n", "The character [\u000f] is a control character.\n", "\n", "The character [\u0010] is a Error.\n", "\n", "The character [\u0011] is a Error.\n", "\n", "The character [\u0012] is a Error.\n", "\n", "The character [\u0013] is a Error.\n", "\n", "The character [\u0014] is a Error.\n", "\n", "The character [\u0015] is a Error.\n", "\n", "The character [\u0016] is a Error.\n", "\n", "The character [\u0017] is a Error.\n", "\n", "The character [\u0018] is a Error.\n", "\n", "The character [\u0019] is a Error.\n", "\n", "The character [\u001a] is a Error.\n", "\n", "The character [\u001b] is a Error.\n", "\n", "The character [\u001c", "] is a Error.\n", "\n", "The character [\u001d", "] is a Error.\n", "\n", "The character [\u001e", "] is a Error.\n", "\n", "The character [\u001f] is a Error.\n", "\n", "The character [ ] is a white space character.\n", "\n", "The character [!] is a punctuation mark.\n", "\n", "The character [\"] is a punctuation mark.\n", "\n", "The character [#] is a punctuation mark.\n", "\n", "The character [$] is a punctuation mark.\n", "\n", "The character [%] is a punctuation mark.\n", "\n", "The character [&] is a punctuation mark.\n", "\n", "The character ['] is a punctuation mark.\n", "\n", "The character [(] is a punctuation mark.\n", "\n", "The character [)] is a punctuation mark.\n", "\n", "The character [*] is a punctuation mark.\n", "\n", "The character [+] is a punctuation mark.\n", "\n", "The character [,] is a punctuation mark.\n", "\n", "The character [-] is a punctuation mark.\n", "\n", "The character [.] is a punctuation mark.\n", "\n", "The character [/] is a punctuation mark.\n", "\n", "The character [0] is a digit.\n", "\n", "The character [1] is a digit.\n", "\n", "The character [2] is a digit.\n", "\n", "The character [3] is a digit.\n", "\n", "The character [4] is a digit.\n", "\n", "The character [5] is a digit.\n", "\n", "The character [6] is a digit.\n", "\n", "The character [7] is a digit.\n", "\n", "The character [8] is a digit.\n", "\n", "The character [9] is a digit.\n", "\n", "The character [:] is a punctuation mark.\n", "\n", "The character [;] is a punctuation mark.\n", "\n", "The character [<] is a punctuation mark.\n", "\n", "The character [=] is a punctuation mark.\n", "\n", "The character [>] is a punctuation mark.\n", "\n", "The character [?] is a punctuation mark.\n", "\n", "The character [@] is a punctuation mark.\n", "\n", "The character [A] is a capital letter.\n", "\n", "The character [B] is a capital letter.\n", "\n", "The character [C] is a capital letter.\n", "\n", "The character [D] is a capital letter.\n", "\n", "The character [E] is a capital letter.\n", "\n", "The character [F] is a capital letter.\n", "\n", "The character [G] is a capital letter.\n", "\n", "The character [H] is a capital letter.\n", "\n", "The character [I] is a capital letter.\n", "\n", "The character [J] is a capital letter.\n", "\n", "The character [K] is a capital letter.\n", "\n", "The character [L] is a capital letter.\n", "\n", "The character [M] is a capital letter.\n", "\n", "The character [N] is a capital letter.\n", "\n", "The character [O] is a capital letter.\n", "\n", "The character [P] is a capital letter.\n", "\n", "The character [Q] is a capital letter.\n", "\n", "The character [R] is a capital letter.\n", "\n", "The character [S] is a capital letter.\n", "\n", "The character [T] is a capital letter.\n", "\n", "The character [U] is a capital letter.\n", "\n", "The character [V] is a capital letter.\n", "\n", "The character [W] is a capital letter.\n", "\n", "The character [X] is a capital letter.\n", "\n", "The character [Y] is a capital letter.\n", "\n", "The character [Z] is a capital letter.\n", "\n", "The character [[] is a punctuation mark.\n", "\n", "The character [\\] is a punctuation mark.\n", "\n", "The character []] is a punctuation mark.\n", "\n", "The character [^] is a punctuation mark.\n", "\n", "The character [_] is a punctuation mark.\n", "\n", "The character [`] is a punctuation mark.\n", "\n", "The character [a] is a lower-case letter.\n", "\n", "The character [b] is a lower-case letter.\n", "\n", "The character [c] is a lower-case letter.\n", "\n", "The character [d] is a lower-case letter.\n", "\n", "The character [e] is a lower-case letter.\n", "\n", "The character [f] is a lower-case letter.\n", "\n", "The character [g] is a lower-case letter.\n", "\n", "The character [h] is a lower-case letter.\n", "\n", "The character [i] is a lower-case letter.\n", "\n", "The character [j] is a lower-case letter.\n", "\n", "The character [k] is a lower-case letter.\n", "\n", "The character [l] is a lower-case letter.\n", "\n", "The character [m] is a lower-case letter.\n", "\n", "The character [n] is a lower-case letter.\n", "\n", "The character [o] is a lower-case letter.\n", "\n", "The character [p] is a lower-case letter.\n", "\n", "The character [q] is a lower-case letter.\n", "\n", "The character [r] is a lower-case letter.\n", "\n", "The character [s] is a lower-case letter.\n", "\n", "The character [t] is a lower-case letter.\n", "\n", "The character [u] is a lower-case letter.\n", "\n", "The character [v] is a lower-case letter.\n", "\n", "The character [w] is a lower-case letter.\n", "\n", "The character [x] is a lower-case letter.\n", "\n", "The character [y] is a lower-case letter.\n", "\n", "The character [z] is a lower-case letter.\n", "\n", "The character [{] is a punctuation mark.\n", "\n", "The character [|] is a punctuation mark.\n", "\n", "The character [}] is a punctuation mark.\n", "\n", "The character [~] is a punctuation mark.\n", "\n", "The character [\u007f] is a Error.\n", "\n" ] } ], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "import math\n", "def isPrime(n):\n", " # returns True if n is prime, False otherwise:\n", " sqrtn = math.sqrt(n)\n", " if (n < 2):\n", " return False\n", " # 0 and 1 are not primes\n", " if (n < 4):\n", " return True\n", " # 2 and 3 are the first primes\n", " if (n%2 == 0):\n", " return False\n", " # 2 is the only even prime\n", " for d in range(3,int(sqrtn+1),2):\n", " if (n%d == 0):\n", " return False\n", " # n has a nontrivial divisor\n", " return True;\n", "\n", "for n in range(0,80):\n", " if (isPrime(n)):\n", " print n,\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79\n" ] } ], "prompt_number": 10 }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def isLeapYear(y):\n", " # returns true iff y is a leap year:\n", " return (y % 4 == 0 and y % 100 != 0 or y % 400 == 0)\n", "\n", "# tests the isLeapYear() function:\n", "n = 2\n", "while n > 1:\n", " n = int(raw_input())\n", " if (isLeapYear(n)):\n", " print \"%d is a leap year.\" % n\n", " else:\n", " print \"%d is not a leap year.\" %n\n" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "2004\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "2004 is a leap year.\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "2006\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "2006 is not a leap year.\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "2013\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "2013 is not a leap year.\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "0\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "0 is a leap year.\n" ] } ], "prompt_number": 11 }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "def age():\n", " # prompts the user to input his/her age, and returns that value:\n", " while (True):\n", " print \"How old are you: \"\n", " n = int(raw_input())\n", " if (n < 0):\n", " print \"\\a\\tYour age could not be negative.\"\n", " elif (n > 120):\n", " print \"\\a\\tYou could not be over 120.\"\n", " else:\n", " return n\n", " print \"\\n\\tTry again.\\n\"\n", "\n", "a = age();\n", "print \"\\nYou are %d years old.\" %a\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "How old are you: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "-12\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\u0007\tYour age could not be negative.\n", "\n", "\tTry again.\n", "\n", "How old are you: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "125\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\u0007\tYou could not be over 120.\n", "\n", "\tTry again.\n", "\n", "How old are you: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "24\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "You are 24 years old.\n" ] } ], "prompt_number": 14 }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "def swap(x,y):\n", " # exchanges the values of x and y:\n", " x[0],y[0] = y[0],x[0]\n", "\n", "a = [22.2]\n", "b = [44.4]\n", "print \"a = %.2f , b = %.2f \" %(a[0],b[0])\n", "swap(a,b)\n", "print \"a = %.2f , b = %.2f \" %(a[0],b[0])\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "a = 22.20 , b = 44.40 \n", "a = 44.40 , b = 22.20 \n" ] } ], "prompt_number": 15 }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "def f(x,y):\n", " x[0]= 88\n", " y[0] = 99\n", "\n", "# tests the f() function:\n", "a = [22]\n", "b = [44]\n", "print \"a = %.2f , b = %.2f \" %(a[0],b[0])\n", "f(a,b)\n", "print \"a = %.2f , b = %.2f \" %(a[0],b[0])\n", "f(2*a,b)\n", "print \"a = %.2f , b = %.2f \" %(a[0],b[0])\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "a = 22.00 , b = 44.00 \n", "a = 88.00 , b = 99.00 \n", "a = 88.00 , b = 99.00 \n" ] } ], "prompt_number": 16 }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "def computeCircle(r):\n", " # returns the area and circumference of a circle with radius r:\n", " PI = 3.141592653589793\n", " area = PI*r*r\n", " circumference = 2*PI*r\n", " return area,circumference\n", "\n", "# tests the computeCircle() function:\n", "print \"Enter radius: \"\n", "r = int(raw_input())\n", "a,c = computeCircle(r)\n", "print \"area = %.2f , circumference = %.2f\" %(a,c)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter radius: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "area = 78.54 , circumference = 31.42\n" ] } ], "prompt_number": 17 }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "def f(x,y,z):\n", " x[0] += z[0]\n", " y[0] += z[0]\n", " print \"x = %d , y = %d , z = %d\" %(x[0],y[0],z[0])\n", "\n", "x = [22]\n", "y = [33]\n", "z = [44]\n", "\n", "print \"x = %d , y = %d , z = %d\" %(x[0],y[0],z[0])\n", "f(x,y,z)\n", "print \"x = %d , y = %d , z = %d\" %(x[0],y[0],z[0])\n", "x[0] = 2*x[0] - 3\n", "f(x,y,z)\n", "print \"x = %d , y = %d , z = %d\" %(x[0],y[0],z[0])\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "x = 22 , y = 33 , z = 44\n", "x = 66 , y = 77 , z = 44\n", "x = 66 , y = 77 , z = 44\n", "x = 173 , y = 121 , z = 44\n", "x = 173 , y = 121 , z = 44\n" ] } ], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def cube(x):\n", " # returns cube of x:\n", " return x*x*x\n", "\n", "# tests the cube() function:\n", "print cube(4)\n", "x = int(raw_input())\n", "y = cube(2*x-3)\n", "print y\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "64\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "343\n" ] } ], "prompt_number": 19 }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "x = 11\n", "\n", "def f():\n", " x = 44\n", " print \"In f(): x = %d\" % x \n", "\n", "def g():\n", " print \"In g(): x = %d\" % x \n", "\n", "x = 22\n", "x = 33\n", "print \"In block inside main(): x = %d\" % x\n", "\n", "\n", "print \"In main(): x = %d\" % x \n", "print \"In main(): ::x = %d\" % x \n", "f()\n", "g()\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "In block inside main(): x = 33\n", "In main(): x = 33\n", "In main(): ::x = 33\n", "In f(): x = 44\n", "In g(): x = 33\n" ] } ], "prompt_number": 20 }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "def max_(x, y,z=0):\n", " if x > y and x > y:\n", " return x\n", " elif y > x and y > z:\n", " return y\n", " else:\n", " return z\n", " \n", " \n", "print max(99,77), \" \" , max(55,66,33)\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "99 66\n" ] } ], "prompt_number": 21 }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "# prints the quotient of two input integers:\n", "print \"Enter two integers: \"\n", "n = int(raw_input())\n", "d = int(raw_input())\n", "if (d == 0):\n", " import sys\n", " sys.exit(0)\n", "print n , \"/\" , d , \" = \" , n/d \n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter two integers: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "8\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "2\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "8 / 2 = 4\n" ] } ], "prompt_number": 22 }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def reciprocal(x):\n", " #returns the reciprocal of x:\n", " if (x == 0):\n", " import sys\n", " sys.exit(1); # terminate the program\n", " return 1.0/x\n", "\n", "x = float(raw_input())\n", "print reciprocal(x)\n" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "25\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "0.04\n" ] } ], "prompt_number": 23 }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def p(x,a0,a1=0,a2=0,a3=0):\n", " # returns a0 + a1*x + a2*x^2 + a3*x^3:\n", " return (a0 + (a1 + (a2 + a3*x)*x)*x)\n", "\n", "\n", "# tests the p() function:\n", "x = 2.0003\n", "print \"p(x,7) = %f\" % p(x,7)\n", "print \"p(x,7,6) = %f\" % p(x,7,6)\n", "print \"p(x,7,6,5) = %f\" % p(x,7,6,5)\n", "print \"p(x,7,6,5,4) = %f\" % p(x,7,6,5,4)\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "p(x,7) = 7.000000\n", "p(x,7,6) = 19.001800\n", "p(x,7,6,5) = 39.007800\n", "p(x,7,6,5,4) = 71.022203\n" ] } ], "prompt_number": 24 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }