Basic tutorial for getting started with Python (learningPython) -- 4.2Python's counting loop body for statement _ PHP Tutorial

Source: Internet
Author: User
Basic tutorial for getting started with Python (learningPython) -- 4.2Python's count loop body for statement. Another type of loop body structure in Python is the counting loop body for. some statement blocks are executed for a certain number of times through the for loop. the syntax structure is as follows. Another type of loop body structure of Python is the count loop body for. some statement blocks are executed for a certain number of times through the for loop. the syntax structure is as follows.

The idea of the for loop application in Python is the same as that in other advanced languages such as C. When the for condition is met, the statement block in the for statement is executed, the difference is that the for condition writing method is somewhat different from other advanced languages.

[Python]View plaincopy
  1. For variable in [value1, value2,...]:
  2. (TAB) statement
  3. (TAB) statement
  4. (TAB) etc.

Here, the left square brackets '[' and right square brackets ']' appear for the first time. the data sequence enclosed by left and right square brackets is called the list, for more information about the list, see.

Note that [value1, value2,...] must be followed by a colon:; otherwise, a syntax error occurs.

The for loop principle is as follows: how many times does a loop take a value valuex from the list behind in and assign a value to the variable behind? A list contains several data loops several times. when all the data in the list is finished, the for operation ends. Therefore, the number of for executions depends on the number of data in the list, the following is an example.

[Python]View plaincopy
  1. Def main ():
  2. Print ('I will display the numbers 1 through 5 .')
  3. For num in [1, 2, 3, 4, 5]:
  4. Print (num)
  5. # Call the main function.
  6. Main ()

Line 2 of the code is a for loop. each time a value is obtained from the list [3rd, 5] and assigned to num, the list ([1, 2, 3, 4, 5]) There are 5 data records. we can see that for can be completed after 5 cycles.


The running result is as follows:

Then, let's analyze why the results are like this?

Each loop for will extract a data from [1, 2, 3, 4, 5] to num.

Explain implements block loop execution of some statements for a certain number of times. The syntax structure is as follows. For loop application of Py thon...

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.