Example of a For loop control statement usage in Python

Source: Internet
Author: User
Tags range in python

This example describes the use of the For loop control statement in Python. Share to everyone for your reference. The specific analysis is as follows:

First: Find the prime number between 50-100

?

1 2 3 4 5 6 7 Import math for I in range (1): With J in range (2, int (math.sqrt (i)) + 1): If I% j = = 0:break Else:print I

The output is as follows:

?

1 2 3 4 5 6 7 8 9 10 53 59 61 67 71 73 79 83 89 97

Second: Put else's position in the same indent as if.

?

1 2 3 4 5 6 7 Import math for I in range (1): With J in range (2, int (math.sqrt (i)) + 1): If I% j = = 0:break Else:print I

Third: Add a break statement after else.

?

1 2 3 4 5 6 7 8 Import math for I in range (1): With J in range (2, int (math.sqrt (i)) + 1): If I% j = = 0:break Else:print I brea K

Description

The For statement is a circular control statement in Python. Can be used to traverse an object and also have an optional else block attached, which is used primarily to handle the break statements contained in a for statement.

If the For loop is not terminated by a break, the statement in the else block is executed.

Break terminates the For loop when needed

Continue skips the following statement and starts the next round of loops.

The For statement is formatted as follows:

>>>for <> in < object collection:

.. if < conditions:

.. break

.. if < conditions:

... continue

... < other statements >

. else:

... <>

...

About the first program

Here, let me explain why the math module was imported: The Math module was imported to root.

If you import the math module and then root out the I, you can reduce the number of operations.

Ask whether a number is prime. You just have to do this:

Divide the number n, and the loop with the open square of 2 to this n.

If all integers in this interval cannot divide n, then n is prime.

This saves the time when the operation ' greater than n ' is less than n '.

Second, let me explain the ' +1 ':

Int (math.sqrt (i)) outputs the largest integer that is smaller than the open square of I.

For example: Math.sqrt (51) result is a bit larger than 7, and Int (MATH.SQRT (51)) outputs 7

And here in range (M, n), the range () function produces a list of integers from M to n-1, and thus requires ' +1 ' to make the operation complete.

By the way, the range () function.

Range ([Start,] stop [, step])

# Start optional parameter, starting number

#stop the number of terminations, if range has only one parameter x, then produces a list of integers containing 0 to x-1

#step Optional parameters, step size

Second Program

else that line is wrong, if else is placed in that place, once there is a number of encounters can not be divided into their own number, it will output I, straight to find a division is equal to 0 of the number. That will output the number sequentially.

For example: i = 77, he is not prime, but will also output 5 times in a row 77, understand?

It's just that I don't understand how it works when else is in the same indent as if.

You explain it in detail, and it's not too much to describe it with ' enlightened '.

Moreover, I must think that drawing is a very good way to understand the cycle.

I hope this article will help you with your Python programming.

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.