For loops in Python

Source: Internet
Author: User
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.

For loop:

Any item in a python for loop that iterates through a sequence, such as a list or a string, has the ability.

The For Loop syntax is:

For Iterating_var in Sequence:statements (s)

If a sequence contains a list of expressions, it is the first one to evaluate. Then, the first item in the sequence allocates the iteration variable Iterating_var. Next, execute the statement block. Each item in the list is assigned to Iterating_var, and the report block is executed until the entire sequence is exhausted.

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.

Example:

#!/usr/bin/pythonfor ' Python ': # first Exampleprint ' Current letter: ', letterfruits = [' banana ', ' apple ', ' Mang O ']for fruit in fruits: # Second Exampleprint ' current fruit: ', fruitprint ' good bye! '

The above will output the result:

Current Letter:p
Current Letter:y
Current Letter:t
Current Letter:h
Current Letter:o
Current Letter:n
Current Fruit:banana
Current Fruit:apple
Current Fruit:mango
Good bye!

Iteration sequence exponent:

Another way to iterate through each item is by the offset exponent of the sequence itself:

For example:

#!/usr/bin/pythonfruits = [' banana ', ' apple ', ' Mango ']for index in range (len (fruits)):p rint ' current fruit: ', fruits[ind Ex]print "Good bye!"

This will produce the following results:

Current Fruit:banana
Current Fruit:apple
Current Fruit:mango
Good bye!

Here we take the help of Len (), built-in functionality, which provides the total number of elements in a tuple, as well as the range () built-in functions that give us the actual sequential traversal.

The above is a small series to introduce you to the analysis of Python in the relevant knowledge of the loop, very good, with reference value, interested friends learn together!

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.