Python quickstart for programmers: Python Kung Fu

download Python quickstart for programmers: Python Kung Fu

If you can't read please download the document

description

A quick introduction to Python aimed at programmers.

Transcript of Python quickstart for programmers: Python Kung Fu

  • 1. Python Kung Fu

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

6. Hello World

  • print(''Hello world'')
    • Python 3
  • print ''Hello World''
    • Python 2.6
  • Notice the missing ;

7. Comments

  • #This is a comment

8. types

  • Numeric
    • Long or int e.g. 1
    • Float e.g. 2.5
    • Complex e.g. 3 + 4j
  • Boolean
    • True
    • False
    • or
    • and

9. Types cont.

  • Strings
    • 'I am a string'
    • Im also a string
  • Sequences
    • Lists (or array)[0,1,3] [2,5,woo hoo]
    • Tuples (1,2,3)

10. Types cont.

  • Collections
    • Dictionary (or hash or map)
      • {123:mi casa, 234:casa de pepito}
    • Set
      • set('hola')
  • Additional types
    • unicode, buffer, xrange, frozenset, file

11. Assingments

  • id = 10
  • Watch out for aliasing!

12. Operators

  • Cool new stuff
    • **
    • //
  • Sorry no ++ or -
  • The rest is about the same
  • Including this
    • +=-=*=/=%=**=//=

13. BIFs

  • y = abs( x )
  • z = complex( r, i )

14. modules

  • from math import *
  • from math import sqrt

15. Objects and Methods

  • Construction
    • today = date( 2006, 8, 25 )
    • Notice the missing 'new'
  • Methods
    • 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

  • ifvalue < 0:
    • print 'Im negative'
  • elif value == 0:
    • print 'Im zero'
  • else
    • print 'Im positive'

18. Comparison

  • Your usual stuff
    • ===!=

19. Comparison cont.

  • str1 = "Abc Def"
  • str2 = "Abc def"
  • if str1 == str2 :
  • print "Equal!!"
  • else :
  • print "Not Equal!!"

20. Comparison cont.

  • name1 = "Smith, John";
  • name2 = "Smith, Jane";
  • if name1 < name2 :
  • print "Name1 comes first."
  • elif name1 > name2 :
  • print "Name2 comes first."
  • else :
  • print "The names are the same."

21. Stuff that rules

  • if "Smith" in name1 :
  • print name1

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 >> 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

56. cont.

  • def __str__( self ) :
  • return "(" + str( self.yCoord ) + ", " +
  • str( self.yCoord ) + ")"
  • def getX( self ) :
  • return self.XCoord

57. cont.

  • def getY( self ) :
  • return self.yCoord
  • def shift( self, xInc, yInc ) :
  • self.xCoord += xInc
  • self.yCoord += yInc

58. Objectinstantiation

  • from point import *
  • pointA = Point( 5, 7 )
  • pointB = Point()

59. Private members and methods

  • Private
    • def __helpermethod
    • def __privatefield

60. Inheritance

  • class DerivedClassName(BaseClassName):
  • .
  • .
  • .

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

69. Python Implementations

  • CPython 2.6 and 3.0
  • CPython with Psyco
  • Jython
  • IronPython
  • PyPy
  • Stackless Python
  • and more...