Some easy-to-forget points in the Python Learning Manual (4-7 parts)

Source: Internet
Author: User

Have time system read the Python study manual and record some easy-to-forget points

1.python function High Polymerization low coupling
1) Use parameters for input and return for output
2) Use global variables only when it is really necessary
3) do not change the parameters of the mutable type unless the caller wishes to do so
4) Each function should have a single, unified goal
5) Each function should be relatively small
6) Avoid changing variables directly in another file

2. Recursive processing of arbitrary structures
>>> a=[3,[2,[2,3,4],2],1,[1,5,[1,3,3]]
>>> def sumtree (L):
... tot=0
... for x in L:
... if not isinstance (x,list):
... tot+=x
.. else:
... tot+=sumtree (x)
.. return tot
...
>>> Sumtree (a)
30
>>>

3.lamdba,map,filter,reduce usage
>>> map ((lambda x:x+3), [1,2,3,4,5,6])
[4, 5, 6, 7, 8, 9]
>>> map ((lambda x,y:x+y), [1,2,3,4,5,6],[2,3,4,5,6,7])
[3, 5, 7, 9, 11, 13]
>>>
>>> Filter ((lambda x:x%2==0), [1,2,3,4,5,6])
[2, 4, 6]
>>> Reduce ((lambda x,y:x+y), [1,2,3,4,5,6])
21st
>>> Reduce ((lambda x,y:x*y), [1,2,3,4,5,6])
720
>>>

>>> Filter ((lambda x:x%2==0), [1,2,3,4,5,6])
[2, 4, 6]
>>> Reduce ((lambda x,y:x+y), [1,2,3,4,5,6])
21st
>>> Reduce ((lambda x,y:x*y), [1,2,3,4,5,6])
720
>>>

Some easy-to-forget points in the Python Learning Manual (4-7 parts)

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.