0. Common methods or functions:
1.urllencode () converts a string, etc. to a URL. (from Urllib.parse import UrlEncode)
2. Code: Json.dumps (). Converts a Pyhton object encoding into a JSON string.
Decoding: Json.loads (). Decodes a JSON-formatted string into a Python object.
3.soup.select:css Selector
4.hashlib provides a common digest algorithm: MD5,SHA1
Abstract the algorithm is also called hash algorithm and hashing algorithm.
It converts any length of data into a fixed-length data string (usually represented by a 16-binary string) through a function.
Hashlib. MD5 (). Hexdigest () output encrypted ciphertext
5.format (): formatted string. http://www.cnblogs.com/hellofengying/p/4272569.html
6.sys.path. Append ("/*"). Add path under current directory *
7.property. Encapsulation of methods, developers more convenient when setting data on attributes
num = Property (Getnum, Setnum) or @property @num. Setter
8.
1. Note: Single line comment: #
Multi-line Comment: ' content '
2.python with line wrap, end= ' \ n '. If written as end= "" does not break.
End can also be followed by "+", "=" and so on
3.python2 Input Function: raw_input
Python3 Input function: input (both have the same effect)
4.print ("Hello" *10) (Hello is the content to output, 10 is the number of passes to output)
5. Swap variable values: a=100,b=200
A, B = b,a can be a=200,b=100
6. Generate Random number import random
Random.randint (x, y) generates a random number greater than or equal to x less than or equal to Y
7. Length Len (XXX)
8. Slicing name= "ABCDEFGHIJKL"
Name[0]=a name[-1]=l
Name[0:2]=ab
name[::]---> ' abcdefghijkl '
Name[::2]--> ' Acegik '
Name[::-1]--> ' LKJIHGFEDCBA '
9. Delay Import Time
Time.sleep (1) units: seconds
10. Global variables can be called arbitrarily in a function, but cannot be modified,
Unless you add a: global variable name to the function, the modification will affect the global
mutable types need to be added to global, immutable and not required
11. function returns multiple data in three ways: tuple (), list [], dictionary {}
12. Sort Xxx.sort () from small to large xxx.sort (Reverse=true) from largest to smallest
If the list is a dictionary, you need to use the anonymous function (lambda)
A.sort (Key],reverse=true of Key=lambda x:x[dictionary)
13. Indefinite length parameter: *args (tuple), **kwargs (dictionary, incoming mode: xx=123),
Unpack
When a list/tuple is passed as an argument, if there is a * in front of it, it is unpacked
It means: [11,22,33]----->11,22,33
When a dictionary is passed as an argument, if there are two * in front of it, it is unpacked
{"AA": $, "BB": $---->aa=100,bb=200
14. Variable data type: list, dictionary
Immutable data types: numbers, strings, tuples
A key in a dictionary can only be an immutable type
The address of 15.a=a+a A has changed
A+=a A's address does not change
16. The function is the stack, the process of the stack
17. When there are two function names, the latter will automatically overwrite the former, but will not cause an error.
18.os.listdir ("directory") gets the names of all the files in the current directory
19.>>> s = set ([1,5,6])
>>> s
{1, 5, 6}
>>> d = ([2,5,8])
>>> D
[2, 5, 8]
>>> type (s)
<class ' Set ' >
>>> Type (d)
<class ' list ' >
20. Determine whether an object is an iterative object: interable
From collections Import iterable
>>> isinstance (' abc ', iterable) # whether STR can iterate
True
>>> ([isinstance], iterable) # Whether the list can be iterated
True
>>> isinstance (123, iterable) # integers can be iterated
False
An object that can be called by the next () function and continually returns the next value is called an iterator: Iterator
You can use Isinstance () to determine whether an object is a iterator object:
>>> from collections Import Iterator
>>> Isinstance ((x for X in range), Iterator)
True
>>> isinstance ([], Iterator)
False
21. List Builder: [x * x for x in range (1, 11)]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
You can also use a two-layer loop to generate a full array:
[M + N for m in ' ABC ' for n in ' XYZ ']
[' AX ', ' AY ', ' AZ ', ' BX ', ' by ', ' BZ ', ' CX ', ' CY ', ' CZ ']
To turn all the strings in a list into lowercase:
L = [' Hello ', ' World ', ' IBM ', ' Apple ']
[S.lower () for s in L]
[' Hello ', ' World ', ' IBM ', ' Apple ']
22. Yang Hui triangle:
Def Yanghui ():
L = [1]
While True:
Yield L
L.append (0)
L = [L[i-1]+l[i] for I in range (len (L))]
n = 0
For T in Yanghui ():
Print (t)
n = n + 1
if n = = 10:
Break
23. The function itself can also assign a value to a variable
>>>F = ABS
>>>f (-10)
10
24. Passing functions as parameters, such functions are called higher-order functions, and functional programming refers to this highly abstract programming paradigm.
From Functools import reduce
25. Multi-process: from multiprocessing import Pool
Group_start = 1
Group_end = 20
if __name__ = = "__main__":
groups = [x * for x in range (Group_start, Group_end + 1)]
Pool = Pool ()
Pool.map (Main, groups)
26.==. Judging is not the same content
Is. To determine whether the same address, space.
27.__XX__ represents the function that the system gives
_ Class name __ Private property
Python Learning Essays