While loop in a For loop statement

Source: Internet
Author: User

While loop

While loop while condition: if the condition is true, continue the loop if the condition is false, stop the loop

Loop means to let the program repeatedly execute some statements, Whiler Loop is one of the loop structure, when the loop is not known in advance how many times, it is necessary to use the while loop

In Python programming, a while statement is used to loop the execution of a program, which, under certain conditions, loops through a program to handle the same tasks that require repeated processing. Its basic form is

While judging condition:

Execute statement ...

This piece must pay attention to the indentation

The execution statement can be a single statement or a block of statements. The judging condition can be any expression, and any value other than 0, or non-null (NULL), is true.

The loop ends when the condition false false is judged.

 loop 1--10 integer  kaixin= 1falag = truewhile Falag:print (Kaishi) if Kaishi = = 10:falag = False Kaishi + = 1d:\python3.5\python.exe d:/untitled/www.py1 2345678910Process finished with exit code 0 
while statement There are two other important command Continue,break to skip the loop, continue to skip the cycle, break is used to exit the loop, in addition, "judging condition" can also be a constant value, indicating that the loop must be set up, the following:
Loop 1 --10  does not include an integer of 7
A = 1while true:     If the condition is true down execute if    a = = 7:   If the condition does not meet 7 o'clock continue to execute        A + = 1    hand increase a continue out of   this loop    print (a)    If a = =:           break     #跳出循环 The following code no longer executes    a + = 1      D:\python3.5\python.exe d:/untitled/ Www.py1234568910Process finished with exit code 0
Another way:
Xin = 1while Xin <:    if Xin = = 3:        print (xin)    else:        print (xin)    xin + = 1d:\python3.5\python. EXE D:/untitled/www.py12345678910process finished with exit code 0

In Python, for ... else means that the statement in for is no different from normal, while the statement in else is executed when the loop is executed normally (that is, for not breaking out by break), while ... else is the same.

Implement user Login three chance q = 0                  initial value while Q < 3:        if Q does not meet 3 o'clock, execute user    = input (' username: ') to   enter    pwd = input (' Pas Sword: ')    Please enter the password if user    = = ' Xin ' and pwd = = ' 123 ':   If you enter the username and password        print (' yes ') break out of the   current loop The following code no longer executes    else:        otherwise        print (' Rey again ')          q + = 1      D:\python3.5\python.exe d:/untitled/ Www.pyusername:ewqpassword:123rey Againusername:ewqpassword:123rey againusername:xinpassword:123yesprocess Finished with exit code 0

1----100 and a = 0q = 1while Q < b:    A + = q    q + 1print (a) D:\python3.5\python.exe D:/untitled/lianxi.py4950proce SS finished with exit code 01--100 all Odd i = 1while i <:    if I% 2 = = 1:        print (i)    i +=1d:\python3.5\pyth On.exe d:/untitled/lianxi.py1357 ..... 959799Process finished with exit code---100 of all even a = 0while True:    print (a)    a + = 2    If a >:        break D:\python3.5\python.exe d:/untitled/lianxi.py02468 ......... 9698100Process finished with exit code 0 Loop 1----15 does not include 7,10 integer i = 0    # The last value of the initial value of the  first value of the loop while i<=15:   #条件是真的就 Execute down    i + = 1       #当初始值不满足条件时就加一    if i = = 7:   #如果条件满足时        Continue   # just stop the loop and continue with the code        i + = 1    #当初始值不满足条件时就加一    If I ==10:   #如果条件满足时        continue  #就停止循环 continue with the following code    print (i) D:\python3.5\ Python.exe D:/untitled/lianxi.py12345689111213141516process finished with exit code 0

2-3+4 ... 100 of and

Q = 0    #初始值 # a = 2    #循环初始值 # while a<=: #条件是不是真的 #     C = a% 2    #判断结果是不是哦数 #     If c = = 0:    #判断是不是是偶数 #< C6/>q + = a  #偶数就相加 #     Else:     #否则就是奇数 #         Q-= a   #奇数就相减 #     A + = 1        # hand Increase # print (q) D:\python3.5\ Python.exe D:/untitled/lianxi.py-51process finished with exit code 0

a Python for loop can traverse any sequence of items, such as a list or a string. 
string q = ' xin ' for i in Q:    print (i) D:\python3.5\python.exe d:/untitled/lianxi.pyxinprocess finished with exit code 0 list Q = [1,2,3,4]for i in Q:    print (i) D:\python3.5\python.exe d:/untitled/lianxi.py1234process finished with exit code 0
Iterating through the sequence index

Another way to perform a loop is through an index, as in the following example:

Li = [' Alex ', ' rice ', ' rain '] for I in range (Len (LI)):     print (i) Result 0,1,2

  1-2 +3 ... 99 and  i = 0a = 1for A in range (1,99):    If a% 2 = = 1:        i + = a    else:        i-= a    A + = 1print (i) D:\python3.5 \python.exe D:/untitled/lianxi.py-49process finished with exit code 0

Range () The meaning of the scope

   Enumrate add an ordinal to an iterative object: Li = [' Alex ', ' rice ', ' rain ']for k,v in Enumerate (li,100):    print (k,v) D:\python3.5\python.exe D :/untitled/lianxi.py100 alex101 rice102 rainprocess finished with exit code 0






While loop in a For loop statement

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.