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.