Common Questions and answers for beginners of python, and questions for beginners of python

Source: Internet
Author: User

Common Questions and answers for beginners of python, and questions for beginners of python

1. lambda Functions

The function format is lambda keys: express anonymous function lambda is an expression function that accepts the keys parameter and returns the value of the expression. Therefore, no return or function name is required. It is often used in functions that require key parameters, such as sorted.

2 tuples (), which are identified by commas rather than parentheses.For example, a newbie of an element is often written as (12). In fact, it will be interpreted as a single element 12. The correct syntax should be (12,), followed by a comma.

3. Import modules. For example

Import random
Print random. choice (range (10 ))

And

From random import choice
Print choice (range (10 ))

There is a misunderstanding for beginners. The second method only imports one function without importing the entire module. This is incorrect. The entire module has actually been imported, but the reference of that function is saved. Therefore, the from-import syntax will not bring about performance differences, nor save memory.

4. If you have many modules, such as hundreds of modules, you may want to import them one by one. Is there a simple way?The answer is yes. These modules are organized into a package. In fact, it is to put all modules in a directory, and then add _ init __. py file, python will view it as a package, and the functions in it can be accessed in dotted-attribute mode.

5. A variable object is passed as a reference object, and a variable object is passed as a value.Then what object is variable and what is immutable. All python objects have three attributes: type, identifier, and value. If the value is variable, it is a variable object. If the value is unchangeable, it is an immutable object. Numbers, strings, and tuples are immutable objects, and the rest of the list, Dictionary, class, and class instances are variable objects.

6. The iterator is a container object that implements the iterator protocol.Implement an iterator by yourself. The class must have the _ iter _ () method, which returns an object. This object must have the _ next _ () method and return the StopIteration exception at an appropriate position in the next method. The iterator is not frequently used, so don't worry too much. An alternative method is generator.

Class MyIterator (object): "" docstring for MyIterator "" def _ init _ (self, num): self. num = num def _ iter _ (self): return self; def _ next _ (self): if self. num <= 0: raise StopIteration; self. num-= 1; return self. num; for each in MyIterator (5): print (each);-> result

7 generator. If the yield statement appears in the function, it is converted into a generator. When a yield statement is met, the context is saved and the function is exited.

Note: The generator does not have a return statement.

Def fun2 (num): print ("start generator"); while (num> 0): yield num; num-= 1; a = [each for each in fun2 (5)] print (a);-> result start generator [5, 4, 3, 2, 1]

Errors are inevitable during the learning process. If you have any questions during your reading.

The common questions and answers for beginners of python are all the content shared by the editor. I hope you can give us a reference and support the help house.

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.