This week, I had a project written in Python, so I had to learn about it. Compared with the golden combination of C # + vs. net, Python + VI is still very uncomfortable to use, but work is more important than interest and hard to learn if you don't like it.
First of all, I feel that python has many similarities with C # in terms of concept, such as producing intermediate byte code.
1. Strange operator number: divmod
>>> (A, B) = divmod (10, 3)
>>> Print a, B is 3 1
2. logical expression:
Like C, there is no boolean type. False can be represented by the following: None, 0, 0.0.
3. The syntax is distinguished by indentation. Be careful.
4. List, similar to the arraylist in C. In []
A = ['A', 'B'], then a [0] = 'A', a [1] = 'B', using Len () the list length is 2.
5. The extraction of sub-lists is a wide variety of tricks.
A = [1, 2, 3, 4, 5]
A [] = [2, 3, 4] is actually a [1], a [2], a [3]. Note that it does not include a [4].
A [-1] = 5,-Indicates taking from the back.
A [:] is all
A [: 3] is a [0], a [1], a [2]
A [3:] is all after a [3], that is, a [3], a [4]
A [1:-1] is a [1], a [2], a [3], excluding a [-1] (that is, a [4]).
6. List Processing Method
A = [0, 1, 2, 3, 4, 5]
A. append (6), then a = [0, 1, 2, 3, 4, 5, 6]
A. acount (1) = 1, used to calculate the number of times 1 appears in this list.
Len (a) is the length, at this time = 7
A. Extend can also use list as a parameter, such as A. Extend ([]).
A. index (6) returns the position of 6 in this list, = 6, if you. index (10) will throw an exception. I don't know why-1 is returned like other languages. This is what I previously said. Program A dialect in a language.
A. insert (0,-1) inserts-1 at the position 0. At this time, a = [-1, 0, 1, 2, 3, 4, 5, 6, 7, 8]
A. Pop (), delete and return the last element. A. Pop () = 8. At this time, a = [-1, 0, 1, 2, 3, 4, 5, 6, 7]
A. Pop (1), pop out a [1]. A. Pop (0) is pop output a [0]
A. Remove (7), find the first 7, and then delete the 7 found. At this time, a = [-1, 0, 1, 2, 3, 4, 5, 6]
A. Reverse (), the order is reversed. At this time, a = [6, 5, 4, 3, 2, 1, 0,-1]
A. Sort () sorting. At this time, a will be sorted in an ascending or small order. A = [-1, 0, 1, 2, 3, 4, 5, 6]
Like C #, A. Sort (fun) can also be used as a sorting function.
If you know all the operation functions for a, Dir (a) will list them for you. For example:
['_ Add _', '_ class _', '_ ins INS _', '_ delattr __', '_ delitem _', '_ delslice _', '_ Doc _', '_ eq _', '_ Ge __', '_ getattribute _', '_ getitem _', '_ getslice _', '_ GT _', '_ Hash __', '_ iadd _', '_ imul _', '_ init _', '_ Le _', '_ Len __', '_ LT _', '_ Mul _', '_ ne _', '_ new _', '_ reduce __', '_ repr _', '_ rmul _', '_ setattr _', '_ setitem _', '_ setSlice __', '_ STR _', 'append', 'Count', 'extend', 'index', 'insert', 'pop', 'delete', 'reverse ', 'sort ']
7. A novel loop for list
[Express1 for K in list if express2]
For example, a = [1, 2, 'a']
[K + 1 for k in a if type (K) = types. inttype] will get [2, 3]. Of course, first import types
8, string
"Is here document. For example
"""
Line1
Line 2
"""
Special symbol. \ U {XXXX} Unicode, \ xhh, hexadecimal, \ 0 octal.
String Conversion:
Float (STR), converted to float, A = '2. 3', float (A) = 2.3
INT (STR), converted to int. Note that you must be able to convert to int, A = '2. 3', INT (a) will cause an error. A = '2. 0', INT (a) also has an error. A = '2' no error
INT (STR, base) is converted in hexadecimal notation. A = '15', INT (A, 8) = 13, which means to convert the octal value 15 to the decimal value 13. The final result must be decimal.
Long (STR), long (STR, base) to long type.
String operation:
Connection: +, 'Hello' + ''+ 'World'
Capitalize () is capitalized. 'Hello world'. capitalize () = 'Hello world'
Lower () is changed to lower case, upper () is changed to upper case, and swapcase () is case-insensitive.
Len () returns the string length, A = 'abc', a [0] = 'A', a [-1] = 'C'
Judge the string category:
Str. isalnum (), matching 0-9, A-Za-z
Str. isalpha (), A-Za-z
Str. isdigit (), 0-9
Str. islower (), A-z
Str. isupper (), A-Z
Str. istitle (), uppercase letters
Str. isspace (), white space
String SEARCH:
A. Find ('A') finds the location of 'A'. If no location is found,-1 is returned.
A. Find ('A', 1) start from a [1]
A. Find ('A', 1, 3), from a [1] to a [3.
A. rfind () from the end
S. Index () is the same as find (). If no value is found, an exception is returned.
S. rindex () from the end
S. Count ('A'), the number of occurrences.
string merging and decomposition:
Str. join (words), S. split ('')
'\ n '. join (['A', 'B', 'C']) = 'a \ Nb \ NC '
'have a Good day '. split ('') = ['have ', 'A', 'good', 'day']