python的快速参考手册
本手册来自图书《Introduction to Computation adn Programming Using Python》。
Common operations on numerical types
i+j
is the sum of i and ji-j
is i minus ji*j
is the product() of i and ji//j
is integer divisioni/j
is i divided by j. In Python 2.7, when i and j are both of type int, the result is also an int, otherwise the result is a floati%j
is the remainder(余数) when the int i is divided by the int ji**j
is i raised to the power jx += y
is equivalent tox = x + y
.*=
and-=
work the same way
Comparison and Boolean operators
x == y
returns True if x and y are equalx != y
returns True if x and y are not equal<, >, <=, >=
have their usual meaningsa and b
is True if both a and b are True, and False otherwisea or b
is Ture if at least one of a and b is True, and False otherwisenot a
is True if a is Fasle, and False if a if True
Common operations on sequence types
seq[i]
returns the ith element in the sequencelen(seq)
returns the length of the sequenceseq1 + seq2
concatenates(链接) the two sequencesn*seq
returns a sequence that repeats seq n timesseq[start:end]
returns a slice(切片) of the sequencee in seq
tests whether e is contained in the sequencee not in seq
tests whether e is not contained in the sequencefor e in seq
iterates(迭代) over the elements of the sequence
Common string methods
s.count(s1)
counts how many times the string s1 occurs in ss.find(s1)
returns the index of the first occurrence of the substring s1 in s; -1 if s1 is not in ss.rfind(s1)
same as find, but starts fron the end of ss.index(s1)
same as find, but raises an exception if s1 is not in ss.rindx(s1)
same as index, but starts from the end of ss.lower()
converts all uppercase letters to lowercases.replace(old, new)
replaces all occurrences of string old with string news.rstrip()
removes trailling white spaces.split(d)
Splits s using d as a delimiter(分隔符). Return a list of substrings of s
Common list methos
L.append(e)
add the object(对象) e to the end of LL.count(e)
returns the number of times that e occurs in LL.insert(i, e)
inserts the object e into L at index iL.extend(L1)
appends(追加) the items in list L1 to the end of LL.remove(e)
deletes the first occurrence of e from LL.index(e)
returns the index of the first occurrence of e in LL.pop(i)
removes and returns the item at index i. Defaults to -1L.sort()
has the side effect of sorting the elements of LL.reverse()
has the side effect of reversing the order of the elements in L
Common operations on dictionaries
len(d)
returns the number of items in dd.keys()
returns a list containing the keys in dd.values()
returns a list containing the values in dk in d
returns True if key k is in dd[k]
returns the item in d with key k. Raises KeyError if k is not in dd.get(k, v)
returns d[k] if k in d, and v otherwised[k] = v
associates the value v with the key k. If there is already a value associated with k, that value is replaced.del d[k]
removes element with key k from d. Raises KeyError if k is not in d.for k in d
iterates over the keys in d
Comparison of common non-scalar types(non-scalar: have internal structure that can be accessed)
Type | Type of Index | Type f element | Examples of literals | Mutable(可变的) |
---|---|---|---|---|
str | int | characters | ’’, ‘a’, ‘abc’ | No |
tuple | int | any type | (), (3,), (‘abc’, 4) | No |
list | int | any type | [], [3], [‘abc’, 4] | Yes |
dict | Hashable objects | any type | {}, {‘a’: 1}, {‘a’:1, ‘b’:2.0} | Yes |
Common input/output mechanisms
raw_input(msg)
prints msg and then returns value entered as a stringprint s1, ..., sn
prints strings s1, …, sn with a space between eachopen('fileName', 'w')
creates a file for writingopen('fileName', 'r')
opens an existing file for readingopen('fileName', 'a')
opens an existing file for appendingfileHandle.read()
returns a string containing contents of the filefileHandle.readline()
returns the next line in the filefileHandle.readlines()
returns a list containing lines of the filefileHandle.write(s)
write the string s to the end of the filefileHandle.writelines(L)
Writes each element of L to the filefileHandle.close()
closes the file