Saturday, May 30, 2009

Python 3

1. Parentheses are optional

2. End of line is end of statement

3. End of indentation is end of block
Inside a block all the indentation should be the same.

4. Multi-statements per line, separate them using semicolon ';'
5. One statement span across multiple lines, by putting the statements into a bracketed pair - parentheses "()", square brackets "[]", or dictionary braces "{}".

6. raw_input() for console input

7. print to file: print >> filename, a, b, c

8. Statements execute one after another, until you say otherwise

9. Block and statement boundaries are detected automatically

10. Compound statements=header,":" indented statements

11. Blank lines, spaces, and comments are usually ignored

12. Docstrings are ignored, but saved and displayed by tools

13. A=Y if X else z

14. while
      while <test1>:

15. pass, continue, break, else

16. for
      for <target> in <object>:

17. the else statement in while and loop will be executed only when the loop is terminated because of the testing condition is broken, i.e. else statement won't be executed when the loop is stopped by a break in the <statements1>.

18. File scanners:
read file character by character
      for char in open('test.txt').read():
              print char

read file line by line
     for line in open('test.txt').realines():
             print line

     for line in open('test.txt').xreadlines():
              print line

readlines() load the file once, xreadlines() load the file when demanded.
We can also
     for line in open('test.txt')
             print line

19. range()

20. Parallel Traversals: zip and map
Using zip to construct dictionaries.

21. Don't forget the colons

22. Start in column 1

23. Blank lines matter at the interactive prompt

24. Indent consistently.

25. Don't code C in python

26. Use simple for loops instead of while or range

27. Beware of mutables in assignments

28. Don't expect results from functions that change objects in-place

29. Always use parentheses to call a function

30. Don't use extensions or paths in imports and reloads

31. def Statements for function definition
    def <name>(arg1, arg2,..., argN):
           return <value>

32. Python Scope Basics
    a. The enclosing module is a global scope;
    b. The global scope spans a single file only;
    c. Each call to a function creates a new local scope;
    d. Assigned names are local unless declared global;
    e. All other names are enclosing locals, globals, or built-ins.

33. Immutable arguments are passed by value
      Mutable arguments are passed by pointer

34. lambda Expressions
    lambda arg1, arg2,... , argN : expression using args

No comments:

Post a Comment