Python while loop statement and synchronous parsing (code example)

Source: Internet
Author: User
As a programming language, Python naturally has its own loop statement, where the python while loop and for loop statements are more common. ( students with programming fundamentals should note that in a Python while loop , there is no do...while format of the loop statement)

Python as a background language, on the basis of other languages, take its essence to its dross: single from the loop statement this point of view, it is used, in other languages are irreplaceable, and on this basis, combined with the background of the actual situation, some of the language "in order to run simple and make logic complex" behavior, has been abandoned.

At first, when I was beginning to program, my teacher asked me a question: " How is your maths and English ?" "At first I did not know why suddenly asked this question, but as I studied gradually, I gradually learned that the teacher's original intention: to have a mathematical basis to learn programming the broadest range of logical language, people will be more efficient than others. of course, the so-called "mathematical basis", as long as the mastery of Junior high school mathematics.

And English, naturally, we all know that the Python language was first invented and issued by the United States, so, the computer personification, in English expressed in the computer language, is the personification of the computer's "mother tongue."

Next, we will begin to explain the Python while loop statement:

(Fig. 1.1)

Figure 1.1 is the process of looping the entire while loop, also called the loop logic

Condition is often a condition that we set for the while loop, which is always true(true). The while Loop statement starts looping only if the condition is true, and when condition is false(not true), the while statement ends the loop.

The following is an instance loop statement when the condition of the while statement is true.

eg

Python code:

#! /usr/bin/env python                #-*-coding:utf-8-*-count = 0while (Count < 9):                                  print ' The Count is: ', Count       coun t = count + 1         print "Good bye!"

Run for a moment and the result is as follows:

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!

Synchronous parsing:

#! /usr/bin/env python

and the General #! /usr/bin/python different, I added envin front of Python,env is to prevent the user not to put/python file in the/usr/bin file, the editor can also find Python files smoothly.

#-*-Coding:utf-8-*-

In order to ensure that the background editing of Chinese can be normal display

Count = 0

Assign a value of 0 to the count variable.

while (Count < 9):

The while loop starts, setting the loop true to: When Count is less than 9

print ' The count is: ',  count

Print The string + variable on python for "The count is thevariable output we set "

Count = Count +1

Whlie true after the condition passes, will continue the loop, for Count+1, put Count plus one after the value , again give count

Print "Good bye!"

This line of code means that when count is greater than 9, that is, count no longer matches the condition of true , another string is output

Above, is the python while the loop statement and synchronous interpretation, I hope that you encounter related problems can be helpful, but also hope that you through this article on the Python while loop statement usage some understanding.

Extracurricular extension:

Python Getting Started loop: For loop vs else usage, enclosing examples

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.