|
| 1 | +''' |
| 2 | +Created on Oct 28, 2014 |
| 3 | +
|
| 4 | +@author: Aleksey |
| 5 | +''' |
| 6 | + |
| 7 | +# Review |
| 8 | +d = {} |
| 9 | +d[1] = "that" |
| 10 | +d[2] = "this" |
| 11 | +print d |
| 12 | + |
| 13 | +for key in sorted(d.keys()): |
| 14 | + print key, d[key] |
| 15 | +print |
| 16 | + |
| 17 | +# Advanced arguments passing |
| 18 | +def f(x, y, w=0, h=0): |
| 19 | + print "position: %s, %s --shape: %s, %s" %(x, y, w, h) |
| 20 | + |
| 21 | +f (3, 4, h=7) |
| 22 | + |
| 23 | +shape = {'w':40, 'h':60} |
| 24 | + |
| 25 | +# positional arguments followed by the dictionary |
| 26 | +f(3, 4, **shape) |
| 27 | + |
| 28 | +def f1(*args, **kwargs): |
| 29 | + print "the positional arguments are:", args |
| 30 | + print "the keyword arguments are:", kwargs |
| 31 | + |
| 32 | +# Returns empty tulpe for *args and empty dictionary for *kwargs |
| 33 | +f1() |
| 34 | + |
| 35 | +formatter = "My name is {first} {last}" |
| 36 | + |
| 37 | +person = {'first':'Chris', 'last':'Baker'} |
| 38 | +print formatter.format(2, 1, **person) |
| 39 | + |
| 40 | +print |
| 41 | + |
| 42 | +g = {} |
| 43 | +g["fore_color"]="white" |
| 44 | +g["back_color"]="green" |
| 45 | +g["link_color"]="blue" |
| 46 | +g["visited_color"]="maroon" |
| 47 | + |
| 48 | +def colors1(**kwargs): |
| 49 | + print "the colors are %s, %s, %s, %s" %(tuple(kwargs.values())) |
| 50 | + |
| 51 | +colors1(**g) |
| 52 | + |
| 53 | +print |
| 54 | + |
| 55 | +# Print unknowun number of arguments |
| 56 | +def print_everything(*args): |
| 57 | + for count, thing in enumerate(args): |
| 58 | + print '{0}. {1}'.format(count, thing) |
| 59 | +print_everything('apple', 'banana', 'cabbage') |
| 60 | + |
| 61 | +print |
| 62 | + |
| 63 | +# print unknown number of items |
| 64 | +def table_things(**kwargs): |
| 65 | + for name, value in kwargs.items(): |
| 66 | + print '{0} = {1}'.format(name, value) |
| 67 | +table_things(apple='fruit', cabbage='vegetable') |
| 68 | + |
| 69 | +# Print whole dictionary |
| 70 | +table_things(**g) |
| 71 | + |
| 72 | +print |
| 73 | + |
| 74 | +def my_d(**kwargs): |
| 75 | + for name, value in kwargs.items(): |
| 76 | + print "The {0} is {1}".format(name, value) |
| 77 | +my_d(**g) |
| 78 | + |
| 79 | +print |
| 80 | + |
| 81 | +def print_three_things(a, b, c): |
| 82 | + print 'a = {0}, b = {1}, c = {2}'.format(a,b,c) |
| 83 | +mylist = ['aardvark', 'baboon', 'cat'] |
| 84 | +print_three_things(*mylist) |
| 85 | + |
| 86 | +lst = [i for i in range(5)] |
| 87 | +print lst |
| 88 | + |
| 89 | +lst1 = [i * 4 for i in range(5)] |
| 90 | +print lst1 |
| 91 | + |
| 92 | +lst2 = ['first', 'that', 'the', 'other'] |
| 93 | +lst3 = [s.upper() for s in lst2] |
| 94 | +print lst3 |
| 95 | + |
| 96 | +lst4 = [ (i, j) for i in range(3) for j in range(4,6)] |
| 97 | +print lst4 |
| 98 | + |
| 99 | +lst5 = [s.upper() for s in lst2 if s.startswith('t')] |
| 100 | +print lst5 |
| 101 | + |
| 102 | +lst6 = [name for name in dir(__builtins__) if "Error" in name] |
| 103 | +for l in lst6: |
| 104 | + print l |
| 105 | + |
| 106 | +print |
| 107 | + |
| 108 | +# List Comprehension |
| 109 | +feast = ['lambs', "sloths", "orangutans", "breakfast cereals", "fruit bats"] |
| 110 | +comprehansion = [delicacy.capitalize() for delicacy in feast] |
| 111 | +print comprehansion[0] |
| 112 | +print comprehansion[2] |
| 113 | +print |
| 114 | +comprehension = [delicacy for delicacy in feast if len(delicacy) > 6] |
| 115 | +print comprehension |
| 116 | +print len(feast) |
| 117 | +print len(comprehension) |
| 118 | +print |
| 119 | +list_of_tuples = [(1, 'lumberjack'), (2, 'inquisition'), (4, 'spam')] |
| 120 | +comprehension = [ skit * number for number, skit in list_of_tuples ] |
| 121 | +print comprehension[0] |
| 122 | +print comprehension[2] |
| 123 | +print |
| 124 | +list_of_eggs = ['poached egg', 'fried egg'] |
| 125 | +list_of_meats = ['lite spam', 'ham spam', 'fried spam'] |
| 126 | +comprehension = [ '{0} and {1}'.format(egg, meat) for egg in list_of_eggs for meat in list_of_meats] |
| 127 | +print len(comprehension) |
| 128 | +for i in comprehension: |
| 129 | + print i |
| 130 | +print |
| 131 | +comprehension = { x for x in 'aabbbcccc'} |
| 132 | +print comprehension |
| 133 | +print |
| 134 | +dict_of_weapons = {'first': 'fear', 'second': 'surprise', 'third':'ruthless efficiency', 'forth':'fanatical devotion', 'fifth': None} |
| 135 | +dict_comprehension = { k.upper(): weapon for k, weapon in dict_of_weapons.iteritems() if weapon} |
| 136 | +print 'first' in dict_comprehension |
| 137 | +print 'FIRST' in dict_comprehension |
| 138 | +print len(dict_of_weapons) |
| 139 | +print len(dict_comprehension) |
0 commit comments