{ "metadata": { "name": "", "signature": "sha256:be4c865c4a86c0cc5a7c7497b49a4eca3de54b6c5955d179c6fbe954c9118aff" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Basic Training\n", "\n", "This material was heavily influenced by (stolen from) the UC Berkeley Python Bootcamp material." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Getting Python to talk back" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"Hello World!\"" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print 'Hello World!'" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# This is a comment.\n", "print 'This is not a comment.'" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "'I think Jeremy Perkins smells funny.'" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ " " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "### Python as a calculator" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are four distinct **numeric** types in Python:\n", " - *int* (32 bit, equivalent to *long* in C)\n", " - *long* (unlimited precision)\n", " - *float* (equivalent to *double* in C)\n", " - *complex* (real + imaginary parts, both *floats*)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print 2 + 2" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# Spaces between characters don't matter\n", "\n", "print 2+2" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "2 + 2" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"2 + 2\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print 2.1 + 2 # The most precise value is a float." ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "(3.*10. - 26.)/5." ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "(3*10 - 26)/5.0" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# Since our most precise value is an int, python spits out the solution as an int\n", "\n", "(3*10 - 26)/5" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# Rounding errors can creep in\n", "2.1 + 2 == 4.0999999999999996 # two 'equals' signs asks whether something is equal" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "complex(1,2)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# note that python uses j to denote the imaginary part\n", "\n", "1+2j" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "a = 1+2j\n", "print a.real, a.imag" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "1+2j-2j" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ " " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "### Defining and using variables" ] }, { "cell_type": "code", "collapsed": false, "input": [ "t = 1.0 # declare a variable t (time)\n", "accel = 9.8 # acceleration in units of m/s^2\n", "dist = 0.5*accel*t*t # distance traveled in time t seconds is 1/2*a*t^2\n", "print dist # this is the distance in meters" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "dist1 = accel*(t**2)/2 # note: t^2 means something very different!\n", "print dist1" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "dist2 = 0.5*accel*pow(t,2)\n", "print dist2" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "### Some more mathy operators" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Integer division prints the floor; i.e., it only takes the integer digits\n", "\n", "print 6/5" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# modulo operator\n", "6 % 5" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# bitwise operators: shift left\n", "# 1 in binary is '1', shifting left by two bits gives '100' = 4\n", "\n", "1 << 2" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# bitwise operators: shift right\n", "# 5 in binary is '101', shifting right by one bit gives '10' = 2\n", "\n", "5 >> 1" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "x = 2 ; y = 3 # multiple commands on the same line, separated by a semicolon\n", "x | y # bitwise OR\n", " # x in binary is '10', y in binary is '11', x | y is '11' -> 3" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "x ^ y # exclusive OR ('10' ^ '11' = '01' = 1)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "x & y # bitwise AND ('10' & '11' = '10' = 2)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "x = x ^ y ; print x # x has been reassigned" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "x += 3 ; print x # 'x += 3' is the same as saying 'x = x+3'\n", " # the equivalent holds from -, *, /" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "## Comparisons" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = 3 ; b = 4\n", "a == b # two '=' signs for comparison, one '=' for assignment" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "a+1 == b" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "a+1.0 == b" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "a < 10" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "a < 3" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "a <= 3" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "a < (10 + 2j)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "a < -2.0" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "a != 3.1415" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "## Truthiness" ] }, { "cell_type": "code", "collapsed": false, "input": [ "0 == False # False is equivalent to 0, and other things" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "1 == True # True is equivalent to 1, and other things" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "not False" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "0.0 == False" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "0j == False" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "not (10.0 - 10.0)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "x = None # None is neither True nor False\n", "print None == False\n", "print None == True" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "type(True)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "## More on variables and types" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print type(1)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print type(\"1\")" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "x = 2 ; type(x)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "type(2) == type(1)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "type(False)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "type(type(1))" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "type(pow)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "isinstance(1,int) # check if something is a certain type" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "isinstance(\"spam\",str)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "isinstance(1.212,int)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Built-in types in python: *int*, *bool*, *str*, *float*, *complex*, *long* ...\n", "\n", "See https://docs.python.org/2/library/stdtypes.html" ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "## Strings\n", "\n", "Strings are sequences of characters.\n", "* They can be indexed and sliced up as if they were an array (we'll talk more about arrays later).\n", "* You can glue strings together with + signs.\n", "\n", "Strings can be formatted and compared." ] }, { "cell_type": "code", "collapsed": false, "input": [ "x = \"spam\" ; print type(x)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"Hello!\\nI'm hungry.\" # '\\n' means 'new line'" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# This doesn't work.\n", "print \"Hello!\n", "I'm hungry.\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "\"Hello!\\nI'm hungry.\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "\"Wah?!\" == \"Wah?!\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"'Wah?!' said the student.\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"\"Wah?!\" said the student.\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print '\"Wah?!\" said the student.'" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"\\\"Wah?!\\\" said the student.\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Backslashes ( \\ ) start special (escape) characters:\n", "\n", " \\n = newline\n", " \\r = return\n", " \\t = tab\n", " \\a = bell\n", " \n", "String literals are defined with double (\") or single quotes (').
\n", "The outermost type of quotation mark cannot be used inside a string -- UNLESS it's escaped with a backslash.\n", "\n", "See http://docs.python.org/reference/lexical_analysis.html#string-literals" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Raw strings don't recognize escape characters\n", "\n", "print r'This is a raw string ... newlines \\n are ignored. So are returns \\r and tabs \\t.'" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# Triple quotes are useful for multiple line strings\n", "\n", "y = '''Four score and seven minutes ago,\n", " you folks all learned some basic mathy stuff with Python\n", " and boy were you blown away!'''\n", "print y" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# Prepending 'u' makes a string \"unicode\"\n", "\n", "print u\"\\N{BLACK HEART SUIT}\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# You can concatenate strings with the '+' sign\n", "\n", "s = \"spam\" ; e = \"eggs\"\n", "\n", "print s + e" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print s + \" and \" + e" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"green \" + e + \" and \" + s" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# You can do multiple concatenations with the '*' sign\n", "\n", "print s*3 + e" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"*\"*50" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# Strings can be compared\n", "\n", "print \"spam\" == \"good\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# s comes before z in the alphabet\n", "\n", "\"spam\" < \"zoo\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# 's' comes before 'spam' in the dictionary\n", "\n", "\"s\" < \"spam\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# 'spaa' comes before 'spam' alphabetically\n", "\n", "\"spaaaaaaaaaaaam\" < \"spam\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print 'I want ' + 3 + ' eggs and no ' + s" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# We have to cast the 3 (an int) as a string\n", "\n", "print 'I want ' + str(3) + ' eggs and no ' + s" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "pi = 3.14159 # There are easier ways to call pi, which we'll see later\n", "print 'I want ' + str(pi) + ' eggs and no ' + s" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print str(True) + ':' + ' I want ' + str(pi) + ' eggs and no ' + s" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print s\n", "print len(s) # len() tells you the length of a string (or, more generally, an array)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print len(\"eggs\\n\") # The newline \\n counts as ONE character\n", "print len(\"\") # empty string" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# Strings act like arrays. We'll see more about arrays later.\n", "s = \"SPAM\"\n", "\n", "print s\n", "print s[0] # Python uses zero-based indexing; i.e., it starts counting at zero\n", "print s[1]\n", "print s[-1]\n", "print s[-2]" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# Take slices of strings\n", "print s\n", "print s[0:1]\n", "print s[1:4]\n", "print s[0:100] # Python doesn't warn you. Be careful!" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Slice counting backwards\n", "print s\n", "print s[-3:-1]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# You don't have to specify both ends\n", "print s\n", "print s[:2]\n", "print s[2:]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# You can slice in different steps\n", "print s\n", "print s[::2]\n", "print s[::-1]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "\n", "Strings are **immutable** (unlike in C), so you cannot change a string in place." ] }, { "cell_type": "code", "collapsed": false, "input": [ "mygrade = 'F+'\n", "print mygrade" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "mygrade[0] = 'A'" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# Ask for user input\n", "\n", "faren = raw_input(\"Enter the temperature (in Fahrenheit): \")\n", "print \"Your temperature is \" + faren + \" degrees.\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# User input is always saved as a string\n", "\n", "faren = raw_input(\"Enter the temperature in Fahrenheit): \")\n", "cel = 5./9. * (faren - 32.)\n", "print \"The temperature in Celcius is \" + cel + \" degrees.\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# Don't forget to convert things to the right type\n", "\n", "faren = raw_input(\"Enter the temperature in Fahrenheit): \")\n", "faren = float(faren) # The calculation on the right gets saved to the variable on the left\n", "cel = 5./9. * (faren - 32.)\n", "print \"The temperature in Celcius is \" + str(cel) + \" degrees.\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "## Loops and branches in python\n", "\n", "Python has the usual control flow statements:\n", "- *if*, *else*, *elif*\n", "- *for* loops\n", "- *while* loops\n", "- *break*, *continue*, *pass*\n", "\n", "Indentation in Python defines where blocks begin and end." ] }, { "cell_type": "code", "collapsed": false, "input": [ "x = 1\n", " print x" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "x = 1\n", "if x > 0: # colons indicate the beginning of a control statement\n", " print \"yo\"\n", "else: # unindenting tells Python to move to the next case\n", " print \"dude\"\n", "print \"ok\" # unindenting also tells Python the control statement is done" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "IPython Notebook automatically converts tabs into spaces, but some programs do not. **Be careful not to mix these up!** Be consistent in your programming.\n", "\n", "If you're working within the Python interpreter (not the IPython Notebook), you'll see this:\n", "\n", " >>> x = 1\n", " >>> if x > 0:\n", " ... print \"yo\"\n", " ... else:\n", " ... print \"dude\"\n", " ... print \"ok\"\n", " ...\n", " yo\n", " ok" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# You can mix indentations between different blocks ... but this is ugly and people will judge you\n", "\n", "x = 1\n", "if x > 0:\n", " print \"yo\"\n", "else:\n", " print \"dude\"" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# You can put everything on one line\n", "\n", "print \"yo\" if x > 0 else \"dude\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# Multiple cases\n", "\n", "x = 1\n", "if x < -10:\n", " print \"yo\"\n", "elif x > 10: # 'elif' is short for 'else if'\n", " print \"dude\"\n", "else:\n", " print \"sup\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "for x in range(5):\n", " print x**2" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "for x in (\"all\",\"we\",\"wanna\",\"do\",\"is\",\"eat\",\"your\",\"brains\"):\n", " print x" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "x = 0\n", "while x < 5:\n", " print pow(2,x)\n", " x += 1 # don't forget to increment x!" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# Multiple levels\n", "for x in range(1,10):\n", " if x % 2 == 0:\n", " print str(x) + \" is even.\"\n", " else:\n", " print str(x) + \" is odd.\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# Blocks cannot be empty\n", "\n", "x = \"fried goldfish\"\n", "if x == \"spam for dinner\":\n", " print \"I will destroy the universe\"\n", "else:\n", " # Nothing here." ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# Use a 'pass' statement, which indicates 'do nothing'\n", "\n", "x = \"fried goldfish\"\n", "if x == \"spam for dinner\":\n", " print \"I will destroy the universe\"\n", "else:\n", " pass" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# Use a 'break' statement to escape a loop\n", "\n", "x = 0\n", "while True:\n", " print x**2\n", " if x**2 >= 100:\n", " break\n", " x +=1" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "## Breakout exercise\n", "\n", "Write a program that allows the user to build up a sentence one word at a time, stopping only when they enter a period (.), exclamation (!), or a question mark (?).\n", "\n", "Example interaction:\n", "\n", " Please enter a word in the sentence (enter . ! or ? to end): My\n", " ...currently: My\n", " Please enter a word in the sentence (enter . ! or ? to end): name\n", " ...currently: My name\n", " Please enter a word in the sentence (enter . ! or ? to end): is\n", " ...currently: My name is\n", " Please enter a word in the sentence (enter . ! or ? to end): Slim\n", " ...currently: My name is Slim\n", " Please enter a word in the sentence (enter . ! or ? to end): Shady\n", " ...currently: My name is Slim Shady\n", " Please enter a word in the sentence (enter . ! or ? to end): !\n", " --->My name is Slim Shady!" ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }