"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,