{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Basic Training\n", "\n", "UC Berkeley Python Bootcamp\n", "
\n",
    "\n",
    "\n",
    "
\n", "follow along the code at: \n", "http://bit.ly/pyboot_01\n" ] }, { "cell_type": "code", "execution_count": 169, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%run talktools.py" ] }, { "cell_type": "code", "execution_count": 170, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 170, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from IPython.display import YouTubeVideo\n", "YouTubeVideo(\"imhrDrE4-mI\",width=\"640\",height=\"390\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Outline\n", "\n", " - Hello World!\n", " - calculator/basic math\n", " - strings\n", " - variables\n", " - basic control statements\n", " - indentation!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print(\"Hello, world.\")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Calculator #\n", "\n", "> there are `int` and `float` (but not doubles)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "scrolled": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "print(2 + 2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "2 + 2 " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "print(2.1 + 2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "2.1 + 2 == 4.0999999999999996" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "%run talktools" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ " - Python stores floats as their byte representation so is limited by the same 16-bit precision issues as most other languages\n", "\n", " - In doing calculations, unless you specify otherwise, Python will store the results in the smallest-byte representation" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "> 1. Indentation matters!\n", "> 2. When you mess up, Python is gentle\n", "> 3. \\# starts a comments (until the end of the line)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "print(2 + 2)\n", " 2 + 2" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "2 # this is a comment and is not printed" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "# this is also a comment" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "  " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "** Calculator **\n", "\n", " - all the math operators you'd expect, including `**` for power.\n", " - In Python 3, there is no distinction between `int` and `long`\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "42**42" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "(42**42).bit_length()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "bin(42**42)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Division always leads to a float" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "2 / 2" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "2 / 2.0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note: This is an important difference between Python 2 and Python 3. Old-style division between `int`s can be done with a double slash `//`" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "2 // 2" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "3 // 2" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "2.5 // 2 # egad, dont do this." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There is also `complex` types" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "complex(1,2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "1+2j" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "1 + 2j - 2j" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note: Access to [`decimal`](https://docs.python.org/3/library/decimal.html#module-decimal) (decimal fixed point and floating point arithmetic) and [`fraction`](https://docs.python.org/3/library/fractions.html#module-fractions) types/operations is through built-in `modules`." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "  " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Let's do some math" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "(3.0*10.0 - 25.0)/5.0" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "print(3.085e18*1e6) # this is a Megaparsec in units of cm!" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "t = 1.0 # declare a variable t (time)\n", "accel = 9.8 # acceleration in units of m/s^2" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "# distance travelled in time t seconds is 1/2 a*t**2\n", "dist = 0.5*accel*t*t\n", "print(dist) # this is the distance in meters" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "dist1 = accel*(t**2)/2\n", "print(dist1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "dist2 = 0.5*accel*pow(t,2)\n", "print(dist2)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ " - **variables** are assigned on the fly\n", " - multiplication, division, exponents as you expect" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "print(6 / 5) ; print(9 / 5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print(6 // 5) ; print(9 // 5) # remember double-slash integer division returns the floor" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "6 % 5 # mod operator, get the remainder. x = (x // y)*y + x % y" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "1 << 2 ## shift: move the number 1 by two bits to the left\n", " ## that is make a new number 100 (base 2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "5 >> 1 ## shift: move the number 5 = 101 (base 2) one to\n", " ## to the right (10 = 2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "x = 2 ; y = 3 ## assign two variables on the same line!\n", "x | y ## bitwise OR" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "x ^ y ## exclusive OR (10 ^ 11 = 01)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "x & y ## bitwise AND" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "x = x ^ y ; print(x)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "x += 3 ; print(x)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "x /= 2.0 ; print(x)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "we'll see a lot more mathy operators and functions later" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Relationships ##" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "# from before dist1 = 4.9 and dist = 4.9\n", "dist1 == dist" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "dist < 10" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "dist <= 4.9" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "dist < (10 + 2j)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "dist < -2.0" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "dist != 3.1415" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Try using some Greek characters as variables:\n", "\n", " \\Delta " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "  " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "** More on Variables & Types **" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "0 == False" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "not False" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "0.0 == False" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "not (10.0 - 10.0)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "not -1" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "not 3.1415" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "x = None # None is something special. Not true or false\n", "None == False" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "None == True" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "False or True" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "False and True" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "float(\"nan\") == True" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print(float('inf'))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import math\n", "math.isnan(float(\"NaN\"))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "  " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "** More on Variables & Types **" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "print(type(1))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "x = 2 ; type(x)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# bytes are immutable sequences of numbers from 0-255\n", "print((255).to_bytes(2,byteorder='big')) ; type(bytes(10))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "type(2) == type(1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "print(type(True))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "print(type(type(1)))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "print(type(pow))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "  " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "we can test whether something is a certain type with **`isinstance()`**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "isinstance(1,int)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "isinstance(1,(int,float))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "isinstance(\"spam\",str)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "isinstance(1.212,int)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "isinstance(1.212,int)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We'll see later than numbers are objects, which have functions (`methods`) that can act upon themselves:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "(1.212).is_integer()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "(1.0).is_integer()" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "builtin-types: **`int`**, **`bool`**, **`str`**, **`float`**, **`complex`**, **`bytes`**" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Strings" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Strings are a sequence of characters\n", "- they can be indexed and sliced up as if they were an array\n", "- you can glue strings together with + signs\n", "\n", "Strings are **immutable** (unlike in C), so you cannot change a string in place (this isn't so bad...)\n", "\n", "Strings can be formatted and compared " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "x = \"spam\" ; print(type(x))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "print(\"hello!\\n...my sire.\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "\"hello!\\n...my sire.\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "\"wah?!\" == 'wah?!'" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "print(\"'wah?!' said the student\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "print(\"\\\"wah?!\\\" said the student\")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "backslashes (\\\\) start special (escape) characters:\n", "```\n", " \\n = newline (\\r = return)\n", " \\t = tab\n", " \\a = bell\n", "```\n", "string literals are defined with double quotes or quotes.\n", "The outermost quote type cannot be used inside the 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": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print(\"\\a\\a\\a\") # try this in CLI python, not the notebook" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "# raw strings don't escape characters\n", "print(r'This is a raw string...newlines \\r\\n are ignored.')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "# Triple quotes are real useful for multiple line strings\n", "y = '''For 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": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "\n", " - prepending ``r`` makes that string \"raw\"\n", "\n", " - triple quotes allow you to compose long strings\n", " \n", " https://docs.python.org/3.4/reference/lexical_analysis.html#literals" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "print(\"\\N{RIGHT CURLY BRACKET}\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "print(\"\\N{BLACK HEART SUIT}\")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "http://www.fileformat.info/info/unicode/char/search.htm" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "s = \"spam\" ; e = \"eggs\"\n", "print(s + e)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print(\"spam\"\n", " \"eggs\"\n", " \"Trumpkins\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print(s\n", " \"eggs\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "print(s + \" and \" + e)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print(s,\"and\",e, sep=\" \")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "print(\"green \" + e + \" and\\n \" + s + \"\\n\\t ... and Trumpkins\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "print(s*3 + e)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print(s*3,e,sep=\"->\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "print(\"*\"*50)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "print(\"spam\" == \"good\") ; print(\"spam\" == \"spam\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "\"spam\" < \"zoo\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "\"s\" < \"spam\"" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ " - you can concatenate strings with ``+`` sign\n", " - you can do multiple concatenations with the ``*`` sign\n", " - strings can be compared" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "print('I want' + 3 + ' eggs and no ' + s)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "print('I want ' + str(3) + ' eggs and no ' + s) " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "pi = 3.14159\n", "print('I want ' + str(pi) + ' eggs and no ' + s)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "print(str(True) + \":\" + ' I want ' + str(pi) + ' eggs and no ' + s)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "you must concatenate only strings, coercing (\"casting\") \n", "other variable types to `str`" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "notes" } }, "source": [ "there's a cleaner way to do this, with string formatting. we'll see that tomorrow." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Getting input from the user: always a string response" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "faren = input(\"Enter the temperature (in Fahrenheit): \")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "cent = (5.0/9.0)*(faren - 32.0)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "faren = float(faren)\n", "cent = (5.0/9.0)*(faren - 32.0) ; print(cent)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "faren = float(input(\"Enter the temperature (in Fahrenheit): \"))\n", "print((5.0/9.0)*(faren - 32.0))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "  " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### We can think of strings as arrays (although, unlike in C you never really need to deal with directly addressing character locations in memory)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "s =\"spam\"\n", "len(s)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "len(\"eggs\\n\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "len(\"\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "s[0]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "s[-1]" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ " - ``len()`` gives us the length of an array\n", " - strings are zero indexed\n", " - can also count backwards" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "We can think of strings as arrays\n", "(although, unlike in C you never really need to deal with directly addressing character locations in memory)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "useful for slicing: indices are between the characters" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "s[0:1] # get every character between 0 and 1" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "s[1:4] # get every character between 1 and 4 " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "s[-2:-1] " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "## slicing [m:n] will return abs(n-m) characters\n", "s[0:100] # if the index is beyond the len(str), you dont segfault!" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "s[100]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "s[1:] # python runs the index to the end" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "s[:2] # python runs the index to the beginning" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "s[::-1] # print it out backwards" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ " s = s[:n] + s[n:] for all n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Basic Control (Flow)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python has pretty much all of what you use:\n", "\n", " if...elif...else, for, while\n", "\n", "As well as:\n", "\n", " break, continue (within loops)\n", " \n", "Does not have:\n", "\n", " case (explicitly), goto\n", "\n", "Does have: `pass`" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Flow is done within blocks (where indentation matters)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "x = 1\n", "if x > 0:\n", " print(\"yo\")\n", "else:\n", " print(\"dude\")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Note: if you are doing this within the Python interpreter you'll see the ...\n", "```\n", ">>> x = 1\n", ">>> if x > 0:\n", "... print \"yo\"\n", "... else:\n", "... print \"dude\"\n", "... \n", "yo\n", "```" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Note colons & indentations (tabbed or spaced)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "x = 1\n", "if x > 0:\n", " print(\"yo\")\n", "else:\n", " print(\"dude\")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Indentations with the same block must be the same but not within different blocks (though this is ugly)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "one-liners" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "print(\"yo\" if x > 0 else \"dude\")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "a small program... Do Control-C to stop (in Python/IPython) or \"Kernel->Interrupt\" in IPython notebook" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "x = 1\n", "y = 0\n", "while True:\n", " print(\"yo\" if x > 0 else \"dude\")\n", " x *= -1\n", " y += 1\n", " if y > 42:\n", " break" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "case statements can be constructed with \n", "just a bunch of if, elif,...else" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "if x < 1:\n", " print(\"t\")\n", "elif x > 100:\n", " print(\"yo\")\n", "else:\n", " print(\"dude\")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "ordering matters. The first block of `True` in an if/elif gets executed then everything else does not." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "blocks cannot be empty" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "x = \"fried goldfish\"\n", "if x == \"spam for dinner\":\n", " print(\"I will destroy the universe\")\n", "else:\n", " # I'm fine with that. I'll do nothing" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "`pass` is a \"do nothing\" statement" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "if x == \"spam for dinner\":\n", " print(\"I will destroy the universe\")\n", "else:\n", " # I'm fine with that. I'll do nothing\n", " pass" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The double percent sign at the top of an IPython/Jupyter cell is a cell-level \"magic\". It's not Python itself, but defined as part of IPython/Jupyter. We'll see more on this later in the bootcamp." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "%%file temp1.py\n", "# set some initial variables. Set the initial temperature low \n", "faren = -1000\n", "\n", "# we dont want this going on forever, let's make sure we cannot have too many attempts \n", "max_attempts = 6\n", "attempt = 0\n", "\n", "while faren < 100:\n", " # let's get the user to tell us what temperature it is \n", " newfaren = float(input(\"Enter the temperature (in Fahrenheit): \"))\n", " if newfaren > faren:\n", " print(\"It's getting hotter\")\n", " elif newfaren < faren:\n", " print(\"It's getting cooler\")\n", " else:\n", " # nothing has changed, just continue in the loop \n", " continue\n", " faren = newfaren # now set the current temp to the new temp just entered \n", " attempt += 1 # bump up the attempt number \n", " if attempt >= max_attempts:\n", " # we have to bail out \n", " break\n", "if attempt >= max_attempts:\n", " # we bailed out because of too many attempts \n", " print(\"Too many attempts at raising the temperature.\")\n", "else:\n", " # we got here because it's hot \n", " print(\"it's hot here, people.\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "%run temp1" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "%run temp1" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "%%file temp2.py\n", "\n", "# set some initial variables. Set the initial temperature low \n", "faren = -1000\n", "\n", "# we dont want this going on forever, let's make sure we cannot have too many attempts \n", "max_attempts = 6\n", "attempt = 0\n", "\n", "while faren < 100 and (attempt < max_attempts):\n", " # let's get the user to tell us what temperature it is \n", " newfaren = float(input(\"Enter the temperature (in Fahrenheit): \"))\n", " if newfaren > faren:\n", " print(\"It's getting hotter\")\n", " elif newfaren < faren:\n", " print(\"It's getting cooler\")\n", " else:\n", " # nothing has changed, just continue in the loop \n", " continue\n", " faren = newfaren # now set the current temp to the new temp just entered \n", " attempt += 1 # bump up the attempt number \n", "\n", "if attempt >= max_attempts:\n", " # we bailed out because of too many attempts \n", " print(\"Too many attempts at raising the temperature.\")\n", "else:\n", " # we got here because it's hot \n", " print(\"it's hot here, people.\")" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false, "slideshow": { "slide_type": "notes" } }, "source": [ "UC Berkeley Python Bootcamp - Basic Training\n", "(c) J. Bloom 2008-2016 All Rights Reserved" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "py3k" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.2" } }, "nbformat": 4, "nbformat_minor": 0 }