The looping structure in Python

Source: Internet
Author: User

Python mainly has a for loop and a while loop in two forms of loop structure, multiple loops can be nested use, and also often and select structure nested use. While loops are generally used in situations where the number of loops is difficult to determine in advance, and of course can be used to determine the number of cycles, and for loops are generally used in situations where the number of loops can be determined in advance, especially when enumerating or iterating over elements in a sequence or iteration of an object. For a loop structure with an ELSE clause, the statement in the else structure is executed if the loop is terminated because the conditional expression is not true or the end of the sequence traversal ends, and the statement in else is not executed if the loop ends prematurely because the break statement was executed. The complete grammatical forms of the two loop structures are:

While conditional expression:

Loop body

[Else:

ELSE clause code block]

And

For value in sequence or iteration object:

Loop body

[Else:

ELSE clause code block]

Where the Else clause in the square is either not, or can have. The following code uses the loop structure to traverse and output all the elements in the list.

A_list = [' A ', ' B ', ' www.2.qixoo.com ', ' z ', ' example ']

For I, V in Enumerate (a_list):

Print (' list ', i+1, ' elements are: ', V)

The following code is used to output all integers between 1~100 that can be divisible by 7 but not divisible by 5.

For I in range (1, 101):

If I%7==0 and i%5!=0:

Print (i)

The following code prints a 99 multiplication table using a nested looping structure.

For I in range (1, 10):

For j in range (1, i+1):

Print (' {0}*{1}={2} '. Format (i,j,i*j), end= ')

Print () #打印空行

The following code shows the loop structure with the ELSE clause, which is used to calculate the result of the 1+2+3+...+99+100.

s = 0

For I in range (1, 101): #不包括101

s + = i

Else

Print (s)

The following code implements the same functionality using the while loop:

s = i = 0

While I <= 100:

s + = i

i + = 1

Else

Print (s)

Of course, the above two code is only to demonstrate the use of the loop structure, where the ELSE clause is not actually necessary, the end of the loop to directly output the result. Also, if you just want to calculate the value of the 1+2+3+...+99+100, use the built-in function sum () and range () directly, for example:

>>> sum (Range (1,101))

Manuscripts: Custom Development www1.qixoo.com

The looping structure in Python

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.