Python Advanced Features

Source: Internet
Author: User

Slice: A partial element used to fetch a list or tuple; L = [' Michael ', ' Sarah ', ' Tracy ', ' Bob ', ' Jack ']l[0:3]       // Represents starting at index 0, up to index 3, but excluding index 3, which is index 0,1,2; exactly three elements;l[:3]         //If the first index is 0, it can be omitted; l[ 1:3]       //starting from index 1 to 3-1 end;l[-2:]       //starting from the penultimate element, taking the elements; L[-2: -1]     //start with the penultimate element and take it to the penultimate element; Well, now we're going to do a little bit more of this, and we want more features, such as one for every two, and L = List (range) l[ :10:2]       //starting from index 0, (as can be seen from here, Range (100) refers to 0 to 100-1), take 10-1, and then every two take 1, that is, 0, 2,4,6,8 and strings can also be seen as a list, each element is a character. You can also use slicing operations, ' ABCDEFG ' [0,3]   /' ABC ' ABCDEFG ' [::2]   /' Aceg ' iterations: Dictionaries can also be implemented iteratively, but the dict iteration is key, If you want to iterate over value, you can use for value in D.values (), if you want to iterate both key and value, use for k,v in D.items (), Iteration dictionary value:for N in d.values ():    Print (n) Iteration dictionary key and Value:for k,v in D.items ():     print (k,v)   in the For Loop, It is normal to refer to two variables at the same time: for x, y in [(1.1), (2,4), (3,6)]:    print (x, y) in the output, directly with print(x, y) is OK ~ ~ List generation: When writing list generation, put the elements to be generated x*x to the front, followed by the For loop, you can create the list; [x * x for x in range]]      & nbsp After putting the elements in range (100) to X, generate the list according to X*x; [M + N for m in ' ABC ' for n ' XYZ ']       //Use two-layer loop to generate the full array; generator : Create a list, subject to memory limitations, with limited list capacity. We want to continue to calculate the subsequent elements in the cycle, so that we do not have to create a complete list, thus saving a lot of space, while the loop side of the computation, called the generator: generator; The first method, change a list-generated [] to (); Create a generator ; L = [x * x for x in range]]g = (x * x for x in range (100))Print (Next (g))//0Print (Next (g))//1As we already know, there are several types of data that can be directly applied to a For loop: One is a collection data type, such as LIST,TUPLE,DICT,SET,STR, and so on; one is generator, including the generator and generator function with yield An object that can directly act on a for loop is called an iterative object: iterable; An object that can be called by the next () function and continually returns the next value is called an iterator: iterator; use Isinstance () to determine whether an object is a iterator object The generator is a iterator object, but although List,dict,str is iterable, it is not iterator; turning List,dict,str into iterable can use the ITER () function Note: All objects that can be used for a for loop are iterable types, and all objects that can be used for the next () function are Iteratorlexington, representing a sequence of lazy computations;

Python 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.