Skip to content

Commit ecf25bf

Browse files
committed
Merge pull request #55 from salimhamed/master
Session 4 Homework
2 parents 344b798 + 6e49646 commit ecf25bf

27 files changed

+1030
-0
lines changed
376 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

Examples/Session05/codingbat.pyc

571 Bytes
Binary file not shown.

Students/salim/notes/class_notes.py

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,12 @@ def complex_function(arg1, arg2, kwarg1=u'bannana'):
471471

472472
############################## SESSION03 ##############################
473473

474+
"""
475+
making a python file an executable:
476+
- change the mode of the file to executable: chmod +x myscript.py
477+
- add the path to python at the top with '#!' before
478+
"""
479+
474480
"""
475481
git question:
476482
- create a pull request for the branch itself
@@ -643,3 +649,148 @@ def func(c, list=[]): # this is wrong because it dones't create a function le
643649
return list.append(c)
644650

645651

652+
############################## SESSION03 ##############################
653+
"""
654+
you almost never have to loop through sequences using range()
655+
- zip() will zip lists together
656+
- unpack embedded squences using this construct
657+
"""
658+
659+
a_list = [(1,2), (3,4), (5,6)]
660+
661+
for i, j in a_list:
662+
print i
663+
print j
664+
665+
"""
666+
enumerate allows you to get the indix while you are looping through a sequence
667+
"""
668+
669+
for i, item in enumerate(a_list):
670+
print i
671+
print item
672+
673+
"""
674+
buidling up a long string:
675+
- one option is to continue to keep += to the string. however, this is
676+
an inefficent process
677+
- a better way is to build a list, then use " ".join(list)
678+
679+
sorting data:
680+
- when you
681+
682+
"""
683+
684+
fruits = ['Apples', 'Pears', 'Grapes']
685+
numbers = [1, 2, 3]
686+
combine = zip(fruits, numbers)
687+
688+
def sort_key(item):
689+
return item[1]
690+
691+
combine.sort(key=sort_key)
692+
693+
"""
694+
you can build up strings for formatting before you subistiute values in
695+
"""
696+
697+
s = 'Hello ' + '%d' * 4
698+
print s % (1,2,3,4)
699+
700+
"""
701+
dictionaries:
702+
- create a dict with d = {}
703+
- keys don't have to be the same type and values don't have to be the same
704+
type
705+
- keys can be any immutable object (technically "hash" objects):
706+
- number
707+
- string
708+
- tuple
709+
- dictionaries are unordered (due to the way they are built using hasing)
710+
- dictionaries are very effecicent
711+
712+
dictionary methods
713+
- dict.setdefault() <-- will use this in the homework
714+
- dict.iteritems() <-- will not return a list
715+
716+
hashing:
717+
- missed this so read the class notes
718+
"""
719+
720+
"""
721+
Exceptions:
722+
- you can use "finally" at the end of an exception, which will always get
723+
run
724+
- they also have an "else" which will run if there is no exception
725+
"""
726+
# never do this! this doesn't give you information about the exception
727+
try:
728+
do_something()
729+
except:
730+
print "something went wrong."
731+
732+
"""
733+
reading and writing files:
734+
735+
text files:
736+
- f
737+
"""
738+
739+
f = open('secrets.txt')
740+
secret_data = f.read()
741+
f.close()
742+
743+
744+
745+
############################## SESSION04 ##############################
746+
747+
""" dealing with "ordered" / "sorted" dicts """
748+
749+
# option #1
750+
import collections # package with tools for dealing with dicts
751+
o_dict = collections.OrderedDict() # this will create an ordered dict
752+
753+
# option #2
754+
sorted() # built in function that sorts iterables
755+
756+
""" Advanced argument passing """
757+
758+
# keyword arguments
759+
def fun(a, b = True, c = False):
760+
return
761+
762+
# functional arguments
763+
764+
def func(x, y, w=0, h=0): # positional arguments are tuples
765+
# keywork arguments are dictionaries
766+
print "%s %s %s %s" % (x, y, x, h)
767+
768+
a_tuple = (1, 2)
769+
a_dict = {'w':1, 'h': 2}
770+
func(*a_tuple, **a_dict) # you can pass the tuple and dict in directly
771+
772+
def func(*args, **kwargs): # you can recieve an undefined number of args
773+
print args
774+
print kwargs
775+
776+
"""mutablility"""
777+
778+
import copy # for making copies of objects
779+
780+
copy.deepcopy() # this is how you make a deep copy
781+
782+
783+
"""list comprehensions"""
784+
l = [1, 2, 3]
785+
[i * 2 for i in l]
786+
[i * 2 for i in l if i > 1] # you can have an if statement here
787+
788+
# searching 'in' a sequence is faster with sets because they are hash tables
789+
790+
"""set comprehension"""
791+
792+
"""dict comprehensions"""
793+
794+
795+
"""testing in python"""
796+
16 KB
Binary file not shown.

Students/salim/session03/list_lab.py

100755100644
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env python
22

3+
34
# print original list
45
a_list = ['Apples', 'Pears', 'Oranges', 'Peaches']
56
print a_list
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/usr/bin/env python
2+
3+
4+
# #############################Lesson 1##############################
5+
6+
# create dict
7+
d1 = {'name': 'Chris', 'city': 'Seattle', 'cake': 'Chocolate'}
8+
print d1
9+
10+
# delete entry for cake
11+
del d1['cake']
12+
print d1
13+
14+
# add an entry to the dict
15+
d1['fruit'] = 'Mango'
16+
print d1
17+
18+
# display the values
19+
d1.values()
20+
21+
# display the keys
22+
d1.keys()
23+
24+
# is 'cake' a key in the dict?
25+
'cake' in d1
26+
27+
# is 'Mango' a value in the dict?
28+
'Mango' in d1.values()
29+
30+
31+
# #############################Lesson 2##############################
32+
33+
# create list from 0 to 15
34+
l2 = range(16)
35+
36+
# create list with hex representation
37+
s2 = []
38+
for item in l2:
39+
s2.append(hex(item))
40+
41+
# zip lists into dict
42+
d2 = dict(zip(l2, s2))
43+
44+
45+
# #############################Lesson 3##############################
46+
47+
# use d1 to create a new dict with same keys but number of 't's in values
48+
d3 = {}
49+
for key, value in d1.iteritems():
50+
d3[key] = value.count('t')
51+
52+
53+
# #############################Lesson 4##############################
54+
55+
# make three sets
56+
s2 = set(range(0, 20, 2))
57+
s3 = set(range(0, 20, 3))
58+
s4 = set(range(0, 20, 4))
59+
60+
# print the sets
61+
print s2
62+
print s3
63+
print s4
64+
65+
# check sets
66+
s3.issubset(s2)
67+
s4.issubset(s2)
68+
69+
70+
# #############################Lesson 4##############################
71+
72+
a_set = set('Python')
73+
a_set.add('i')
74+
75+
b_set = frozenset('marathon')
76+
77+
u_set = a_set.union(b_set)
78+
i_set = a_set.intersection(b_set)
79+
80+
print u_set
81+
print i_set

0 commit comments

Comments
 (0)