Python while loop using instance, pythonwhile

Source: Internet
Author: User
Tags python list

Python while loop using instance, pythonwhile

A while LOOP is one of the loop structures in Python. The while LOOP continues until the expression changes to false. The expression is a logical expression and must return a true or false value. This article introduces Python while LOOP usage to coders. If you need a friend, please refer to this article.

A loop is a structure that causes the first program to repeat for a certain number of times. The repeat and loop condition remains the same. When the condition changes to false, the loop ends and the program control is passed to the subsequent statement loop.

 

While loop:

A while LOOP is one of the loop structures in Python. The while LOOP continues until the expression changes to false. It is a logical expression and must return a value of true or false.
The syntax of the while loop is:

while expression:   statement(s)

Here, we first calculate the expression statement. If the expression is true, the block is declared to be executed repeatedly until the expression changes to false. Otherwise, the block after the next statement is executed.
Note: In Python, all reports with the same number of programming structures after spaces with indentions are considered part of a single code block. Python uses indentation as its statement grouping method.
For example:

#!/usr/bin/pythoncount = 0while (count < 9):   print 'The count is:', count   count = count + 1print "Good bye!"

This produces 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: 7The count is: 8Good bye!

Until the count is no longer less than 9 blocks, print and incremental statements are combined and executed repeatedly. In each iteration, the current value of the index count is displayed, and then 1 is increased.

 

Infinite Loop:

When using a while loop, you must use it with caution because this situation never solves the possibility of a false value. This will lead to a loop that will never end. This loop is called an infinite loop.

An infinite loop may occur when the Client/Server programming useful servers need to run continuously, so that the client program can communicate with it and, if necessary, when necessary.
For example:

#!/usr/bin/pythonvar = 1while var == 1 :  # This constructs an infinite loop   num = raw_input("Enter a number  :")   print "You entered: ", numprint "Good bye!"

This produces the following results:

Enter a number  :20You entered:  20Enter a number  :29You entered:  29Enter a number  :3You entered:  3Enter a number between :Traceback (most recent call last):  File "test.py", line 5, innum = raw_input("Enter a number :")KeyboardInterrupt

The above example will be in the infite loop, you will need to use Ctrl + C program.

Single statement group: similar to the if statement syntax. if your clause contains only one separate statement at the same time, it can be placed in the same row,
Here is the syntax of a row and a clause:

while expression : statement

 

Address: http://www.manongjc.com/article/970.html

Python reading:

Share official python2.4.1

Python2.4.2

Python development-IDE (integrated development environment)

Python list. cmp () function usage and Examples

Python features

Python development environment and download and Installation

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.