@@ -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"""
475481git 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+
0 commit comments