The while loop is one of the looping structures in Python. The while loop continues until the expression becomes false. The expression is a logical expression, must return a value of TRUE or FALSE, this article to the code farm introduction Python While loop use method, need friends can look at this article.
A loop is a structure that causes the first program to repeat a certain number of times. The same is true for repeated cyclic conditions. When the condition becomes false, the loop ends and the control of the program is passed to the following statement loop.
While loop:
The while loop is one of the looping structures in Python. The while loop continues until the expression becomes false. Expression is a logical expression and must return a value of TRUE or False
The syntax for the while loop is:
While expression: statement (s)
Here, the expression statement is evaluated first. If the expression is true, then the declaration block executes repeatedly until the expression becomes false. Otherwise, the statement block after the next statement is executed.
Note: In Python, all indented characters spaces the same number of programming structures as the report, which is considered to be part of a single code block. Python uses indentation as a method of grouping its statements.
For example:
#!/usr/bin/python count = 0while (Count < 9): print ' The Count is: ', count count = count + 1 print "Good by e! "
This will produce the following results:
The Count is:0the Count is:1the Count is:2the Count is:3the Count is:4the Count is:5the count is:6the Count is:7th E Count Is:8good bye!
Until the count is no longer less than 9 blocks, the print and increment statements are composed and executed repeatedly. For each iteration, the index count current value is displayed and then increased by 1.
Infinite loops:
Use a while loop, because this situation never solves the possibility of a false value when you have to use caution. This will lead to a loop that never ends. This cycle is called an infinite loop.
An infinite loop, which may be useful in client/server programming, requires a continuous run of the server so that the client program can communicate with it and when necessary.
For example:
#!/usr/bin/python var = 1while var = = 1: # This constructs a infinite loop num = Raw_input ("Enter a number:")
print "You entered:", num print "Good bye!"
This will produce the following results:
Enter a number:20you entered:20enter a number:29you entered:29enter a number:3you entered:3enter a number between:t Raceback (most recent): File "test.py", line 5, Innum = Raw_input ("Enter a number:") keyboardinterrupt
The above example will be in Infite loop, you will need to use CTRL + C program to.
Single statement group: Similar if statement syntax, if you have a clause that contains only a single statement, it can be placed on the same line,
Here is the syntax for a line and clause:
While Expression:statement
Above this python while loop use of a simple example is the small part of the whole content to share to everyone, I hope to give you a reference, but also hope that we support topic.alibabacloud.com.