Python fourth day advanced features

Source: Internet
Author: User
Tags function definition iterable

Slice operation : How to take the first three elements through a loop? Can be manipulated by slicing

L[0:3], where the first parameter represents the start element of the slice, the second argument is taken to the end of the first element, and the third parameter represents the hop interval at which the value is taken.

The bottom slice: l[-2:] The output is 8,9

Tuple and list have slice manipulation characteristics, string is also a list, so the string can also be sliced, but the result of the operation is a string

iteration: As long as it is an iterative object, whether it is a list or a tuple collection

Iteration of the Dict collection

1.key Iteration

2.value Iteration

3.key and Value iterate together

Determine if an object can iterate? Judging by the iterable type of the collections module

If you want to implement a Java-like subscript loop for a list, Python's built-in enumerate function turns a list into an index-element pair

List-Generated

A line statement generates a list collection

With a two-layer loop, you can create a fully arranged

Generator: continuous extrapolation of subsequent elements in the loop, no need to create a complete list. The mechanism for calculating one side of the loop is called the generator.

There are many ways to create a generator. The first is to create a generator by changing the [] to () of the list-generating type;

1. If one prints out, you can get the next return value of generator with the next () function: Each call to next (g) Calculates the value of the next element of G until the last element of the JI, without more elements, throws a stopiteration error.

2. Direct for loop, because the generator object is iterative.

The second method of generating generator, as long as a function definition contains the yield keyword, is not a normal function. But generator.

Generator is not the same as the execution of a function, the function is executed sequentially, and the generator function is executed each time the next () is called, and the yield statement is returned.

Execution resumes from the last yield statement returned. If you want to get the return value of the generator return statement, you must catch the Stopiteration error, and the return value is contained in the value of stopiteration.

iterator: An object that can be called by the next () function and continuously returns the next value becomes an iterator, iterationlist, str, tuple, generator are iterative iterable,isinstance ([],iterable) is true, but the generator is a iteration object, and List,str,tuple is not a iteration object. Isinstance ([],interation) is false,

List,str,tuple can be changed to Iteration Object Isinstance (ITER ([]), iteration) with the ITER () function as True

Python fourth day advanced features

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.