Created
January 8, 2017 14:49
-
-
Save SlinkoIgor/c3388fea00ccd43ac50b852e240e0dd0 to your computer and use it in GitHub Desktop.
Revisions
-
Igor Slinko created this gist
Jan 8, 2017 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,1246 @@ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Basics of Python" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python is a high-level, dynamically typed multiparadigm programming language. Python code is often said to be almost like pseudocode, since it allows you to express very powerful ideas in very few lines of code while being very readable. As an example, here is an implementation of the classic quicksort algorithm in Python:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 1, 2, 3, 6, 8, 10]\n" ] } ], "source": [ "def quicksort(arr):\n", " if len(arr) <= 1:\n", " return arr\n", " pivot = arr[len(arr) / 2]\n", " left = [x for x in arr if x < pivot]\n", " middle = [x for x in arr if x == pivot]\n", " right = [x for x in arr if x > pivot]\n", " return quicksort(left) + middle + quicksort(right)\n", "\n", "print quicksort([3,6,8,10,1,2,1])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Python versions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are currently two different supported versions of Python, 2.7 and 3.4. Somewhat confusingly, Python 3.0 introduced many backwards-incompatible changes to the language, so code written for 2.7 may not work under 3.4 and vice versa. For this class all code will use Python 2.7.\n", "\n", "You can check your Python version at the command line by running `python --version`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Basic data types" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Numbers" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Integers and floats work as you would expect from other languages:" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3 <type 'int'>\n" ] } ], "source": [ "x = 3\n", "print x, type(x)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n", "2\n", "6\n", "9\n" ] } ], "source": [ "print x + 1 # Addition;\n", "print x - 1 # Subtraction;\n", "print x * 2 # Multiplication;\n", "print x ** 2 # Exponentiation;" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n", "8\n" ] } ], "source": [ "x += 1\n", "print x # Prints \"4\"\n", "x *= 2\n", "print x # Prints \"8\"" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "<type 'float'>\n", "2.5 3.5 5.0 6.25\n" ] } ], "source": [ "y = 2.5\n", "print type(y) # Prints \"<type 'float'>\"\n", "print y, y + 1, y * 2, y ** 2 # Prints \"2.5 3.5 5.0 6.25\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that unlike many languages, Python does not have unary increment (x++) or decrement (x--) operators.\n", "\n", "Python also has built-in types for long integers and complex numbers; you can find all of the details in the [documentation](https://docs.python.org/2/library/stdtypes.html#numeric-types-int-float-long-complex)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Booleans" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python implements all of the usual operators for Boolean logic, but uses English words rather than symbols (`&&`, `||`, etc.):" ] }, { "cell_type": "code", "execution_count": 139, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "<type 'bool'>\n" ] } ], "source": [ "t, f = True, False\n", "print type(t) # Prints \"<type 'bool'>\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we let's look at the operations:" ] }, { "cell_type": "code", "execution_count": 140, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n", "True\n", "False\n", "True\n" ] } ], "source": [ "print t and f # Logical AND;\n", "print t or f # Logical OR;\n", "print not t # Logical NOT;\n", "print t != f # Logical XOR;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Strings" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello 5\n" ] } ], "source": [ "hello = 'hello' # String literals can use single quotes\n", "world = \"world\" # or double quotes; it does not matter.\n", "print hello, len(hello)" ] }, { "cell_type": "code", "execution_count": 143, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello world\n" ] } ], "source": [ "hw = hello + ' ' + world # String concatenation\n", "print hw # prints \"hello world\"" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello world 12\n" ] } ], "source": [ "hw12 = '{} {} {}'.format(hello, world, 12) # sprintf style string formatting\n", "print hw12 # prints \"hello world 12\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "String objects have a bunch of useful methods; for example:" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello\n", "HELLO\n", " hello\n", " hello \n", "he(ell)(ell)o\n", "world\n" ] } ], "source": [ "s = \"hello\"\n", "print s.capitalize() # Capitalize a string; prints \"Hello\"\n", "print s.upper() # Convert a string to uppercase; prints \"HELLO\"\n", "print s.replace('l', '(ell)') # Replace all instances of one substring with another;\n", " # prints \"he(ell)(ell)o\"\n", "print ' world '.strip() # Strip leading and trailing whitespace; prints \"world\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can find a list of all string methods in the [documentation](https://docs.python.org/2/library/stdtypes.html#string-methods)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Containers" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python includes several built-in container types: lists, dictionaries, sets, and tuples." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Lists" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A list is the Python equivalent of an array, but is resizeable and can contain elements of different types:" ] }, { "cell_type": "code", "execution_count": 146, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[3, 1, 2] 2\n", "2\n" ] } ], "source": [ "xs = [3, 1, 2] # Create a list\n", "print xs, xs[2]\n", "print xs[-1] # Negative indices count from the end of the list; prints \"2\"" ] }, { "cell_type": "code", "execution_count": 148, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[3, 1, 'foo']\n" ] } ], "source": [ "xs[2] = 'foo' # Lists can contain elements of different types\n", "print xs" ] }, { "cell_type": "code", "execution_count": 150, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[3, 1, 'foo', 'bar', 'bar']\n" ] } ], "source": [ "xs.append('bar') # Add a new element to the end of the list\n", "print xs " ] }, { "cell_type": "code", "execution_count": 152, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "bar [3, 1, 'foo']\n" ] } ], "source": [ "x = xs.pop() # Remove and return the last element of the list\n", "print x, xs " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As usual, you can find all the gory details about lists in the [documentation](https://docs.python.org/2/tutorial/datastructures.html#more-on-lists)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Slicing" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In addition to accessing list elements one at a time, Python provides concise syntax to access sublists; this is known as slicing:" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0, 1, 2, 3, 4]\n", "[2, 3]\n", "[2, 3, 4]\n", "[0, 1]\n", "[0, 1, 2, 3, 4]\n", "[0, 1, 2, 3]\n", "[0, 1, 8, 9, 4]\n" ] } ], "source": [ "nums = range(5) # range is a built-in function that creates a list of integers\n", "print nums # Prints \"[0, 1, 2, 3, 4]\"\n", "print nums[2:4] # Get a slice from index 2 to 4 (exclusive); prints \"[2, 3]\"\n", "print nums[2:] # Get a slice from index 2 to the end; prints \"[2, 3, 4]\"\n", "print nums[:2] # Get a slice from the start to index 2 (exclusive); prints \"[0, 1]\"\n", "print nums[:] # Get a slice of the whole list; prints [\"0, 1, 2, 3, 4]\"\n", "print nums[:-1] # Slice indices can be negative; prints [\"0, 1, 2, 3]\"\n", "nums[2:4] = [8, 9] # Assign a new sublist to a slice\n", "print nums # Prints \"[0, 1, 8, 8, 4]\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Loops" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can loop over the elements of a list like this:" ] }, { "cell_type": "code", "execution_count": 153, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "cat\n", "dog\n", "monkey\n" ] } ], "source": [ "animals = ['cat', 'dog', 'monkey']\n", "for animal in animals:\n", " print animal" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you want access to the index of each element within the body of a loop, use the built-in `enumerate` function:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "#1: cat\n", "#2: dog\n", "#3: monkey\n" ] } ], "source": [ "animals = ['cat', 'dog', 'monkey']\n", "for idx, animal in enumerate(animals):\n", " print '#{}: {}'.format(idx + 1, animal)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### List comprehensions:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When programming, frequently we want to transform one type of data into another. As a simple example, consider the following code that computes square numbers:" ] }, { "cell_type": "code", "execution_count": 155, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0, 1, 4, 9, 16]\n" ] } ], "source": [ "nums = [0, 1, 2, 3, 4]\n", "squares = []\n", "for x in nums:\n", " squares.append(x ** 2)\n", "print squares" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can make this code simpler using a list comprehension:" ] }, { "cell_type": "code", "execution_count": 156, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0, 1, 4, 9, 16]\n" ] } ], "source": [ "nums = [0, 1, 2, 3, 4]\n", "squares = [x ** 2 for x in nums]\n", "print squares" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "List comprehensions can also contain conditions:" ] }, { "cell_type": "code", "execution_count": 157, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0, 4, 16]\n" ] } ], "source": [ "nums = [0, 1, 2, 3, 4]\n", "even_squares = [x ** 2 for x in nums if x % 2 == 0]\n", "print even_squares" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Dictionaries" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A dictionary stores (key, value) pairs, similar to a `Map` in Java or an object in Javascript. You can use it like this:" ] }, { "cell_type": "code", "execution_count": 158, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "cute\n", "True\n" ] } ], "source": [ "d = {'cat': 'cute', 'dog': 'furry'} # Create a new dictionary with some data\n", "print d['cat'] # Get an entry from a dictionary; prints \"cute\"\n", "print 'cat' in d # Check if a dictionary has a given key; prints \"True\"" ] }, { "cell_type": "code", "execution_count": 159, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "wet\n" ] } ], "source": [ "d['fish'] = 'wet' # Set an entry in a dictionary\n", "print d['fish'] # Prints \"wet\"" ] }, { "cell_type": "code", "execution_count": 161, "metadata": { "collapsed": false }, "outputs": [ { "ename": "KeyError", "evalue": "'monkey'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m<ipython-input-161-85f0c5ba0fa7>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mprint\u001b[0m \u001b[0md\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'monkey'\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;31m# KeyError: 'monkey' not a key of d\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mKeyError\u001b[0m: 'monkey'" ] } ], "source": [ "print d['monkey'] # KeyError: 'monkey' not a key of d" ] }, { "cell_type": "code", "execution_count": 162, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "N/A\n", "wet\n" ] } ], "source": [ "print d.get('monkey', 'N/A') # Get an element with a default; prints \"N/A\"\n", "print d.get('fish', 'N/A') # Get an element with a default; prints \"wet\"" ] }, { "cell_type": "code", "execution_count": 163, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "N/A\n" ] } ], "source": [ "del d['fish'] # Remove an element from a dictionary\n", "print d.get('fish', 'N/A') # \"fish\" is no longer a key; prints \"N/A\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can find all you need to know about dictionaries in the [documentation](https://docs.python.org/2/library/stdtypes.html#dict)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It is easy to iterate over the keys in a dictionary:" ] }, { "cell_type": "code", "execution_count": 164, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A person has 2 legs\n", "A spider has 8 legs\n", "A cat has 4 legs\n" ] } ], "source": [ "d = {'person': 2, 'cat': 4, 'spider': 8}\n", "for animal in d:\n", " legs = d[animal]\n", " print 'A %s has %d legs' % (animal, legs)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you want access to keys and their corresponding values, use the iteritems method:" ] }, { "cell_type": "code", "execution_count": 165, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A person has 2 legs\n", "A spider has 8 legs\n", "A cat has 4 legs\n" ] } ], "source": [ "d = {'person': 2, 'cat': 4, 'spider': 8}\n", "for animal, legs in d.iteritems():\n", " print 'A %s has %d legs' % (animal, legs)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Dictionary comprehensions: These are similar to list comprehensions, but allow you to easily construct dictionaries. For example:" ] }, { "cell_type": "code", "execution_count": 166, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{0: 0, 2: 4, 4: 16}\n" ] } ], "source": [ "nums = [0, 1, 2, 3, 4]\n", "even_num_to_square = {x: x ** 2 for x in nums if x % 2 == 0}\n", "print even_num_to_square" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Sets" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A set is an unordered collection of distinct elements. As a simple example, consider the following:" ] }, { "cell_type": "code", "execution_count": 167, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n", "False\n" ] } ], "source": [ "animals = {'cat', 'dog'}\n", "print 'cat' in animals # Check if an element is in a set; prints \"True\"\n", "print 'fish' in animals # prints \"False\"\n" ] }, { "cell_type": "code", "execution_count": 170, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n", "2\n" ] } ], "source": [ "animals.add('fish') # Add an element to a set\n", "print 'fish' in animals\n", "print len(animals) # Number of elements in a set;" ] }, { "cell_type": "code", "execution_count": 171, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3\n", "2\n" ] } ], "source": [ "animals.add('cat') # Adding an element that is already in the set does nothing\n", "print len(animals) \n", "animals.remove('cat') # Remove an element from a set\n", "print len(animals) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "_Loops_: Iterating over a set has the same syntax as iterating over a list; however since sets are unordered, you cannot make assumptions about the order in which you visit the elements of the set:" ] }, { "cell_type": "code", "execution_count": 63, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "#1: fish\n", "#2: dog\n", "#3: cat\n" ] } ], "source": [ "animals = {'cat', 'dog', 'fish'}\n", "for idx, animal in enumerate(animals):\n", " print '#%d: %s' % (idx + 1, animal)\n", "# Prints \"#1: fish\", \"#2: dog\", \"#3: cat\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Set comprehensions: Like lists and dictionaries, we can easily construct sets using set comprehensions:" ] }, { "cell_type": "code", "execution_count": 172, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "set([0, 1, 2, 3, 4, 5])\n" ] } ], "source": [ "from math import sqrt\n", "print {int(sqrt(x)) for x in range(30)}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Tuples" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A tuple is an (immutable) ordered list of values. A tuple is in many ways similar to a list; one of the most important differences is that tuples can be used as keys in dictionaries and as elements of sets, while lists cannot. Here is a trivial example:" ] }, { "cell_type": "code", "execution_count": 173, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "<type 'tuple'>\n", "5\n", "1\n" ] } ], "source": [ "d = {(x, x + 1): x for x in range(10)} # Create a dictionary with tuple keys\n", "t = (5, 6) # Create a tuple\n", "print type(t)\n", "print d[t] \n", "print d[(1, 2)]" ] }, { "cell_type": "code", "execution_count": 176, "metadata": { "collapsed": false }, "outputs": [ { "ename": "TypeError", "evalue": "'tuple' object does not support item assignment", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m<ipython-input-176-0a69537257d5>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mt\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" ] } ], "source": [ "t[0] = 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python functions are defined using the `def` keyword. For example:" ] }, { "cell_type": "code", "execution_count": 178, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "negative\n", "zero\n", "positive\n" ] } ], "source": [ "def sign(x):\n", " if x > 0:\n", " return 'positive'\n", " elif x < 0:\n", " return 'negative'\n", " else:\n", " return 'zero'\n", "\n", "for x in [-1, 0, 1]:\n", " print sign(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We will often define functions to take optional keyword arguments, like this:" ] }, { "cell_type": "code", "execution_count": 179, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, Bob!\n", "HELLO, FRED\n" ] } ], "source": [ "def hello(name, loud=False):\n", " if loud:\n", " print 'HELLO, %s' % name.upper()\n", " else:\n", " print 'Hello, %s!' % name\n", "\n", "hello('Bob')\n", "hello('Fred', loud=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Classes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The syntax for defining classes in Python is straightforward:" ] }, { "cell_type": "code", "execution_count": 76, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, Fred\n", "HELLO, FRED!\n" ] } ], "source": [ "class Greeter:\n", "\n", " # Constructor\n", " def __init__(self, name):\n", " self.name = name # Create an instance variable\n", "\n", " # Instance method\n", " def greet(self, loud=False):\n", " if loud:\n", " print 'HELLO, %s!' % self.name.upper()\n", " else:\n", " print 'Hello, %s' % self.name\n", "\n", "g = Greeter('Fred') # Construct an instance of the Greeter class\n", "g.greet() # Call an instance method; prints \"Hello, Fred\"\n", "g.greet(loud=True) # Call an instance method; prints \"HELLO, FRED!\"" ] } ], "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.3" } }, "nbformat": 4, "nbformat_minor": 0 }