{ "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", "execution_count": 1, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello World!\n" ] } ], "source": [ "print \"Hello World!\"" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello World!\n" ] } ], "source": [ "print 'Hello World!'" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "This is not a comment.\n" ] } ], "source": [ "# This is a comment.\n", "print 'This is not a comment.'" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'I think Jeremy Perkins smells funny.'" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'I think Jeremy Perkins smells funny.'" ] }, { "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", "execution_count": 5, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n" ] } ], "source": [ "print 2 + 2" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n" ] } ], "source": [ "# Spaces between characters don't matter\n", "\n", "print 2+2" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 + 2" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2 + 2\n" ] } ], "source": [ "print \"2 + 2\"" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4.1\n" ] } ], "source": [ "print 2.1 + 2 # The most precise value is a float." ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0.8" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(3.*10. - 26.)/5." ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0.8" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(3*10 - 26)/5.0" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Since our most precise value is an int, python spits out the solution as an int\n", "\n", "(3*10 - 26)/5" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Rounding errors can creep in\n", "2.1 + 2 == 4.0999999999999996 # two 'equals' signs asks whether something is equal" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(1+2j)" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "complex(1,2)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(1+2j)" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# note that python uses j to denote the imaginary part\n", "\n", "1+2j" ] }, { "cell_type": "code", <<<<<<< HEAD "execution_count": 20, ======= "execution_count": 16, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.0 2.0\n" ] } ], "source": [ "a = 1+2j\n", "print a.real, a.imag" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(1+0j)" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1+2j-2j" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "### Defining and using variables" ] }, { "cell_type": "code", <<<<<<< HEAD "execution_count": 21, ======= "execution_count": 18, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4.9\n" ] } ], "source": [ "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" ] }, { "cell_type": "code", <<<<<<< HEAD "execution_count": 22, ======= "execution_count": 19, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4.9\n" ] } ], "source": [ "dist1 = accel*(t**2)/2 # note: t^2 means something very different!\n", "print dist1" ] }, { "cell_type": "code", <<<<<<< HEAD "execution_count": 25, ======= "execution_count": 20, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4.9\n" ] } ], "source": [ "dist2 = 0.5*accel*pow(t,2)\n", "print dist2" ] }, { "cell_type": "code", <<<<<<< HEAD "execution_count": null, ======= "execution_count": 20, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "### Some more mathy operators" ] }, { "cell_type": "code", <<<<<<< HEAD "execution_count": 26, ======= "execution_count": 21, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n" ] } ], "source": [ "# Integer division prints the floor; i.e., it only takes the integer digits\n", "\n", "print 6/5" ] }, { "cell_type": "code", <<<<<<< HEAD "execution_count": 27, ======= "execution_count": 22, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "1" ] }, <<<<<<< HEAD "execution_count": 27, ======= "execution_count": 22, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": {}, "output_type": "execute_result" } ], "source": [ "# modulo operator\n", "6 % 5" ] }, { "cell_type": "code", <<<<<<< HEAD "execution_count": 28, ======= "execution_count": 23, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "4" ] }, <<<<<<< HEAD "execution_count": 28, ======= "execution_count": 23, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": {}, "output_type": "execute_result" } ], "source": [ "# bitwise operators: shift left\n", "# 1 in binary is '1', shifting left by two bits gives '100' = 4\n", "\n", "1 << 2" ] }, { "cell_type": "code", <<<<<<< HEAD "execution_count": 29, ======= "execution_count": 24, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "2" ] }, <<<<<<< HEAD "execution_count": 29, ======= "execution_count": 24, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": {}, "output_type": "execute_result" } ], "source": [ "# bitwise operators: shift right\n", "# 5 in binary is '101', shifting right by one bit gives '10' = 2\n", "\n", "5 >> 1" ] }, { "cell_type": "code", <<<<<<< HEAD "execution_count": 30, ======= "execution_count": 25, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "3" ] }, <<<<<<< HEAD "execution_count": 30, ======= "execution_count": 25, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": {}, "output_type": "execute_result" } ], "source": [ "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" ] }, { "cell_type": "code", <<<<<<< HEAD "execution_count": 31, ======= "execution_count": 26, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "1" ] }, <<<<<<< HEAD "execution_count": 31, ======= "execution_count": 26, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": {}, "output_type": "execute_result" } ], "source": [ "x ^ y # exclusive OR ('10' ^ '11' = '01' = 1)" ] }, { "cell_type": "code", <<<<<<< HEAD "execution_count": 32, ======= "execution_count": 27, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "2" ] }, <<<<<<< HEAD "execution_count": 32, ======= "execution_count": 27, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": {}, "output_type": "execute_result" } ], "source": [ "x & y # bitwise AND ('10' & '11' = '10' = 2)" ] }, { "cell_type": "code", <<<<<<< HEAD "execution_count": 33, ======= "execution_count": 28, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n" ] } ], "source": [ "x = x ^ y ; print x # x has been reassigned" ] }, { "cell_type": "code", <<<<<<< HEAD "execution_count": 36, ======= "execution_count": 29, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ <<<<<<< HEAD "10\n" ======= "4\n" >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d ] } ], "source": [ "x += 3 ; print x # 'x += 3' is the same as saying 'x = x+3'\n", " # the equivalent holds from -, *, /" ] }, { "cell_type": "code", <<<<<<< HEAD "execution_count": null, ======= "execution_count": 29, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "## Comparisons" ] }, { "cell_type": "code", <<<<<<< HEAD "execution_count": 37, ======= "execution_count": 30, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "False" ] }, <<<<<<< HEAD "execution_count": 37, ======= "execution_count": 30, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 3 ; b = 4\n", "a == b # two '=' signs for comparison, one '=' for assignment" ] }, { "cell_type": "code", <<<<<<< HEAD "execution_count": 38, ======= "execution_count": 31, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, <<<<<<< HEAD "execution_count": 38, ======= "execution_count": 31, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": {}, "output_type": "execute_result" } ], "source": [ "a+1 == b" ] }, { "cell_type": "code", <<<<<<< HEAD "execution_count": 39, ======= "execution_count": 32, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, <<<<<<< HEAD "execution_count": 39, ======= "execution_count": 32, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": {}, "output_type": "execute_result" } ], "source": [ "a+1.0 == b" ] }, { "cell_type": "code", <<<<<<< HEAD "execution_count": 40, ======= "execution_count": 33, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, <<<<<<< HEAD "execution_count": 40, ======= "execution_count": 33, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": {}, "output_type": "execute_result" } ], "source": [ "a < 10" ] }, { "cell_type": "code", <<<<<<< HEAD "execution_count": 41, ======= "execution_count": 34, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "False" ] }, <<<<<<< HEAD "execution_count": 41, ======= "execution_count": 34, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": {}, "output_type": "execute_result" } ], "source": [ "a < 3" ] }, { "cell_type": "code", <<<<<<< HEAD "execution_count": 42, ======= "execution_count": 35, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, <<<<<<< HEAD "execution_count": 42, ======= "execution_count": 35, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": {}, "output_type": "execute_result" } ], "source": [ "a <= 3" ] }, { "cell_type": "code", <<<<<<< HEAD "execution_count": 43, ======= "execution_count": 36, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": { "collapsed": false }, "outputs": [ { "ename": "TypeError", "evalue": "no ordering relation is defined for complex numbers", "output_type": "error", "traceback": [ <<<<<<< HEAD "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0ma\u001b[0m \u001b[0;34m<\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;36m10\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;36m2j\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", ======= "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0ma\u001b[0m \u001b[0;34m<\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;36m10\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;36m2j\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "\u001b[0;31mTypeError\u001b[0m: no ordering relation is defined for complex numbers" ] } ], "source": [ "a < (10 + 2j)" ] }, { "cell_type": "code", <<<<<<< HEAD "execution_count": 44, ======= "execution_count": 37, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "False" ] }, <<<<<<< HEAD "execution_count": 44, ======= "execution_count": 37, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": {}, "output_type": "execute_result" } ], "source": [ "a < -2.0" ] }, { "cell_type": "code", <<<<<<< HEAD "execution_count": 45, ======= "execution_count": 38, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, <<<<<<< HEAD "execution_count": 45, ======= "execution_count": 38, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": {}, "output_type": "execute_result" } ], "source": [ "a != 3.1415" ] }, { "cell_type": "code", <<<<<<< HEAD "execution_count": null, ======= "execution_count": 38, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "## Truthiness" ] }, { "cell_type": "code", <<<<<<< HEAD "execution_count": 46, ======= "execution_count": 39, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, <<<<<<< HEAD "execution_count": 46, ======= "execution_count": 39, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": {}, "output_type": "execute_result" } ], "source": [ "0 == False # False is equivalent to 0, and other things" ] }, { "cell_type": "code", <<<<<<< HEAD "execution_count": 55, ======= "execution_count": 40, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, <<<<<<< HEAD "execution_count": 55, ======= "execution_count": 40, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 == True # True is equivalent to 1, and other things" ] }, { "cell_type": "code", <<<<<<< HEAD "execution_count": 48, ======= "execution_count": 41, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, <<<<<<< HEAD "execution_count": 48, ======= "execution_count": 41, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": {}, "output_type": "execute_result" } ], "source": [ "not False" ] }, { "cell_type": "code", <<<<<<< HEAD "execution_count": 52, ======= "execution_count": 42, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ <<<<<<< HEAD "False" ] }, "execution_count": 52, ======= "True" ] }, "execution_count": 42, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": {}, "output_type": "execute_result" } ], "source": [ <<<<<<< HEAD "1 == False" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "# 0j == False" ======= "0.0 == False" >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d ] }, { "cell_type": "code", <<<<<<< HEAD "execution_count": 56, ======= "execution_count": 43, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, <<<<<<< HEAD "execution_count": 56, ======= "execution_count": 43, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": {}, "output_type": "execute_result" } ], "source": [ <<<<<<< HEAD "not (10.0 - 10.0)" ] }, { "cell_type": "code", "execution_count": 58, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n", "False\n" ] } ], "source": [ "x = None # None is neither True nor False\n", "print None == False\n", "print None == True" ] }, { "cell_type": "code", "execution_count": 64, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "NoneType" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(None)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "## More on variables and types" ] }, { "cell_type": "code", "execution_count": 65, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print type(1)" ] }, { "cell_type": "code", "execution_count": 66, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print type(\"1\")" ] }, { "cell_type": "code", "execution_count": 67, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = 2 ; type(x)" ] }, { "cell_type": "code", "execution_count": 68, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 68, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(2) == type(1)" ] }, { "cell_type": "code", "execution_count": 69, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "bool" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(False)" ] }, { "cell_type": "code", "execution_count": 70, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "type" ] }, "execution_count": 70, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(type(1))" ======= "0j == False" >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d ] }, { "cell_type": "code", <<<<<<< HEAD "execution_count": 71, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "builtin_function_or_method" ] }, "execution_count": 71, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(pow)" ] }, { "cell_type": "code", "execution_count": 72, ======= "execution_count": 44, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, <<<<<<< HEAD "execution_count": 72, ======= "execution_count": 44, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": {}, "output_type": "execute_result" } ], "source": [ <<<<<<< HEAD "isinstance(1,int) # check if something is a certain type" ] }, { "cell_type": "code", "execution_count": 73, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 73, "metadata": {}, "output_type": "execute_result" } ], "source": [ "isinstance(\"spam\",str)" ] }, { "cell_type": "code", "execution_count": 74, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 74, "metadata": {}, "output_type": "execute_result" } ], "source": [ "isinstance(1.212,int)" ] }, { "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", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "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", "execution_count": 75, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "x = \"spam\" ; print type(x)" ======= "not (10.0 - 10.0)" >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d ] }, { "cell_type": "code", <<<<<<< HEAD "execution_count": 76, ======= "execution_count": 45, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ <<<<<<< HEAD "Hello!\n", "I'm hungry.\n" ======= "False\n", "False\n" >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d ] } ], "source": [ <<<<<<< HEAD "print \"Hello!\\nI'm hungry.\" # '\\n' means 'new line'" ======= "x = None # None is neither True nor False\n", "print None == False\n", "print None == True" >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d ] }, { "cell_type": "code", <<<<<<< HEAD "execution_count": 79, "metadata": { "collapsed": false }, "outputs": [ { "ename": "SyntaxError", "evalue": "EOL while scanning string literal (, line 2)", "output_type": "error", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m2\u001b[0m\n\u001b[0;31m print \"Hello!\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m EOL while scanning string literal\n" ] } ], "source": [ "# This doesn't work.\n", "print \"Hello! \n", "I'm hungry.\"" ] }, { "cell_type": "code", "execution_count": 80, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello! \n", " I'm hungry.\n" ] } ], "source": [ "print \"Hello! \\n I'm hungry.\"" ] }, { "cell_type": "code", "execution_count": 81, ======= "execution_count": 46, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ <<<<<<< HEAD "True" ] }, "execution_count": 81, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"Wah?!\" == \"Wah?!\"" ] }, { "cell_type": "code", "execution_count": 82, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'Wah?!' said the student.\n" ] } ], "source": [ "print \"'Wah?!' said the student.\"" ] }, { "cell_type": "code", "execution_count": 83, "metadata": { "collapsed": false }, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (, line 1)", "output_type": "error", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m print \"\"Wah?!\" said the student.\"\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" ] } ], "source": [ "print \"\"Wah?!\" said the student.\"" ] }, { "cell_type": "code", "execution_count": 84, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\"Wah?!\" said the student.\n" ] } ], "source": [ "print '\"Wah?!\" said the student.'" ] }, { "cell_type": "code", "execution_count": 85, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\"Wah?!\" said the student.\n" ] } ], "source": [ "print \"\\\"Wah?!\\\" said the student.\"" ] }, { "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", "execution_count": 86, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "This is a raw string ... newlines \\n are ignored. So are returns \\r and tabs \\t.\n" ] } ], "source": [ "# 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.'" ] }, { "cell_type": "code", "execution_count": 87, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "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" ] } ], "source": [ "# 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" ] }, { "cell_type": "code", "execution_count": 88, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "♥\n" ] } ], "source": [ "# Prepending 'u' makes a string \"unicode\"\n", "\n", "print u\"\\N{BLACK HEART SUIT}\"" ] }, { "cell_type": "code", "execution_count": 89, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "spameggs\n" ] } ], "source": [ "# You can concatenate strings with the '+' sign\n", "\n", "s = \"spam\" ; e = \"eggs\"\n", "\n", "print s + e" ] }, { "cell_type": "code", "execution_count": 90, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "spam and eggs\n" ] } ], "source": [ "print s + \" and \" + e" ] }, { "cell_type": "code", "execution_count": 91, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "green eggs and spam\n" ] } ], "source": [ "print \"green \" + e + \" and \" + s" ] }, { "cell_type": "code", "execution_count": 92, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "spamspamspameggs\n" ] } ], "source": [ "# You can do multiple concatenations with the '*' sign\n", "\n", "print s*3 + e" ] }, { "cell_type": "code", "execution_count": 93, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "**************************************************\n" ] } ], "source": [ "print \"*\"*50" ] }, { "cell_type": "code", "execution_count": 94, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n" ] } ], "source": [ "# Strings can be compared\n", "\n", "print \"spam\" == \"good\"" ] }, { "cell_type": "code", "execution_count": 95, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 95, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# s comes before z in the alphabet\n", "\n", "\"spam\" < \"zoo\"" ] }, { "cell_type": "code", "execution_count": 96, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 96, ======= "bool" ] }, "execution_count": 46, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": {}, "output_type": "execute_result" } ], "source": [ <<<<<<< HEAD "# 's' comes before 'spam' in the dictionary\n", "\n", "\"s\" < \"spam\"" ======= "type(True)" >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d ] }, { "cell_type": "code", <<<<<<< HEAD "execution_count": 97, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 97, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 'spaa' comes before 'spam' alphabetically\n", "\n", "\"spaaaaaaaaaaaam\" < \"spam\"" ======= "execution_count": 46, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "## More on variables and types" >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d ] }, { "cell_type": "code", <<<<<<< HEAD "execution_count": 98, ======= "execution_count": 47, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": { "collapsed": false }, "outputs": [ { <<<<<<< HEAD "ename": "TypeError", "evalue": "cannot concatenate 'str' and 'int' objects", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mprint\u001b[0m \u001b[0;34m'I want '\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;36m3\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;34m' eggs and no '\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0ms\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: cannot concatenate 'str' and 'int' objects" ======= "name": "stdout", "output_type": "stream", "text": [ "\n" >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d ] } ], "source": [ <<<<<<< HEAD "print 'I want ' + 3 + ' eggs and no ' + s" ======= "print type(1)" >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d ] }, { "cell_type": "code", <<<<<<< HEAD "execution_count": 99, ======= "execution_count": 48, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ <<<<<<< HEAD "I want 3 eggs and no spam\n" ======= "\n" >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d ] } ], "source": [ <<<<<<< HEAD "# We have to cast the 3 (an int) as a string\n", "\n", "print 'I want ' + str(3) + ' eggs and no ' + s" ======= "print type(\"1\")" >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d ] }, { "cell_type": "code", <<<<<<< HEAD "execution_count": 100, ======= "execution_count": 49, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": { "collapsed": false }, "outputs": [ { <<<<<<< HEAD "name": "stdout", "output_type": "stream", "text": [ "I want 3.14159 eggs and no spam\n" ] } ], "source": [ "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" ======= "data": { "text/plain": [ "int" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = 2 ; type(x)" ] }, { "cell_type": "code", "execution_count": 50, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(2) == type(1)" >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d ] }, { "cell_type": "code", <<<<<<< HEAD "execution_count": 101, ======= "execution_count": 51, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": { "collapsed": false }, "outputs": [ { <<<<<<< HEAD "name": "stdout", "output_type": "stream", "text": [ "True: I want 3.14159 eggs and no spam\n" ======= "data": { "text/plain": [ "bool" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(False)" ] }, { "cell_type": "code", "execution_count": 52, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "type" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(type(1))" ] }, { "cell_type": "code", "execution_count": 53, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "builtin_function_or_method" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(pow)" ] }, { "cell_type": "code", "execution_count": 54, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "isinstance(1,int) # check if something is a certain type" ] }, { "cell_type": "code", "execution_count": 55, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "isinstance(\"spam\",str)" ] }, { "cell_type": "code", "execution_count": 56, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "isinstance(1.212,int)" ] }, { "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", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "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", "execution_count": 57, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d ] } ], "source": [ <<<<<<< HEAD "print str(True) + ':' + ' I want ' + str(pi) + ' eggs and no ' + s" ======= "x = \"spam\" ; print type(x)" >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d ] }, { "cell_type": "code", <<<<<<< HEAD "execution_count": 102, ======= "execution_count": 58, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ <<<<<<< HEAD "spam\n", "4\n" ======= "Hello!\n", "I'm hungry.\n" >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d ] } ], "source": [ <<<<<<< HEAD "print s\n", "print len(s) # len() tells you the length of a string (or, more generally, an array)" ======= "print \"Hello!\\nI'm hungry.\" # '\\n' means 'new line'" >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d ] }, { "cell_type": "code", <<<<<<< HEAD "execution_count": 103, ======= "execution_count": 59, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": { "collapsed": false }, "outputs": [ { <<<<<<< HEAD "name": "stdout", "output_type": "stream", "text": [ "5\n", "0\n" ======= "ename": "SyntaxError", "evalue": "EOL while scanning string literal (, line 2)", "output_type": "error", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m2\u001b[0m\n\u001b[0;31m print \"Hello!\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m EOL while scanning string literal\n" >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d ] } ], "source": [ <<<<<<< HEAD "print len(\"eggs\\n\") # The newline \\n counts as ONE character\n", "print len(\"\") # empty string" ======= "# This doesn't work.\n", "print \"Hello!\n", "I'm hungry.\"" >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d ] }, { "cell_type": "code", <<<<<<< HEAD "execution_count": 104, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "SPAM\n", "S\n", "P\n", "M\n", "A\n" ] } ], "source": [ "# 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]" ] }, { "cell_type": "code", "execution_count": 105, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "SPAM\n", "S\n", "PAM\n", "SPAM\n" ] } ], "source": [ "# 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!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "code", "execution_count": 106, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "SPAM\n", "PA\n" ] } ], "source": [ "# Slice counting backwards\n", "print s\n", "print s[-3:-1]" ] }, { "cell_type": "code", "execution_count": 107, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "SPAM\n", "SP\n", "AM\n" ] } ], "source": [ "# You don't have to specify both ends\n", "print s\n", "print s[:2]\n", "print s[2:]" ] }, { "cell_type": "code", "execution_count": 108, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "SPAM\n", "SA\n", "MAPS\n" ] } ], "source": [ "# You can slice in different steps\n", "print s\n", "print s[::2]\n", "print s[::-1]" ] }, { "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", "execution_count": 109, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "F+\n" ] } ], "source": [ "mygrade = 'F+'\n", "print mygrade" ] }, { "cell_type": "code", "execution_count": 112, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A+\n" ] } ], "source": [ "#mygrade[0] = 'A'\n", "mygrade = 'A+'\n", "print mygrade" ] }, { "cell_type": "code", "execution_count": 113, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter the temperature (in Fahrenheit): 78\n", "Your temperature is 78 degrees.\n" ] } ], "source": [ "# Ask for user input\n", "\n", "faren = raw_input(\"Enter the temperature (in Fahrenheit): \")\n", "print \"Your temperature is \" + faren + \" degrees.\"" ] }, { "cell_type": "code", "execution_count": 114, ======= "execution_count": 64, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ <<<<<<< HEAD "Enter the temperature in Fahrenheit): 78\n" ] }, { "ename": "TypeError", "evalue": "unsupported operand type(s) for -: 'str' and 'float'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mfaren\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mraw_input\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Enter the temperature in Fahrenheit): \"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mcel\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m5.\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0;36m9.\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mfaren\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0;36m32.\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 5\u001b[0m \u001b[0;32mprint\u001b[0m \u001b[0;34m\"The temperature in Celcius is \"\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mcel\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;34m\" degrees.\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for -: 'str' and 'float'" ======= "Hello! \n", " I'm hungry.\n" ] } ], "source": [ "print \"Hello! \\n I'm hungry.\"" ] }, { "cell_type": "code", "execution_count": 65, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"Wah?!\" == \"Wah?!\"" ] }, { "cell_type": "code", "execution_count": 66, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'Wah?!' said the student.\n" ] } ], "source": [ "print \"'Wah?!' said the student.\"" ] }, { "cell_type": "code", "execution_count": 67, "metadata": { "collapsed": false }, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (, line 1)", "output_type": "error", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m print \"\"Wah?!\" said the student.\"\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" ] } ], "source": [ "print \"\"Wah?!\" said the student.\"" ] }, { "cell_type": "code", "execution_count": 68, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\"Wah?!\" said the student.\n" ] } ], "source": [ "print '\"Wah?!\" said the student.'" ] }, { "cell_type": "code", "execution_count": 69, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\"Wah?!\" said the student.\n" ] } ], "source": [ "print \"\\\"Wah?!\\\" said the student.\"" ] }, { "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", "execution_count": 70, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "This is a raw string ... newlines \\n are ignored. So are returns \\r and tabs \\t.\n" ] } ], "source": [ "# 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.'" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# 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" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Prepending 'u' makes a string \"unicode\"\n", "\n", "print u\"\\N{BLACK HEART SUIT}\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "# You can concatenate strings with the '+' sign\n", "\n", "s = \"spam\" ; e = \"eggs\"\n", "\n", "print s + e" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print s + \" and \" + e" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print \"green \" + e + \" and \" + s" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# You can do multiple concatenations with the '*' sign\n", "\n", "print s*3 + e" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print \"*\"*50" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Strings can be compared\n", "\n", "print \"spam\" == \"good\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# s comes before z in the alphabet\n", "\n", "\"spam\" < \"zoo\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# 's' comes before 'spam' in the dictionary\n", "\n", "\"s\" < \"spam\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# 'spaa' comes before 'spam' alphabetically\n", "\n", "\"spaaaaaaaaaaaam\" < \"spam\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print 'I want ' + 3 + ' eggs and no ' + s" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# We have to cast the 3 (an int) as a string\n", "\n", "print 'I want ' + str(3) + ' eggs and no ' + s" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "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" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print str(True) + ':' + ' I want ' + str(pi) + ' eggs and no ' + s" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print s\n", "print len(s) # len() tells you the length of a string (or, more generally, an array)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print len(\"eggs\\n\") # The newline \\n counts as ONE character\n", "print len(\"\") # empty string" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "# 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]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# 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!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Slice counting backwards\n", "print s\n", "print s[-3:-1]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# You don't have to specify both ends\n", "print s\n", "print s[:2]\n", "print s[2:]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# You can slice in different steps\n", "print s\n", "print s[::2]\n", "print s[::-1]" ] }, { "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", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "mygrade = 'F+'\n", "print mygrade" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "mygrade[0] = 'A'" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Ask for user input\n", "\n", "faren = raw_input(\"Enter the temperature (in Fahrenheit): \")\n", "print \"Your temperature is \" + faren + \" degrees.\"" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter the temperature in Fahrenheit): 15\n" >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d ] } ], "source": [ "# 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.\"" ] }, { "cell_type": "code", "execution_count": 115, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter the temperature in Fahrenheit): 78\n", "The temperature in Celcius is 25.5555555556 degrees.\n" ] } ], "source": [ "# 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.\"" ] }, { "cell_type": "raw", "metadata": {}, "source": [ "x = raw_input()\n", "x = float(x)\n", "print x" ] }, { "cell_type": "code", "execution_count": 118, "metadata": { "collapsed": false }, "outputs": [ { "ename": "NameError", "evalue": "name 'u' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mu\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mNameError\u001b[0m: name 'u' is not defined" ] } ], "source": [ "u" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Running a Program from Command Line\n", "\n", "This is a good of place as any to introduce the show that you can run you don't HAVE to use the notebook.\n", "\n", "Everyone should have a file called temperature.py, open the file in your text editor (emacs, vim, notepad) of choice at look at its content. \n", "You will see it the same code as from the previous example. Let's try running it with the execfile command." ] }, { "cell_type": "code", "execution_count": 119, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter the temperature in Fahrenheit): 53\n", "The temperature in Celcius is 11.6666666667 degrees.\n" ] } ], "source": [ "execfile(\"temperature.py\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "With python as in other languages you can separate scripts and programs containing python code that can be used independently or with other python programs. The notebook is excellent for code development and presentations but not the best for production level code. " ] }, { "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", "execution_count": 120, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "ename": "IndentationError", "evalue": "unexpected indent (, line 2)", "output_type": "error", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m2\u001b[0m\n\u001b[0;31m print x\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mIndentationError\u001b[0m\u001b[0;31m:\u001b[0m unexpected indent\n" ] } ], "source": [ "x = 1\n", " print x" ] }, { "cell_type": "code", "execution_count": 121, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "yo\n", "ok\n" ] } ], "source": [ "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" ] }, { "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", "execution_count": 125, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { <<<<<<< HEAD "name": "stdout", "output_type": "stream", "text": [ "yo\n" ======= "ename": "TypeError", "evalue": "unsupported operand type(s) for -: 'str' and 'float'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mfaren\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mraw_input\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Enter the temperature in Fahrenheit): \"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mcel\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m5.\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0;36m9.\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mfaren\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0;36m32.\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 5\u001b[0m \u001b[0;32mprint\u001b[0m \u001b[0;34m\"The temperature in Celcius is \"\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mcel\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;34m\" degrees.\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for -: 'str' and 'float'" >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d ] } ], "source": [ <<<<<<< HEAD "# 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\"\n", " print \"dude\"" ======= "# 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.\"" >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d ] }, { "cell_type": "code", <<<<<<< HEAD "execution_count": 126, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "yo\n" ] } ], "source": [ "# You can put everything on one line\n", "\n", "print \"yo\" if x > 0 else \"dude\"" ======= "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# 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.\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Running a Program from Command Line\n", "\n", "This is a good of place as any to introduce the show that you can run you don't HAVE to use the notebook.\n", "\n", "Everyone should have a file called temperature.py, open the file in your text editor (emacs, vim, notepad) of choice at look at its content. \n", "You will see it the same code as from the previous example. Let's try running it with the execfile command." >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d ] }, { "cell_type": "code", <<<<<<< HEAD "execution_count": 130, ======= "execution_count": 15, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ <<<<<<< HEAD "yo\n" ======= "Enter the temperature in Fahrenheit): 15\n", "The temperature in Celcius is -9.44444444444 degrees.\n" >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d ] } ], "source": [ <<<<<<< HEAD "# Multiple cases\n", "\n", "x = -100\n", ======= "execfile(\"temperature.py\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "With python as in other languages you can separate scripts and programs containing python code that can be used independently or with other python programs. The notebook is excellent for code development and presentations but not the best for production level code. " ] }, { "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", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "x = 1\n", " print x" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "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" ] }, { "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", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "# 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\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# You can put everything on one line\n", "\n", "print \"yo\" if x > 0 else \"dude\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Multiple cases\n", "\n", "x = 1\n", >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "if x < -10:\n", " print \"yo\"\n", "elif x > 10: # 'elif' is short for 'else if'\n", " print \"dude\"\n", "else:\n", " print \"sup\"" ] }, { "cell_type": "code", <<<<<<< HEAD "execution_count": 136, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "25\n", "225\n", "625\n", "1225\n", "2025\n" ] } ], "source": [ "for x in range(5,50,10):\n", ======= "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "for x in range(5):\n", >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d " print x**2" ] }, { "cell_type": "code", <<<<<<< HEAD "execution_count": 140, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "all\n", "we\n", "wanna\n", "do\n", "is\n", "eat\n", "your\n", "brains\n" ] } ], ======= "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "source": [ "for x in (\"all\",\"we\",\"wanna\",\"do\",\"is\",\"eat\",\"your\",\"brains\"):\n", " print x" ] }, { "cell_type": "code", <<<<<<< HEAD "execution_count": 138, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "4\n", "8\n", "16\n" ] } ], ======= "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "source": [ "x = 0\n", "while x < 5:\n", " print pow(2,x)\n", " x += 1 # don't forget to increment x!" ] }, { "cell_type": "code", <<<<<<< HEAD "execution_count": 141, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 is odd.\n", "2 is even.\n", "3 is odd.\n", "4 is even.\n", "5 is odd.\n", "6 is even.\n", "7 is odd.\n", "8 is even.\n", "9 is odd.\n" ] } ], ======= "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "source": [ "# 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.\"" ] }, { "cell_type": "code", <<<<<<< HEAD "execution_count": 142, "metadata": { "collapsed": false }, "outputs": [ { "ename": "IndentationError", "evalue": "expected an indented block (, line 7)", "output_type": "error", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m7\u001b[0m\n\u001b[0;31m # Nothing here.\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mIndentationError\u001b[0m\u001b[0;31m:\u001b[0m expected an indented block\n" ] } ], ======= "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "source": [ "# 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." ] }, { "cell_type": "code", <<<<<<< HEAD "execution_count": 143, ======= "execution_count": null, >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "metadata": { "collapsed": false }, "outputs": [], "source": [ "# 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" ] }, { "cell_type": "code", <<<<<<< HEAD "execution_count": 144, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "4\n", "9\n", "16\n", "25\n", "36\n", "49\n", "64\n", "81\n", "100\n" ] } ], ======= "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d "source": [ "# 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" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "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", <<<<<<< HEAD "execution_count": 145, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Please enter a word in the sentence (enter . ! or ? to end.): J\n", "...currently: J \n", "Please enter a word in the sentence (enter . ! or ? to end.): Perkins\n", "...currently: J Perkins \n", "Please enter a word in the sentence (enter . ! or ? to end.): smells\n", "...currently: J Perkins smells \n", "Please enter a word in the sentence (enter . ! or ? to end.): funny\n", "...currently: J Perkins smells funny \n", "Please enter a word in the sentence (enter . ! or ? to end.): .\n", "--->J Perkins smells funny.\n" ] } ], "source": [ "sent = \"\"\n", "while True:\n", " newword = raw_input(\"Please enter a word in the sentence (enter . ! or ? to end.): \")\n", " if newword == \".\" or newword == \"?\" or newword == \"!\":\n", " if len(sent) > 0:\n", " # get rid of the nasty space we added in\n", " sent = sent[:-1]\n", " sent += newword\n", " break\n", " \n", " sent += newword + \" \"\n", " print \"...currently: \" + sent\n", "print \"--->\" + sent" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.10" } ======= "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.10" } >>>>>>> 27af03b15945bfdf9ec327f2f8f50b0f1c82e55d }, "nbformat": 4, "nbformat_minor": 0 }