This article mainly introduces the usage of for loop control statements in Python, and analyzes in detail the principle and related usage skills of for loop statements, for more information about how to use the for loop control statement in Python, see the example in this article. Share it with you for your reference. The specific analysis is as follows:
First: calculate the prime number between 50-100.
import mathfor i in range(50, 100 + 1): for j in range(2, int(math.sqrt(i)) + 1): if i % j == 0: break else: print i
The output is as follows:
53596167717379838997
Second, place the else position in the same indentation as if.
import mathfor i in range(50, 100 + 1): for j in range(2, int(math.sqrt(i)) + 1): if i % j == 0: break else: print i
Third: add a break statement after else.
import mathfor i in range(50, 100 + 1): for j in range(2, int(math.sqrt(i)) + 1): if i % j == 0: break else: print i break
Note:
A for statement is a loop control statement in python. It can be used to traverse an object and has an optional else block that is used to process the break statements contained in the for statement.
If the for loop is not terminated by break, the statement in the else block is executed.
Break terminates the for loop when needed
Continue skips the statement behind it and starts the next loop.
The format of the for statement is as follows:
>>> For <> in <对象集合> :
... If <条件> :
... Break
... If <条件> :
... Continue
... <其他语句>
... Else:
... <>
...
About the first program
Here, I will explain why to import the math module: import the math module to the developer.
If you import the math module and then open the I, you can reduce the number of operations.
Determines whether a number is a prime number. You only need to perform the following operations on it:
Divide the number n and the cycle with the square of the 2 to the n
If all integers in this interval cannot divide n, n is the prime number.
In this way, the time consumed for the operation between the square that is greater than n and the square that is less than n is saved.
Second, let me explain the '+ 1 ':
Int (math. sqrt (I) outputs the largest integer smaller than I's square.
For example, math. sqrt (51) results are larger than 7, while int (math. sqrt (51) outputs 7
In addition, in range (m, n), the range () function generates a list of integers from m to n-1. Therefore, you must '+ 1' to complete the operation.
By the way, the range () function.
Range ([start,] stop [, step])
# Start (optional)
# Stop Termination count, if range has only one parameter x, a list of integers containing 0 to X-1 is generated
# Step (optional)
Second program
The else line is wrong. if else is placed in that place, once a certain number cannot be divided into its own number, it will output I, and find a number whose division is equal to 0. In this way, this number is output continuously.
For example, if I = 77, it is not a prime number, but it will output 77 consecutive times. do you understand?
However, you just don't understand how else runs when it and if are in the same indent.
You have explained it in detail, but you can't say it in a word.
Moreover, I must think that drawing is a very good way to understand the loop.
I hope this article will help you with Python programming.