Python Exercise 25

Source: Internet
Author: User

The Python language allows you to embed another loop inside a loop body.

Python for loop nesting syntax:

For in sequence: For 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.

The following instance uses the prime number between the nested loop output 2~100:

#!/usr/bin/python#-*-Coding:utf-8-*-I= 2While(I< 100):J= 2 While(J<= (I/J)): If Not(I%J): break J = J + 1 if   (j > I /j)  :print I,  "is prime"  I = I + 1< Span class= "PLN" >print  "good bye!"      
           

Python Practice

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.