CS 234 Python Review Part 2
Recap import function: define, return boolean, conditional, branching loop: for, range, while file: open, close, readlines string: split
Classes Define blueprint for a custom data type. Have all of the data types wrapped under one name. Important to have (self) as an argument to all methods in Python classes. self is the instance itself. Access class methods and attributes by the dot (. ) operator.
Classes: example Class CS234Students Name Program Year Midterm Marks Final Marks Assignment Marks
Classes: example
Classes: methods XXX: public method XXX: protected method XXX: private method
Classes: methods class Test1: def public method(self): print( This is a public method ); def protected method(self): print( This is a protected method ); def private method(self): print( This is a private method ); def call private(self): self. private method(); class Test2(Test1): def call protected(self): self. protected method();
Classes: magic methods init : called whenever a new class object is created. It is optional to have it in the program. (You are supposed to include this in your assignment.)... contains (xx): membership test, called by xx in object. len : returns length, called by len(object).
Classes: some useful magic methods Definition Usage equality object. eq (self,other) object==other addition object. add (self,other) object+other subtraction object. sub (self,other) object-other multiplication object. mul (self,other) object*other division object. truediv (self,other) object/other absolute value object. abs (self) abs(object) the official string object. repr (self) repr(object) the informal string object. str (self) str(object)
Recursion
Recursion: formally The solution to a problem depends on solutions to smaller instances of the same problem. Achieved by a function calling itself. It is important to have a base case, so that the recursion terminates at some point.
Recursion: example Fibonacci sequence: 1, 1, 2, 3, 5, 8,... def fibonacci(n): if n==0 or n==1: return 1; else: return fibonacci(n-1)+fibonacci(n-2); for i in range(0,10): print(fibonacci(i));
Lists List of comma-separated values (items) between square brackets. Items in the list can be of different types. Access and update items by indices in square brackets. list=[1,2,3,4, cat, dog,5,6,7]; print( list[0] :,list[0]); print( list[1] :,list[1]); list[3]= rat ; print( list[0:1] :,list[0:1]); print( list[2:5] :,list[2:5]);
Lists: operations append: adds a single element to the end of the list. extend: adds the elements in another list to the end of the list. do not return the new list, just modifies the original. list1=[1,2,3,4]; list1.append([5,6]); print( list1:,list1); list2=[1,2,3,4]; list2.extend([5,6]); print( list2:,list2);
Lists: + a=[1,2]; b=[3,4]; print(a+b);
Tuples Tuples are like lists except that once created, they cannot be changed. Tuples use round brackets. It s possible to add 2 tuples: a=(1,2, rat ); b=(5, cat, dog ); print(a+b);
Dictionaries A dictionary in Python is a group of key-value pairs. Each key is separated from its value by a colon (:), the items are separated by commas, and the whole thing is enclosed in curly braces. An empty dictionary: {}. Keys are unique within a dictionary while values may not be. The values of a dictionary can be of any type, but the keys must be of an immutable data type such as strings, numbers, or tuples. Very useful when we want a mapping between 2 entities in O(1) time.
Dictionaries # create a dictionary dict={5: cat,8: dog } print(dict); # add a key-value pair dict[3]= mouse ; print(dict); # remove a key-value pair del dict[3]; print(dict); # access a value print(dict[5]); # modify a value dict[8]= hot dog ; print(dict);
That s All