Python learning and python Learning

Source: Internet
Author: User

Python learning and python Learning

0. Common methods or functions:
1. urllencode () converts string and so on to URL. (from urllib. parse import urlencode)
2. encoding: json. dumps (). encode a Pyhton object into a Json string.
Decoding: json. loads (). decodes json strings into Python objects.
3. soup. select: CSS Selector
4. hashlib provides common digest algorithms: MD5, SHA1
Digest algorithms are also called hash algorithms and hash algorithms.
It converts data of any length into a fixed-length data string (usually expressed in hexadecimal strings) through a function)
Hashlib. MD5 (). hexdigest () outputs the encrypted ciphertext
5. format (): format String. http://www.cnblogs.com/hellofengying/p/4272569.html
6. sys. path. Append ("/*"). Add the path under the current directory *
7. property. encapsulate methods to make it easier for developers to set data for properties.
Num = property (getNum, setNum) or @ property @ num. setter
8.
1. Comment: single line comment :#
Multi-line comment: '''content '''

2. The line feed is self-contained in python, and end = '\ n'. If it is written as end = "", no line feed is provided.
End can be followed by "+", "=", etc.

3. python2 input function: raw_input
Python3 input function: input (the two have the same effect)

4. print ("hello" * 10) (hello is the content to be output, and 10 is the number of times to be 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 and less than or equal to y.

7. len (xxx)

8. Slice name = "abcdefghijkl"
Name [0] = a name [-1] = l
Name [0: 2] = AB
Name [:] ---> 'abcdefghijkl'
Name [: 2] --> 'acegik'
Name [:-1] --> 'lkjihgfedcba'

9. delayed import time
Time. sleep (1) Unit: seconds

10. global variables can be called at will in the function, but cannot be modified,
Unless the global variable name is added to the function, the global variables will be affected after modification.
Global is required for variable types, but not for immutable types.

11. The function returns multiple data records in three ways: tuples (), list [], Dictionary {}

12. sort xxx. sort () from small to large xxx. sort (reverse = True) from large to small
If the list contains a dictionary, you need to use an anonymous function (lambda)
A. sort (key = lambda x: x [dictionary key], reverse = True)

13. Variable Length Parameter: * args (tuples), ** kwargs (Dictionary, input method: xx = 123 ),
Unpack:
When the list/tuples are passed as real parameters, if there is a * above, it indicates to unpackage it
[11, 22, 33] -----> 11, 22, 33
When the dictionary is passed as a real parameter, if there are two first *, it indicates to unpackage it
Meaning: {"aa": 100, "bb": 200} ----> aa = 100, bb = 200

14. variable data types: List and Dictionary
Unchangeable data types: Numbers, strings, and tuples
The keys in the dictionary can only be of an unchangeable type.

15. The address of a = a + a changes
The address of a + = a does not change.

16. Calling a function is the process of stack and exit.

17. When two functions have the same name, the latter will automatically overwrite the former, but no error will be reported.

18. OS. listdir ("directory") obtain the names of all 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) # Can str be iterated?
True
>>> Isinstance ([1, 2, 3], Iterable) # Whether list can be iterated
True
>>> Isinstance (123, Iterable) # Whether the integer can be iterated
False
The object that can be called by the next () function and continuously return the next value is called the Iterator: Iterator
You can use isinstance () to determine whether an object is an Iterator object:
>>> From collections import Iterator
>>> Isinstance (x for x in range (10), Iterator)
True
>>> Isinstance ([], Iterator)
False

21. list generator: [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 arrangement:
[M + n for m in 'abc' for n in 'xyz']
['Ax ', 'ay', 'az', 'bx ', 'by', 'bz', 'cx', 'cy ', 'cz']

Converts all strings in a list to 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. functions can also be assigned to variables.
>>> F = abs
>>> F (-10)
10

24. pass in a function as a parameter. Such a function is called a high-order function. 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 * 20 for x in range (GROUP_START, GROUP_END + 1)]
Pool = Pool ()
Pool. map (main, groups)

26. =. Check whether the content is the same
Is. determines if it points to the same address and space.

27. _ xx _ indicates the function provided by the system.
_ Class name__ private property

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.