# Execute this cell to define this variable # Either click Cell => Run from the menu or type # ctrl-Enter to execute. See the Help menu for lots # of useful tips. Help => IPython Help and # Help => Keyboard Shortcuts are especially # useful. message = "I want to mine the social web!" # The variable 'message is defined here. Execute this cell to see for yourself print message # The variable 'message' is defined here, but we'll delete it # after displaying it to illustrate an important point... print message del message # The variable message is no longer defined in this cell or two cells # above anymore. Try executing this cell or that cell to see for yourself. print message # Try typing in some code of your own! an_integer = 23 print an_integer, type(an_integer) print a_float = 23.0 print a_float, type(a_float) print a_string = "string" print a_string, type(a_string) print a_list = [1,2,3] print a_list, type(a_list) print a_list[0] # access the first item print a_dict = {'a' : 1, 'b' : 2, 'c' : 3} print a_dict, type(a_dict) print a_dict['a'] # access the item with key 'a' contacts = [ { 'name' : 'Bob', 'age' : 23, 'married' : False, 'height' : 1.8, # meters 'languages' : ['English', 'Spanish'], 'address' : '123 Maple St.', 'phone' : '(555) 555-5555' }, {'name' : 'Sally', 'age' : 26, 'married' : True, 'height' : 1.5, # meters 'languages' : ['English'], 'address' : '456 Elm St.', 'phone' : '(555) 555-1234' } ] for contact in contacts: print "Name:", contact['name'] print "Married:", contact['married'] print import json print contacts print type(contacts) # list # json.dumps pronounced (dumps stands for "dump string") takes a Python data structure # that is serializable to JSON and dumps it as a string jsonified_contacts = json.dumps(contacts, indent=2) # indent is used for pretty-printing print type(jsonified_contacts) # str print jsonified_contacts a_tuple = (1,2,3) an_int = (1) # You must include a trailing comma when only one item is in the tuple a_tuple = (1,) a_tuple = (1,2,3,) # Trailing commas are ok in tuples and lists none = None print none == None # True print none == True # False print none == False # False print # In general, you'll see the special 'is' operator used when comparing a value to # None, but most of the time, it works the same as '==' print none is None # True print none is True # False print none is False # False def square(x): return x*x print square(2) # 4 print # The default value for L is only created once and shared amongst # calls def f1(a, L=[]): L.append(a) return L print f1(1) # [1] print f1(2) # [1, 2] print f1(3) # [1, 2, 3] print # Each call creates a new value for L def f2(a, L=None): if L is None: L = [] L.append(a) return L print f2(1) # [1] print f2(2) # [2] print f2(3) # [3] a_list = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] print a_list[0] # a print a_list[0:2] # ['a', 'b'] print a_list[:2] # Same as above. The starting index is implicitly 0 print a_list[3:] # ['d', 'e', 'f', 'g'] Ending index is implicitly the length of the list print a_list[-1] # g Negative indices start at the end of the list print a_list[-3:-1] # ['e', 'f'] Start at the end and work backwards. (The index after the colon is still excluded) print a_list[-3:] # ['e', 'f', 'g'] The last three items in the list print a_list[:-4] # ['a', 'b', 'c'] # Everything up to the last 4 items a_string = 'abcdefg' # String slicing works the very same way print a_string[:-4] # abc # One way to create a list containing 0..9: a_list = [] for i in range(10): a_list.append(i) print a_list # How to do it with a list comprehension print [ i for i in range(10) ] # But what about a nested loop like this one, which # even contains a conditional expression in it: a_list = [] for i in range(10): for j in range(10, 20): if i % 2 == 0: a_list.append(i) print a_list # You can achieve a nested list comprehension to # achieve the very same result. When written with readable # indention like below, note the striking similarity to # the equivalent code as presented above. print [ i for i in range(10) for j in range(10, 20) if i % 2 == 0 ] # Literal syntax a_dict = { 'a' : 1, 'b' : 2, 'c' : 3 } print a_dict print # Using the dict constructor a_dict = dict([('a', 1), ('b', 2), ('c', 3)]) print a_dict print # Dictionary comprehension syntax a_dict = { k : v for (k,v) in [('a', 1), ('b', 2), ('c', 3)] } print a_dict print # A more appropriate circumstance to use dictionary comprehension would # involve more complex computation a_dict = { k : k*k for k in xrange(10) } # {0: 0, 1: 1, 2: 4, 3: 9, ..., 9: 81} print a_dict lst = ['a', 'b', 'c'] # You could opt to maintain a looping index... i = 0 for item in lst: print i, item i += 1 # ...but the enumerate function spares you the trouble of maintaining a loop index for i, item in enumerate(lst): print i, item def f(a, b, c, d=None, e=None): print a, b, c, d, e f(1, 2, 3) # 1 2 3 None None f(1, 3, 3, d=4) # 1 2 3 4 None f(1, 2, 3, d=4, e=5) # 1 2 3 4 5 args = [1,2,3] kwargs = {'d' : 4, 'e' : 5} f(*args, **kwargs) # 1 2 3 4 5 name1, name2 = "Bob", "Sally" print "Hello, " + name1 + ". My name is " + name2 print "Hello, %s. My name is %s" % (name1, name2,) print "Hello, {0}. My name is {1}".format(name1, name2) print "Hello, {0}. My name is {1}".format(*[name1, name2]) names = [name1, name2] print "Hello, {0}. My name is {1}".format(*names) print "Hello, {you}. My name is {me}".format(you=name1, me=name2) print "Hello, {you}. My name is {me}".format(**{'you' : name1, 'me' : name2}) names = {'you' : name1, 'me' : name2} print "Hello, {you}. My name is {me}".format(**names) import envoy # pip install envoy # Run a command just as you would in a terminal on the virtual machine r = envoy.run('ps aux | grep ipython') # show processes containing 'ipython' # Print its standard output print r.std_out # Print its standard error print r.std_err # Print the working directory for the IPython Notebook server print envoy.run('pwd').std_out # Try some commands of your own... %%bash # Print the working directory pwd # Display the date date # View the first 10 lines of a manual page for wget man wget | head -10 # Download a webpage to /tmp/index.html wget -O /tmp/foo.html http://ipython.org/notebook.html # Search for 'ipython' in the webpage grep ipython /tmp/foo.html %%bash ls ../ # Displays the status of the local repository git status # Execute "git pull" to perform an update from IPython.display import IFrame from IPython.core.display import display # IPython Notebook can serve files relative to the location of # the working notebook into inline frames. Prepend the path # with the 'files' prefix static_content = 'files/resources/appc-pythontips/hello.txt' display(IFrame(static_content, '100%', '600px')) import os # The absolute path to the shared folder on the VM shared_folder="/vagrant" # List the files in the shared folder print os.listdir(shared_folder) print # How to read and display a snippet of the share/README.md file... README = os.path.join(shared_folder, "README.md") txt = open(README).read() print txt[:200] # Write out a file to the guest but notice that it is available on the host # by checking the contents of your GitHub checkout f = open(os.path.join(shared_folder, "Hello.txt"), "w") f.write("Hello. This text is written on the guest but synchronized to the host by Vagrant") f.close() %%bash tail -n 100 /var/log/kern.log %%bash ps -e -orss=,args= | sort -b -k1,1n | pr -TW$COLUMNS | tail -n 10