Python Manual Notes1
python3.1似乎和以前的2.X版本不同了。至少 print ‘hello world’被认为是语法错误,而要写成函数形式—— print(“hello world”)
1. Comments in Python start with the hash character, #, and extend to the end of the physical line. A hash character within a string literal is just a hash character.
2. numbers
1)
2)To do integer division and get an integer result, discarding any fractional result, there is another operator, //:
3)Variables must be “defined” (assigned a value) before they can be used, or an error will occur.
4)Complex numbers are also supported; imaginary numbers are written with a suffix of j or J. Complex numbers with a nonzero real component are written as (real+imagj), or can be created with the complex(real, imag) function.
To extract these parts from a complex number z, use z.real and z.imag.
The conversion functions to floating point and integer (float(), int()) don’t work for complex numbers — there is not one correct way to convert a complex number to a real number. Use abs(z) to get its magnitude (as a float) or z.real to get its real part:
3.Strings
1) They can be enclosed in single quotes or double quotes. Pay attention to the \ in the string.
2)Continuation lines can be used, with a backslash as the last character on the line indicating that the next line is a logical continuation of the line.
3)strings can be surrounded in a pair of matching triple-quotes: """ or '''. End of lines do not need to be escaped when using triple-quotes, but they will be included in the string.
4)If we make the string literal a “raw” string, \n sequences are not converted to newlines, but the backslash at the end of the line, and the newline character in the source, are both included in the string as data.
code:
result:
5)Strings can be concatenated (glued together) with the + operator, and repeated with *:
6)Two string literals next to each other are automatically concatenated; the first line above could also have been written word = 'Help' 'A'; this only works with two literals, not with arbitrary string expressions:
7)Slice indices have useful defaults; an omitted first index defaults to zero, an omitted second index defaults to the size of the string being sliced.
8)Unlike a C string, Python strings cannot be changed. Assigning to an indexed position in the string results in an error.However, creating a new string with the combined content is easy and efficient. Degenerate slice indices are handled gracefully: an index that is too large is replaced by the string size, an upper bound smaller than the lower bound returns an empty string.
9)Indices may be negative numbers, to start counting from the right. But note that -0 is really the same as 0, so it does not count from the right! Out-of-range negative slice indices are truncated, but don’t try this for single-element (non-slice) indices. For example:
10)The built-in function len() returns the length of a string.
4. Unicode
5. Lists
The most versatile is the list, which can be written as a list of comma-separated values (items) between square brackets. List items need not all have the same type.
Unlike strings, which are immutable, it is possible to change individual elements of a list.
It is possible to nest lists (create lists containing other lists), for example:
6. Programming
The body of the loop is indented: indentation is Python’s way of grouping statements. Python does not (yet!) provide an intelligent input line editing facility, so you have to type a tab or space(s) for each indented line.
The keyword end can be used to avoid the newline after the output, or end the output with a different string: