1. In the case of determining that you will not import multiple functions with the same name (imported from different modules), you can use the
From math import sqrt the From module import function
This allows direct use of sqrt (9) without having to use MATH.SQRT (9) each time
2, you can use variables to refer to functions (and most of the objects in Python), such as
>>> Import Math
>>> foo = math.sqrt
>>> int (foo)
4
3, the original string, before the string to be exported with an R, it will not be escaped (but the last character of the original string cannot be \ Unless it is escaped)
>>> print ' C:\now '
C:
ow
>>> print R ' c:\now '
c:\now
4, fragment, used to extract part of the sequence
>>> numbers = [1,2,3,4,5,6,7,8,9]
>>> numbers[3:6]
[4, 5, 6]
>>> numbers[ 0:1] #用 [ -3:-1] obtained 7, 8 #用 [-3:] Get the last three
[1]
>>> numbers[::2] #第三个数字为步长
[1, 3, 5, 7, 9]
>>> Numbers[8:3:-2] #步长不能为0, but can be negative
[9, 7, 5]
5, Index
>>> "Hello" [1] #[-1] got the last ' o '
e '
>>> forth = Raw_input ("Year:") [3]
year:2008
>>> forth
' 8 '
6. In membership, use the in operator to detect whether a value is in a sequence
Database = [
[' A ', ' 1234 '],
[' B ', ' 0985 '],
[' C ', ' 9527 '],
]
username = raw_input ("Username:")
pin = raw_input ("pin:")
If[username,pin] in database:
print "Access granted"
7. Piecewise Assignment
>>> name = List ("he")
>>> name
[' H ', ' e ']
>>> name[1:] = List ("is")
>>> name
[' H ', ' I ', ' s ']
>>> numbers = [1,5]
>>> numbers[1:1] = [2,3,4] # ' replace ' empty slices
>>> numbers
[1, 2, 3, 4, 5]
>>> numbers[1:3]=[] #删除
>> > Numbers
[1, 4, 5]
8, the copy of the list
y = x[:] Do not use y=x so that x and Y point to the same list
9, string format
>>> s = "Hello%s,%s python"
>>> values = ("I", "hate") <span style= "White-space:pre" > </span> #只有元组和字典可以格式化一个以上的值
>>> print s% values
Hello i,hate python
10. Template string
11, the Translate function of the string
>>> from string import maketrans
>>> table = Maketrans ("cs", "KZ") #maketrans参数为两个等长的字符串, Represents that each character in the first string is replaced with the character of the same position in the second string
>>> len (table)
256
>>> table[97:123]
' Abkdefghijklmnopqrztuvwxyz '
>>> Maketrans ("", "") [97:123]<span style= "White-space:pre" > </span> #maketrans中包含了256个ASCII字符
' abcdefghijklmnopqrstuvwxyz ' >>> ' This is
a incredible Test ". Translate (table)
' thiz iz a inkredible tezt '
12, = = and is
>>> x = y = [1,2,3]
>>> z = [1,2,3]
>>> x==y
True
>>> x is Y<span s tyle= "White-space:pre" > </span> #is是同一运算符, = = Determine whether two objects are equal, is judge whether equal (same object)
True
>>> x is Z
False
Not to be continued ...