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.
- 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
- Everything! Ok not everything but almost
6. Hello World
7. Comments
8. types
9. Types cont.
-
- Lists (or array)[0,1,3] [2,5,woo hoo]
10. Types cont.
-
- Dictionary (or hash or map)
-
-
- {123:mi casa, 234:casa de pepito}
-
- unicode, buffer, xrange, frozenset, file
11. Assingments
12. Operators
- The rest is about the same
13. BIFs
14. modules
15. Objects and Methods
-
- today = date( 2006, 8, 25 )
-
- which_day = today.weekday()
16. Cooler printing
- print "The sum of %d and %d is %f " % (a, b, sum)
- print ''The sum of'' + a + '' and '' + b '' is '' + sum
17. Selection statements
18. Comparison
19. Comparison cont.
20. Comparison cont.
- print "Name1 comes first."
- print "Name2 comes first."
- print "The names are the same."
21. Stuff that rules
22. Null reference
- # The is operator along with None can tests for null
references.
- Watch out, ther is no Null!
23. Repetition Statements
- while i >> 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
- # Defines a class to represent two-dimensional discrete
points.
- def __init__( self, x = 0, y = 0 ) :
56. cont.
- return "(" + str( self.yCoord ) + ", " +
57. cont.
- def shift( self, xInc, yInc ) :
58. Objectinstantiation
59. Private members and methods
60. Inheritance
- class DerivedClassName(BaseClassName):
61. Stuff you maybe haven't tried
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
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)
64. Ta tan!
- [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
- myList = [ 12, 50, 5, 17 ]
- print "Error: Index out of range."
67. Raising exceptions
- def min( value1, value2 ) :
- if value1 == None or value2 == None :
68. Black belt python
69. Python Implementations