Jason Koo

      Stay hungry, Stay foolish!

导航

Python知识点

Posted on 2013-04-12 16:44  Jason Koo  阅读(217)  评论(0编辑  收藏  举报

1. A Python file is called a module/package. A Python module can be run directly, or it can be imported and used by some other modules.

2. When a Python file is run directly, the special variable "__name__" is set to "__main__". Therefore, it's common to have the boilerplate if  __name__== ... to call a main function when the module is run directly, but not when the module is imported by some other modules.

3. In a standard Python program, the list sys.argv contains the command line arguments in the standard way with sys.argv[0] being the program itself, sys.argv[1] the first argument, and so on.

4. A string literal can span multiple lines, but there must be a backslash \ at the end of each line to escape the newline.

5. Unlike Java, the "+" does not automatically convert numbers or other types to string form. The str() function converts values to string form so they can be conbined with other strings.

6. There is no ++ operator, but +=, -=, etc. work.

7. If you want integer division, it is most correct to use 2 slashes-----e.g. 6//5 is 1. // for integer division ; / for decimal division

8. The "print" operator prints out one or more Python items followed by a newline(leave a trailing comma at the end of the items to inhibit the newline).

9. The "zero" values all count as false: None, 0, empty string, empty list, empty dictionary.

10. The boolean operator are the spelled out words *and*, *or*, *not*. (Python does not use the C-style &&, ||, !).

11. You can also use for/in to work out a string. The string acts like a list of chars, so "for ch in s: print ch" prints all the chars in a string s.

12. The range(n) function yields the numbers 0, 1, 2, ..., n-1, and range(a, b) returns a, a+1, ..., b-1------- up to but not including the last number. range(n) and range(a, b) can only generate number sequence in ascending order, in order to get numbers in descending order, use range(a, b, step), range(a, b, step) returns a, a + step, a + 2*step,... b-step. Another way to accomplish a decreasing range is reversed(range(x, y)), which goes from y-1 to x in decreasing order.

13. A tuple is a fixed size grouping of elements. Tuples are like lists except they are immutable and do not change size.  Tuples play a sort of "struct" role in Python------- a convenient way to pass around a little logical, fixed size buddle of values. A function that needs to return multiple values can just return a tuple of the values. Accessing elements in a tuple is just like a list--------len(), [] , for, in, etc. all work the same. To create a size-1 tuple, the lone element must be followed by a comma. --------e.g. tuple = ('hi',).   Assigning a tuple to an identically sized tuple of varible names assigns all the corresponding values. If the tuples are not the same size, it throws an error. e.g.--------------(x, y, z) = (42, 13, 'hike').

14. strings, numbers and tuples work as keys and any type can be a value. Looking up a value which is not in the dict throws a KeyError-----use "in" to check if the key is in the dict, or use dict.get(key) which returns the value or None if the key is not present.

15. A for loop on a dictionary iterates over its keys by default. The methods dict.keys() and dick.values() return list of the keys or values explicitly. There is also an items() which returns a list of (key, value) tuples. The dictionary takes in scattered data and make it into something coherent.

16. Being able to look at the printout of your variables at one state can help you think about how you need to transform those variables to get to the next state.

17. The Python "re" module provides regular expression support. e.g match = re.search(r'pattern', string). The "r" at the start of the pattern string designates a Python 'raw' string which passes through backslashes without change which is very handy for regular expressions. I recommend that you always write pattern strings with the 'r' just as a habit.

18. First, the search finds the leftmost match for the pattern and second it tries to use up as much of the string as possible.(leftmost and largest/greedy)

19. You can also use a dash(-) to indicate a range, so [a-z] matches all lowercase letters. To use a dash without indicating a range, put the dash last, e.g. [abc-]  .

20. An up-hat(^) at the start of a square-bracket set inverts it, so [^ab] means any char except 'a' or 'b'.

21. If you are using Python as a calculator or doing some small experiments, here is a useful shortcut. The underscore character _ refers to the result of the previous expression which the interactive prompt evaluated. For example, if you type 2+3 on one line, then _*2 on the next line, you will see the10.

22. Some languages, like Ruby or Python support parallel assignments, which simplifies the task of swapping two variables: a, b = b, a

23. In Python, any line of instructions containing the # symbol ("pound sign" or "hash") denotes the start of a comment. The rest of the line will be ignored when the program is run.  If a pound sign # appears in a string, then it does not get treated as a comment.

24. A block  means a series of commands that can be one or multiple lines long. Python determines where the block starts and stops using indentation, or in other words you need to put an equal number of spaces in front of each line of the block. All lines in the block must have exactly the same amount of indentation: they must start with the same number of spaces. Whenever Python sees that the next line has more spaces, it assumes a new block is starting, and when there are less spaces, the block ends.

25. Integer division with negative numbers: The expressions a // b and int(a / b) are the same when a and b are positive. However, when a is negative, a // b uses "round towards negative infinity" and int(a / b) uses "round towards zero."

26. A syntax error happens when Python can't understand what you are saying. A runtime error happens when Python understands what you are saying, but runs into trouble when following your instructions.

27. Another similar concept to scope is a namespace. A namespace is like a scope for a package.