Python Basics 7-Cycle of Process Control

Source: Internet
Author: User

Cycle:
The execution of a piece of code repeats n times until a certain condition is met.
In order to stop repeating execution at the appropriate time, the program needs to have a condition that satisfies the stop loop.
There are three loops in Python (only two of them are in essence):
While loop
For loop
Nested loops

Break #完全结束一个循环本身
Continue #停止当前循环体的执行 (equivalent to ignoring the statement behind continue) to begin the next loop body;

While Loop #表达式为true, the loop body is executed, and the number of expressions that return a Boolean value is always evaluated more than the loop body.
Format:
While returns an expression of a Boolean value:
Statements to be executed

---------------------------------
#coding =utf8

num = 0
while (Num < 8):
Print num
num = num + 1

print ' over '
---------------------------------
#coding =utf8
#遇到2中断整个循环
num = 0
while (Num < 8):
num = num + 1
if num = = 2:
Break
Print num

print ' over '

---------------------------------
#coding =utf8
#遇到2重新开始下一次循环
num = 0
while (Num < 8):
num = num + 1
if num = = 2:
Continue
Print num

print ' over '

---------------------------------
For loop #Python for loop Unlike other languages, Python's for loop is primarily used to traverse sequences and strings.
Format 1:
For variable in sequence/string:
EXECUTE statement
---------------------------------
List = [1,3,5, ' TT ', ' 999 ']
#每次从序列 [1,3,5, ' TT ', ' 999 '] to take out an element and assign the element to X
For x in list:
Print X
---------------------------------
#内置函数range () #新建一个序列. The elements of this sequence are integers, and 0 begins with the next element 1 greater than the previous one, until the upper limit written in the function-1
For x in range (10):
Print x #输出0
---------------------------------
IDX = Range (10)
If as a judgment, must have to judge with the object to be judged, so must first declare a, otherwise error
If a in IDX:
Print A * * 20

For is to assign the value of each loop traversal to a, and then use a
For a in IDX:
Print A * * 20

Python Basics 7-Cycle of Process Control

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.