For loop control statements in Python

Source: Internet
Author: User
Tags terminates

Http://www.cnblogs.com/way_testlife/archive/2010/06/14/1758276.html

# First: Calculate the prime number between 50 and 100.

Import math
For I in range (50,100 + 1 ):
For J in range (2, INT (math. SQRT (I) + 1 ):
If I % J = 0:
Break
Else:
Print I

 

# Second: place the else in the same indentation as if.

Import math
For 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 math
For 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

 

### Idea ###

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 <object set>:

... If <condition>:

... Break

... If <condition>:

... Continue

... <Other statements>

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

 

Original post address (LZ is my): http://tieba.baidu.com/F? Kz= 798217685

Http://www.cnblogs.com/way_testlife/archive/2010/06/14/1758276.html

# First: Calculate the prime number between 50 and 100.

Import math
For I in range (50,100 + 1 ):
For J in range (2, INT (math. SQRT (I) + 1 ):
If I % J = 0:
Break
Else:
Print I

 

# Second: place the else in the same indentation as if.

Import math
For 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 math
For 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

 

### Idea ###

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 <object set>:

... If <condition>:

... Break

... If <condition>:

... Continue

... <Other statements>

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

 

Original post address (LZ is my): http://tieba.baidu.com/F? Kz= 798217685

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.