This article example describes the use of Python basics of the loop statement. Shared for your reference, as follows:
while loop
The general form of the while statement in Python:
while Determines the condition:
statements
Again, you need to pay attention to colons and indentation. Also, there are no do...while loops in Python.
The following example uses while to calculate the sum of 1 through 100:
#!/usr/bin/env python3 n = 100 sum = 0 counter = 1 while counter <= n: sum = sum + counter counter += 1 print("Sum of 1 until %d: %d" % (n,sum))
The results of the implementation are as follows:
Sum of 1 until 100: 5050
for statement
Python for loops can iterate over any sequence of items, such as a list or a string.The general format of a for loop is as follows:
for <variable> in <sequence>:
<statements>
else:
<statements>
Python loop loop example:
>>> languages = ["C", "C++", "Perl", "Python"] >>> for x in languages: ... print x ... C C++ Perl Python >>>
Example of the followingfor
The example uses thebreak
statement.break
statement is used to jump out of the current loop body:
#!/usr/bin/env python3 edibles = ["ham", "spam","eggs","nuts"] for food in edibles: if food == "spam": print("No more spam please!") break print("Great, delicious " + food) else: print("I am so glad: No spam!") print("Finally, I finished stuffing myself")
After executing the script, it will jump out of the loop when it reaches "spam":
Great, delicious ham
No more spam please!
Finally, I finished stuffing myself
range() function
If you need to iterate through a sequence of numbers, you can use the built-in range() function. It will generate the sequence of numbers, e.g.
>>> for i in range(5): ... print(i) ... 0 1 2 3 4
You can also use range to specify the value of the interval:
>>> for i in range(5,9) : print(i) 5 6 7 8 >>>
It is also possible to make range start with a specified number and specify a different increment (it can even be a negative number; sometimes this is called a 'step').
>>> for i in range(0, 10, 3) : print(i) 0 3 6 9 >>> negative number: >>> for i in range(-10, -100, -30) : print(i) -10 -40 -70 >>>
can be combined withrange()
cap (a poem)len()
function to traverse the index of a sequence, as follows.
>>> a = ['Mary', 'had', 'a', 'little', 'lamb'] >>> for i in range(len(a)): ... print(i, a[i]) ... 0 Mary 1 had 2 a 3 little 4 lamb
It is also possible to userange()
function to create a list:
>>> list(range(5)) [0, 1, 2, 3, 4] >>>
break and continue statements and else clauses in loops
The break statement allows you to jump out of the body of for and while loops. If you terminate from a for or while loop, any corresponding loop else blocks will not be executed.
The continue statement is used to tell Python to skip the remaining statements in the current loop block and continue with the next round of the loop.
A loop statement can have an else clause; it is executed when the loop is terminated by exhausting the list (in a for loop) or when the condition becomes false (in a while loop), but not when the loop is terminated by break. The following is an example of a loop to find a prime number.
>>> for n in range(2, 10): ... for x in range(2, n): ... if n % x == 0: ... print(n, 'equals', x, '*', n//x) ... break ... else: ... # No element found in loop ... print(n, 'is a prime number') ... 2 is a prime number 3 is a prime number 4 equals 2 * 2 5 is a prime number 6 equals 2 * 3 7 is a prime number 8 equals 2 * 4 9 equals 3 * 3
pass statement
The pass statement does nothing. It is used only when a statement is syntactically required, but the program does not need to do anything. For example.
>>> while True: ... pass # Wait for keyboard interrupt (Ctrl+C)
Smallest class.
>>> class MyEmptyClass: ... pass
For those interested in Python-related content, check out this site's feature: theSummary of Python function usage tips》、《Python Object-Oriented Programming Introductory and Advanced Tutorials》、《Python Data Structures and Algorithms Tutorial》、《Summary of Python string manipulation techniques》、《Summary of Python coding manipulation techniquesand thePython introductory and advanced classic tutorials》
I hope that what I have said in this article will help you in Python programming.