What is the Python loop nesting? (code example)

Source: Internet
Author: User
Presumably everyone is just touching the language of Python, today this article will help you to understand python loop nestingThis knowledge point. Specifically, we introduce two loops, the for loop and the while loop, and the Python language allows you to embed another loop inside a loop body. In this article I will explain some of the python Looping nested instancesTo help understand, I hope this article can bring you some help, so that you can learn python less detours.

One, Python for loop nesting syntax:

For Iterating_var in sequence: for   iterating_var in sequence:      statements (s)   statements (s)

Second, 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.

Only the description and the structure may not actually work, and then I'll cite an example to deepen the understanding of this knowledge point:

#!/usr/bin/python#-*-Coding:utf-8-*-i = 2while (i <):   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 print "Good bye!"

The results of the above instance output are as follows:

2 is the prime number 3 is the prime number 5 is the prime number 7 is the prime number 11 is the prime number 13 is the prime number 17 is the prime number 19 is the prime number 23 is the prime number 29 is the prime number 31 is the prime number 37 is the Primes 41 is the prime number 43 is the prime number 47 is the prime number 53 is prime number 59 79 is prime number 83 is prime number 89 is prime number 97 is prime good bye!

This article explains the knowledge points of Python looping nesting and demonstrates some of its examples. Looping nesting is a very useful point of knowledge and you will find yourself able to use them in many situations. Hopefully this article will give you a little help in learning about Python.

For more information, please visit the PHP Chinese Web Python tutorial section.

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.