The document provides an overview of key Python concepts including data types, operators, control flow statements, functions, objects and classes. It discusses lists in depth, covering creation, iteration, searching and common list methods. It also briefly touches on modules, exceptions, inheritance and other advanced topics.
2. The Zen of Python Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules.
3. The Zen of Python cont. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never.
4. The Zen of Python cont. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those!
5. Uses Prototyping Glue language Web development System administration Desktop applications Games Everything! Ok not everything but almost
22. Null reference # The is operator along with None can tests for null references. result = name1 is None result2 = name1 == None Watch out, ther is no Null!
23. Repetition Statements theSum = 0 i = 1 while i <= 100: theSum = theSum + i i = i + 1 print "The sum = ", theSum
24. Repetition Statements cont. for <loop-var> in <object> : <statement-block> for i in xrange( 1, 11 ) : print i
35. Python lists rule Don't worry about list lengths. The interpreter will take care of that for you. I case you want to simulate a classic array you can do this. histogram = [ 0 ] * 10
36. List iteration total = 0 for value in valueList : total = total + value avg = float( total ) / len( valueList ) print "The average value = %6.2f" % avg
37. Tuples, tuples. Immutable = safety t = ( 0, 2, 4 ) # 3 element tuple a = ( 2, ) # 1 element tuple b = ( 'abc', 1, 4.5, 5 ) # 4 element mixed tuple c = ( 0, ( 'a', 'b' ) ) # nested tuple
38. List ninja skills import random # Create the empty list. valueList = [] # Build the list of random values. for i in range( 1000 ) : valueList.append( random.random() )
45. This isn't funny anymore // Java item search. int i = 0; while( i < gradeList.length ) { if( gradeList[ i ] == 100 ) System.out.println( "Someone received at least one " + " perfect score." ); i++; }
46. Haha if 100 in gradeList : print "Student received at least one perfect score."
48. Min max print "The minimum value = ", min( valueList ) print "The maximim value = ", max( valueList )
49. List concatenation listA = [ 1, 2, 3 ] listB = [ 8, 9 ] which can be concatenated to produce a third list. If we execute the statements bigList = listA + listB print bigList
50. List duplication listX = [ 1, 2, 3 ] listY = listX + [] listY[0] = 0 print "X:", listX print "y:", listY Will now produce the following since listY now references a new list which happens to be a duplicate copy of listX.
53. The coolest shit ever The following are common ways to describe lists (or sets, or tuples, or vectors) in mathematics. S = {x² : x in {0 ... 9}} V = (1, 2, 4, 8, ..., 2¹²) M = {x | x in S and x even}
54. Wuahahahha! >>> S = [x**2 for x in range(10)] >>> V = [2**i for i in range(13)] >>> M = [x for x in S if x % 2 == 0]
55. OO programming # point.py # Defines a class to represent two-dimensional discrete points. class Point : def __init__( self, x = 0, y = 0 ) : self.xCoord = x self.yCoord = y
61. Stuff you maybe haven't tried Multiple Inheritance Operator overloading
62. Extra stuff you should know There are no 'interfaces' but you can emulate them. There is no 'abstract' or 'virtual' keyword but you can emulate this behaviour. Abstract base classes serve as THE alternative to interfaces. Python ABC's are somewhat similar to C++ ABC's Duck typing
63. If it walks like a duck and quacks like a duck, I would call it a duck. function calculate(a, b, c) => return (a+b)*c example1 = calculate (1, 2, 3) example2 = calculate ([1, 2, 3], [4, 5, 6], 2) example3 = calculate ('apples ', 'and oranges, ', 3) print to_string example1 print to_string example2 print to_string example3
64. Ta tan! 9 [1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6] apples and oranges, apples and oranges, apples and oranges,
65. Conclusion Thus, duck typing allows polymorphism without inheritance. The only requirement that function calculate needs in its variables is having the "+" and the "*" methods
66. Exceptions try: myList = [ 12, 50, 5, 17 ] print myList[ 4 ] except IndexError: print "Error: Index out of range."
67. Raising exceptions def min( value1, value2 ) : if value1 == None or value2 == None : raise TypeError if value1 < value2 : return value1 else: return value2
68. Black belt python Class factories Function factories Functional programming Generators Advanced list idioms Tons of other tricks