How to use the For loop in Python

Source: Internet
Author: User
Tags in python

This article mainly introduces the use of the For loop in Python, is the basic knowledge of Python introduction, the need for friends can refer to the

The For loop has an item in Python that iterates through all the sequences, such as a list or a string.

Grammar:

The For loop syntax is as follows:

?

1 2 For Iterating_var in Sequence:statements (s)

If a sequence contains an expression list, compute the first one. Then, the first item in the sequence is assigned to the iteration variable Iterating_var. The statement block is then executed. Each item in the list is assigned to Iterating_var, and the statement block is executed until the entire sequence completes (to the tail).

Flow chart:

Example:

?

1 2 3 4 5 6 7 8 9 10 #!/usr/bin/python for letter in ' Python ': # Example print ': ', letter fruits = [' banana ', ' apple ' , ' mango '] for fruit in fruits: # Second Example print ' current fruit: ', fruit print ' good bye! '

When the above code is executed, the following results are produced:

?

1 2 3 4 5 6 7 8 9 10 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!

By traversing the sequence index:

An alternative to iterating through each project is through the index offset sequence itself. The following is a simple example:

?

1 2 3 4 5 6 7 #!/usr/bin/python fruits = [' banana ', ' apple ', ' mango ' for the index in range (len (fruits)): print ' current fruit: ', fruits [Index] print "Good bye!"

When the above code is executed, the following results are produced:

?

1 2 3 4 Current Fruit:banana current fruit:apple current Fruit:mango good bye!

Here we take the built-in function Len (), which calculates the total number of tuple elements and the actual traversal order that the range () built-in function gives us.

Looping through the Else statement

Python supports the Else statement associated with a loop statement.

If the Else statement is used with a for loop and the Else statement is executed, the loop already iterates through the completion list.

If a while loop is used in the Else statement, the Else statement is executed when the condition is false.

The following example shows an else statement that searches for a combination of primes from 10 to 20.

?

1 2 3 4 5 6 7 8 9 10 #!/usr/bin/python for Num in range (10,20): #to iterate between-I in range (2,num): #to iterate on the factor S of the number if num%i = 0: #to determine the factor j=num/i #to calculate the second factor print '%d equals%d *%d '% (num,i,j) break #to the "next" number, the #first for else: # Else part of the loop print num, ' is a prime n Umber '

When the above code is executed, the following results are produced:

?

1 2 3 4 5 6 7 8 9 10 2 * 5 are a prime number equals 2 * 6 is a prime number equals 2 * 7 equals 3 * 5 equals 2 * 8 The is a prime number equals 2 * 9 is a prime number

        Note < : More Wonderful tutorials please focus on the triple Programming

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.