Python functions Advanced Features

Source: Internet
Author: User
Tags for in range generator

slices(intercept element, can operate list,tuple,string)
L = List (range)  #  [0,1,2,3,...,]l[0:10] = l[:10]  #  intercept the first 10 numbers [0,1,2,..., 9 ]# l[-10:  #  10 numbers after intercept    [90,91,92,...,]l[10:20]  #  intercept elements of 10-20 positions [ 10,11,12,13,...,]l[:10:2]  #  first 10 numbers, every two take one [0,2,4,6,8]L[::5]  # all numbers, fetch 1 per 5 [0,5,10,...,] l[:]  #  Copy the original array
iterative for...in ...
    • Iterative Dict
D = {'a'b'c': 3}  #  By default, 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 at the same time, you can use for K and V in D.items ()
    • Iteration List
 for  in Enumerate (["a""b""C"  ]):    Print(i, value)  #  Iteration list needs to use the Enumerate function #  0 A#  1 b#  2 C
List-generated
List (range (1,11))#generate [,..,][X*x forXinchRange (1,11)]#build [1*1,2*2,3*3,..., 10*10]#list Generation and if-judging[X*x forXinchRange (1,11)ifx%2 ==0]#build [2*2,4*4,6*6,8*8,10*10]#double-layer loop generation full array[X+y forXinch "ABC"  forYinch "XYZ"]#generate [' AX ', ' AY ', ' AZ ', ' BX ', ' by ', ' BZ ', ' CX ', ' CY ', ' CZ ']
Generator: Genterator (side loop calculation, if a function contains the yield keyword, then the function is a generator)
    • The list generation [] should be ()
 for  in range (1,10)#  Call Method:# each time you call the next () method to get the element, you can only get one for   theNext () method    print(n) is constantly called inside the For loop
    • Using the yield keyword
def fib (max):     = 0, 0, 1 while     n < Max:        yield  b        = b, A + b        = n + 1
   return'done'
iterators
    • An object that can act directly on a for loop iterable
 from Import iterableisinstance ([], iterable)   # True use Isinstance () to determine whether the Iterable object
    • An object that can be called by next () and continually returns the next value Iterator
 from Import   for in range (Iterator)    # use Isinstance () to determine if the object is Iterator, The iterator object is an inert evaluation and is evaluated only when needed

Python functions Advanced Features

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.