Python Loop Nesting

Source: Internet
Author: User
The Python language allows you to embed another loop inside a loop body.

Python for loop nesting syntax:

For Iterating_var in sequence:

For Iterating_var in sequence:

Statements (s)

Statements (s)

Python while loop nesting syntax:

While expression:

While expression:

Statement (s)

Statement (s)

You can embed another loop body in the loop, such as a For loop in the while loop, and conversely, you can embed a while loop in a for loop.

Instance:

1. Remove one from the first list, one at a time from the second list, combined into a new list, with all the combinations in the new list

List1 = [' Zi ', ' Qiang ', ' Xue ', ' Tang ']

List2 = [1, 2]

New_list = []

For M in List1:

For N in List2:

New_list.append ([M, N])

Print New_list

Results:

[[' Zi ', 1], [' Zi ', 2], [' Qiang ', 1], [' Qiang ', ' 2], [' Xue ', 1], [' Xue ', 2], [' Tang ', 1], [' [' Tang '], ' 2] '

2. Remove two from a list to find all combinations

List = [1, 2, 3, 4, 5]

length = Len (List)

For x in range (0, length-1):

For y in range (x+1, length):

Print list[x], list[y]

Results:

1 2

1 3

1 4

1 5

2 3

2 4

2 5

3 4

3 5

4 5

3. The following instance uses the prime number between the nested loop output 2~20:

Analysis: For a number n, if the number from 1 to n * * 0.5 (square root n) is divisible, then n is the number of primes.

3.1 using for to implement

#-*-Coding:utf-8-*-

n = 20

For I in range (1, N):

For j in range (2, int (i**0.5)):

If I% J = = 0:

Break

Else

Print I, ' is prime '

3.2 Using while to implement

#!/usr/bin/python

#-*-Coding:utf-8-*-

i = 2

while (I < 20):

j = 2

while (J <= (i/j)):

If not (I%J): # or write if I% J = = 0, if the meaning of divisible

Break

j = j + 1

if (J > i/j):

Print I, "is prime"

i = i + 1

Print "Good bye!"

Explanation: I% J means I divided by J after the remainder. For numbers, the Boolean value of 0 is false, the other values are true, the not (i% j) means that the condition is set when I j is 0 o'clock, meaning that when the division is divisible, the meaning is divisible.

The result of the above example output:

2 is prime

3 is prime

5 is prime

7 is prime

11 is prime

13 is prime

17 is prime

Good bye!

  • 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.