2012年4月30日 星期一

Python - a study note

Reference : http://www.rexx.com/~dkuhlman/python_book_01.html
  • Naming
    • Allowed characters in a name: a-z A-Z 0-9 underscore, and must begin with a letter or underscore.
    • Names and identifiers are case sensitive.
    • Identifiers can be of unlimited length.Special names, customizing, etc. -- Usually begin and end in double underscores.
    • Special name classes -- Single and double underscores.
      • Leading double underscores -- Name mangling for method names.
      • Leading single underscore -- Suggests a "private" method name in a class. Not imported by "from module import *".
      • Trailing single underscore -- Sometimes used to avoid a conflict with a keyword, for example, class_.
    • Naming conventions -- Not rigid, but here is one set of recommendations:
      • Modules and packages -- all lower case.
      • Globals and constants -- Upper case.
      • Class names -- Bumpy caps with initial upper.
      • Method and function names -- All lower case with words separated by underscores.
      • Local variables -- Lower case (possibly with underscore between words) or bumpy caps with initial lower or your choice.
  • No declaration or data type definition is needed/used.
  • Block
    • Python represents block structure and nested block structure with indentation, not with begin and end brackets
    • The statements which go together must have the same indentation. Each such set of statements is called a block.
    • The empty block -- Use the pass no-op statement.
  • DocStrings : A doc string is a quoted string at the beginning of a module, function, class, or method.We can use triple-quoting to create doc strings that span multiple lines.
    • Doc strings can be viewed with several tools, e.g. help(),obj.__doc__, and, in IPython, a question mark (?) after a name will produce help.
  • Statement
    • while running:
          guess = int(raw_input('Enter an integer : '))
          if guess == number:
             print 'Congratulations, you guessed it.'
             running = False # this causes the while loop to stop
          elif guess < number:
             print 'No, it is a little higher than that.'
             continue

          else:
             print 'No, it is a little lower than that.'
             break

      else:
          print 'The while loop is over.'
      # Do anything else you want to do here
      print 'Done'
    • for i in range(1, 5):
         print i
      else:
         print 'The for loop is over'

沒有留言: