Python while loop and for loop

Source: Internet
Author: User

I just looked at Python's while and for loop today, so I'm going to write it down:

The while statement is a loop condition statement in Python, while judging the condition:

Pass

Break

For example:

i = 1
sum = 1
While I <= 9:
j = 1
While J <= I:
sum = J * I

Print "%d *%d =%d"% (i, J, Sum),
J + = 1
i + = 1
print "\ n"

While statements can be nested IF statements, you can make various judgments of the statements, and then various execution.

For statement:

The FOR statement and for statements in other languages have changed a bit, in which there is no such thing as C: for (i = 0; i<100; 1++) Like this,

The For statement in this case is only for lists or collections or the meta-ancestors (arrays Ah, dictionaries, etc.), the traversal of them, and then each traversal will be traversed by the character of the characters into a custom variable inside, we can output that variable to test.

For example:

Import time
Name = "Liuguibin"
For tamp in name:
Print "%s"% tamp
Time.sleep (1)

Break and Continue:

Break is used in the while and for statement loops to end the loop, as long as our program reads this break, the loop ends.

Example: i = 0

While I <= 50:

Print "This is a test"

i = i * I

Print "%d"% i

i + = 1

Break

Continue is used to end this cycle and then proceed to the next loop, which means that the loop will still be executed, but only if the loop that meets the continue condition is read.

For example:

For ABC in ABCDEFG:

If ABC = ' C '

Continue

Else

Pass

Print "%s"% ABC

Above, that's it!

Python while loop and for loop

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.