Python while looping using instances

Source: Internet
Author: User
Tags python list

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/pythoncount = 0while (Count < 9):   print ' The Count is: ', count   count = count + 1print "Good bye!"

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/pythonvar = 1while var = = 1:  # This constructs a infinite loop   num = Raw_input ("Enter A number  :"   print "You entered:", numprint "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:traceback (most recent call last):  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

Original address: http://www.manongjc.com/article/970.html

Read more about Python:

Share python2.4.1 official

python2.4.2 Share

Python Development-IDE (integrated development environment)

Python list.cmp () function usage methods and examples

Python Functional Features Overview

Python development environment and download installation

Python while looping using instances

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.