Python Basic Learning

Source: Internet
Author: User

has been intermittent to study, learned a long time to discover that they are still only the level of entry, so, today, determined to study systematically, and the daily study to make a summary.

    • the difference between *args and **args: meaning 1 or the value of parameter A, args represents the remaining value, and Kwargs represents a pair of pairs of key values after args. The procedure is as follows:

 1  def  Test (A,*args,**kwargs):  2  print   a  3  print   args  4  print   Kwargs  5  Test (1,2,3,d= " 4  , E=5)  6  
    • unpacking Argument Lists : the reverse situation Occurs when the arguments is already in a list or tuple but need to being unpacked for a function call requiring separate PO sitional arguments. For instance, the built-in range () function expects separate start and stop arguments. If They is not available separately, write the function call with the *-operator to unpack the arguments out of a list or Tuple. In the same fashion, dictionaries can deliver keyword arguments with the **-operator.
    • python closure: Simply put, closures are based on different configuration information to get different results. Let's take a look at the professional explanation: Closures (Closure) are short for lexical closures (Lexical Closure) and are functions that reference free variables. This quoted free variable will exist with this function, even if it has left the environment in which it was created. So, there is another argument that closures are entities that are composed of functions and their associated reference environment. It can also be understood that the function and its arguments are also parameters of the other function. For example, the following code:                    
defIndex (func):defPanduan (license):defL90 (a):ifA>90:                Print('1')        defL60 (a):ifA>60:                Print('2') func (a)Pass        iflicense==150:            returnL90eliflicense==100:            returnl60Pass    returnPanduan@indexdefLiu (a):Print(a)PassLiu (license=100) (a=91)

  • iterator (iterator):In Python, A For loop can be used for any type in Python, including lists, Ganso, and so on, in fact, the For loop is available for any "iterator object," which is actually an iterator. An iterator is an object that implements an iterator protocol, and the iterator protocol in Python is that an object with the next method advances to the next result, and Stopiteration is thrown at the end of a series of results. Any such object can be iterated with a for loop or other traversal tool in Python, and the iteration tool will invoke the next method at each iteration and catch the stopiteration exception to determine when to leave. One of the obvious benefits of using iterators is that you only read one piece of data from the object at a time, without causing too much memory overhead. For example, to read the contents of a file row by line, using the ReadLines () method, we can write:
    1  for  in open ("test.txt"). ReadLines ():2     print Line

    Using the file iterator, we can write:

     for  in open ("test.txt"#usefile iterators    Print Line

    This is the simplest and fastest-running notation, and he does not read the file explicitly, but instead uses the iterator to read one row at a time.

    • Generator (constructor): The generator function is associated with the concept of an iterator protocol in Python. In short, a function that contains a yield statement is specifically compiled by the genetic builder. When the function is called, they return a generator object that supports the iterator interface. The function may have a return statement, but its purpose is to yield the value. Unlike normal functions that generate values and exit, the generator functions automatically suspend and pause their execution and state after the values are generated, and his local variables will save state information that will be valid again when the function is restored, that is, we use yeild to generate an iterative sequence directly, and we can wake it up He is still a sequence, and the number of sequential lengths can be executed with the next () function.

Python Basic Learning

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.