For loop in Python

Source: Internet
Author: User
This article mainly introduces the information about the for loop in Python, which is very good and has reference value. if you need it, you can refer to the structure of the next loop, this 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.

For loop:

Any item in the Python for loop traversal 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 evaluation. Then, the first item in the sequence is assigned the iteration variable iterating_var. Next, execute the statement block. Each item in the list is allocated to iterating_var, and the report block is executed until the entire sequence is exhausted.

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.

Example:

#!/usr/bin/pythonfor letter in 'Python': # First Exampleprint 'Current Letter :', letterfruits = ['banana', 'apple', 'mango']for fruit in fruits: # Second Exampleprint 'Current fruit :', fruitprint "Good bye!"

The output result is as follows:

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 index:

Another way to traverse each project is by the offset index of the sequence itself:

For example:

#!/usr/bin/pythonfruits = ['banana', 'apple', 'mango']for index in range(len(fruits)):print 'Current fruit :', fruits[index]print "Good bye!"

This produces the following results:

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

Here, with the assistance of len (), we adopt the built-in function, which provides the total number of elements in tuple and the actual sequential traversal given by the built-in function of range.

The above is a small Editor to introduce you to the knowledge of the for loop in Python. it is very good and has reference value. if you are interested, study it together!

For more articles about the for loop in Python, please follow the PHP Chinese network!

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.