Python Learning summary 1:for loop control statement

Source: Internet
Author: User

use a list to determine the range of a for loop
>>> x = [0,1,2,3,4] for in x:    print  i,    1 2 3 4

Loop A String

' python '  for inch x:     Print i,    p y t h o n

Tuple for Loop

>>> x = [('http','HTTPS'),('Java','python')]>>> for(A, B)inchx:Print(A, b) ('http','HTTPS')('Java','python')

iterators

#file iterators, best practices for reading files>>> forLineinchOpen'Test.txt'):    Printline.upper () hello,word!#Dictionary iterators>>> testdict = {'name':'Ethon','Aender':'male'}>>> forKeyinchtestdict:PrintKey +':'+Testdict[key] Aender:malename:ethon

iterative Protocols : Some functions can be run on objects that support an iterative protocol

>>> testlist = [9,8,7,6,5]>>>PrintSorted (testlist) [5, 6, 7, 8, 9]>>>Printsum (testlist)35>>>PrintAny (testlist) True>>>PrintList (Open ('Test.txt'))['hello,word!']>>>PrintTuple (Open ('Test.txt'))('hello,word!',)
#tuples, list constructors, and joins can operate on objects that support iterative protocols>>>Print('--'). Join (Open ('Test.txt')) hello,word!

Use the range function to produce a range of loops

 for  in range (5):    print'was the currentvalue'       is The current value  is The current value  is The current value  is The current value  is the current value
Zip Zip : Use the zip function to merge two lists into a list of tuples.
>>> L1 = [1,3,5,7]>>> L2 = [2,4,6,8]print  zip (l1,l2) [(1, 2), (3, 4 ), (5, 6), (7, 8)]for in  Zip (l1,l2):    Print (A, b)    ( 1, 2)(3, 4)(5, 6) (7, 8)

Python Learning summary 1:for loop control statement

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.