Python Learning Diary

Source: Internet
Author: User

1) range () control for Loop, Len () returns the number of objects

A='asfefqfq' for in range (0,len (a), 2):    Print a[ b

How do I get it out of line output (Python2 directly in a[b] after adding a comma)

2) Enumerate () can get subscripts and elements from the loop

A='asfefqfq' for in Enumerate (a):    Print (x, y),

3) Zip () extracts one element from a sequence of equal lengths and then makes up a new sequence

A='abc'b={1,2,3} for in Zip (b):     Print C,

Use the * to untie C, almost a restore operation (A1,A2) =zip (*c)

4) Loop Object

A loop is an object with a next () method, for example, when a for loop is invoked, the next () method is called until Stopiteration appears, the For loop is accepted, the for loop ends, and the call is stopped.

 for  in open ("log.txt","R"):    Print Line

5) iterators

There is also an object between the loop object and the For loop call, which needs to be performed with an iterator (iterator)

6) Generator

The generator (generator) is a user-defined loop object that, when writing and defining a function, converts return to yield, and each time it is removed using this method or loop, it starts from the point where it was last paused.

def   KKK ():    a=1    yield  a    =2    b=3    yield A * b     yield   KKK ()    :print x

Further understanding of generator expressions:

g= (x for X in range (4))
7) Table Inference

Table inference is a way to quickly generate a table (list comprehension)

Q=[] for in range (5):    q.append (x**2)print Q
X1=[1,3,5]y1=[9,12,13]l=[x**2 for (x, y) in zip (x1,y1) if Y>10]print L
Mechanism to be understood? _?

Python Learning Diary

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.