From: Michael Droettboom Date: Thu, 14 Jun 2012 16:51:39 +0000 (-0400) Subject: Add if, for loop, def X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=d99786f186fd20e755ed9830b1745f51d31b0fd6;p=swc-testing-nose.git Add if, for loop, def --- diff --git a/Python/PresenterNotes.ipynb b/Python/PresenterNotes.ipynb index b0425dc..2051fbb 100644 --- a/Python/PresenterNotes.ipynb +++ b/Python/PresenterNotes.ipynb @@ -658,6 +658,309 @@ "1. Make a dictionary and experiment using different types as keys. Can containers be keys?" ] }, + { + "cell_type": "markdown", + "source": [ + "# If statements" + ] + }, + { + "cell_type": "code", + "collapsed": true, + "input": [ + "x = 5", + "if x < 0:", + " print \"x is negative\"" + ], + "language": "python", + "outputs": [], + "prompt_number": 1 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "x = -5", + "if x < 0:", + " print \"x is negative\"" + ], + "language": "python", + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "x is negative" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "x = 5", + "if x < 0:", + " print \"x is negative\"", + "else:", + " print \"x in non-negative\"" + ], + "language": "python", + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "x in non-negative" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "x = 5", + "if x < 0:", + " print \"x is negative\"", + "elif x == 0:", + " print \"x is zero\"", + "else:", + " print \"x is positive\"" + ], + "language": "python", + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "x is positive" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "markdown", + "source": [ + "# Exercises", + "1. Write an if statement that prints whether x is even or odd" + ] + }, + { + "cell_type": "markdown", + "source": [ + "# Looping" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "fruits = ['apples', 'oranges', 'pears', 'bananas']", + "for fruit in fruits:", + " print fruit" + ], + "language": "python", + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "apples", + "oranges", + "pears", + "bananas" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Use range for a range on integers", + "for i in range(len(fruits)):", + " print i, fruits[i]" + ], + "language": "python", + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "0 apples", + "1 oranges", + "2 pears", + "3 bananas" + ] + } + ], + "prompt_number": 17 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Use zip to iterate over two lists at once", + "fruits = ['apples', 'oranges', 'pears', 'bananas']", + "prices = [0.49, 0.99, 1.49, 0.32]", + "for fruit, price in zip(fruits, prices):", + " print fruit, \"cost\", price, \"each\"", + " " + ], + "language": "python", + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "apples cost 0.49 each", + "oranges cost 0.99 each", + "pears cost 1.49 each", + "bananas cost 0.32 each" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Use \"items\" to iterate over a dictionary", + "# Note the order is non-deterministic", + "prices = {'apples': 0.49, 'oranges': 0.99, 'pears': 1.49, 'bananas': 0.32}", + "for fruit, price in prices.items():", + " print fruit, \"cost\", price, \"each\"", + " " + ], + "language": "python", + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "pears cost 1.49 each", + "apples cost 0.49 each", + "oranges cost 0.99 each", + "bananas cost 0.32 each" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "values = [42.0, 0.01, -32.0, -16.0, 0.0]", + "values.sort()", + "for x in values:", + " if x > 0.0:", + " print \"First non-negative value is\", x", + " break" + ], + "language": "python", + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "First non-negative value is 0.01" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Use continue to skip an iteration", + "for x in values:", + " if x <= 0.0:", + " continue", + " print math.log(x)" + ], + "language": "python", + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "-4.60517018599", + "3.73766961828" + ] + } + ], + "prompt_number": 13 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Calculating a sum", + "sum = 0", + "for x in values:", + " sum = sum + x", + "sum" + ], + "language": "python", + "outputs": [ + { + "output_type": "pyout", + "prompt_number": 15, + "text": [ + "-5.990000000000002" + ] + } + ], + "prompt_number": 15 + }, + { + "cell_type": "markdown", + "source": [ + "# Exercises", + "1. Using a loop, calculate the factorial of 42 (the product of all integers less than 42)." + ] + }, + { + "cell_type": "markdown", + "source": [ + "# Functions" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def square(x):", + " return x * x", + "print square(2)" + ], + "language": "python", + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "4" + ] + } + ], + "prompt_number": 16 + }, + { + "cell_type": "markdown", + "source": [ + "# Exercises", + "1. Create a function that returns the factorial of a given number" + ] + }, { "cell_type": "code", "collapsed": true,