Python core programming Reading Pen 6: conditions and loops

Source: Internet
Author: User
Tags iterable

Eighth chapter conditions and circulation

First, if

Conditional expressions in Python: very wonderful!!!

smaller = (x < y and [x] or [y]) [0]

Or:

smaller = x if x < y else y

Second, while

Third, for

1 Iteration by Sequence item:

For Eachname in NameList:
2 Iteration by index:

>>>for Nameindex in range (len (namelist)):
... print "Liu,", Namelist[nameindex]
3 using Sequence items and index iterations:

>>> for I, Eachlee in Enumerate (namelist):
... print "%d%s Lee"% (i+1, Eachlee)

Iv. Break and Continue

Five, pass

Python does not use the traditional curly braces to mark blocks of code, and sometimes there is a syntax for code, and Python does not have the corresponding empty curly braces or semicolons (;) to represent the C language "do nothing", if you do not need to write any statement in the block, the interpreter will prompt you syntax Error. So Python provides the pass statement, which does nothing-that is, NOP, (no operation, no action)

Six, iterators iterators

Using iterators:

  sequence :

For i in SEQ:
Do_something_to (i)

In fact, it works like this:
Fetch = ITER (seq)
While True:
Try
i = Fetch.next ()
Except stopiteration:
Break
Do_something_to (i)
  
  Dictionary :

The dictionary iterator iterates through its keys (keys). The statement for Eachkey in Mydict.keys () can be abbreviated as for Eachkey in Mydict;

In addition, Python has introduced three new built-in dictionary methods to define iterations: Mydict. Iterkeys () (via the keys iteration), Mydict. itervalues () (Through the values iteration), and mydicit. Iteritems () (Iteration by Key/value).

  File:

The iterator generated by the file object automatically calls the ReadLine () method. This allows the loop to access all lines of the text file. The programmer can replace the for Eachline in Myfile.readlines () with a simpler for-Eachline in MyFile:

Seven, list analysis

In Python 2.0, we added list parsing, which revolutionized the language, providing users with a powerful tool to create a list of specific content with just one line of code!!!

Basic syntax:

[Expr for Iter_var in iterable]

[Expr for Iter_var in iterable if cond_expr]

Practical Examples:

>>> [x * * 2 for X in range (6)]
[0, 1, 4, 9, 16, 25]

>>> [x for x in seq if x% 2]
[11, 9, 9, 9, 23, 9, 7, 11]

>>> [(x+1,y+1) for x in range (3) for Y in range (5)]
[(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (2, 1), (2, 2), (2,
3), (2, 4), (2, 5), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5)]

Viii. Generator Expression

A builder expression is an extension of list resolution

Basic syntax:

(Expr for Iter_var in iterable if cond_expr)

Practical Examples:

>>>x_product_pairs = ((i, j) for I in rows for J in Cols ())
>>>type (X_product_pairs)

<type ' Generator ' >

>>>for I in X_product_pairs:

... print I

...

(0,0)

(0,1)

...

Python core programming Reading Pen 6: conditions and loops

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.