Control Statements 1
Conditionals 2
Loops 3
4 Simple Loop Examples def echo(): while echo1(): pass def echo1(): line = input('Say something: ') print('You said', line) return line def polite_echo(): while echo1() != 'bye': pass
Initialization of Loop Values 5
6 def recording_echo(): # initialize entry and lst vlst = [] # get the first input entry = echo1() # test entry while entry != 'bye': # use entry lst.append(entry) # change entry entry = echo1() # repeat # return result return lst
Looping Forever 7
Loops with Guard Conditions 8
Iterations Iteration Statements –Iteration statements all begin with the keyword for 9
Exception Handlers def get_gi_ids(filename): with open(filename) as file: return [extract_gi_id(line) for line in file if line[0] == '>'] IOError: [Errno 2] No such file or directory: 'aa2.fasta‘ 10
Python Errors –Tracebacks –Runtime errors 11
Exception Handling Statements 12
Classes 13
GeneBank Function 14
Problems of GeneBank Function Complexity –Using the representation in a program requires understanding its intricacies. Awkward navigation –Accessing specific parts of the representation requires tricky combinations of index expressions and function calls. Exposure to change –Changes to the representation require widespread changes in every program that uses it. 15
Defining Classes Python’s collection types work as follows: 1.The type is called like a function to create new instances. 2.The call to the type can include arguments used to initialize the new instance. 3.Methods are invoked on instances of types instead of accessing their internal representations. 4.Methods defined for different types can have the same name: when a method is called through a value, Python finds the appropriate function by looking in the definition of the value’s type. 5.The way an instance is printed is based on its type. 16
Defining Classes obj.name() obj.species 17
Example 18
Instance Attributes Access methods 19
Instance Attributes Predicate methods 20
Instance Attributes Initialization methodsString methods 21
Instance Attributes Modification methods Action methods 22 Support methods