How to Use the for Loop in Python

Source: Internet
Author: User
This article mainly introduces how to use the for Loop in Python. It is the basic knowledge in getting started with Python. If you need it, you can refer to the for loop projects that traverse all sequences in Python, such as a list or a string.
Syntax:

The for loop syntax is as follows:

for iterating_var in sequence:  statements(s)

If a sequence contains a list of expressions, calculate the first one. Then, the first item in the sequence is allocated to the iteration variable iterating_var. Then, the statement block is executed. Each item in the list is allocated to iterating_var, and the statement block is executed until the entire sequence is completed (to the end ).
Flowchart:

Example:

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

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

Current Letter : PCurrent Letter : yCurrent Letter : tCurrent Letter : hCurrent Letter : oCurrent Letter : nCurrent fruit : bananaCurrent fruit : appleCurrent fruit : mangoGood bye!

Index through traversal sequence:

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

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

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

Current fruit : bananaCurrent fruit : appleCurrent fruit : mangoGood bye!

Here, we use the built-in function len (), which calculates the total number of tuples and the actual traversal order given by the range () built-in function.
Loop using else statements

Python supports else statements associated with loop statements.

  • If the else statement and for statement are used in a loop, the loop has completed the list by iteration when the else statement is executed.
  • If the while loop is used in the else statement, when the condition is false, the else statement is executed.

The following example demonstrates an else statement that searches for a combination of prime numbers ranging from 10 to 20.

#!/usr/bin/pythonfor num in range(10,20): #to iterate between 10 to 20  for i in range(2,num): #to iterate on the factors of the number   if num%i == 0:   #to determine the first factor     j=num/i     #to calculate the second factor     print '%d equals %d * %d' % (num,i,j)     break #to move to the next number, the #first FOR  else:         # else part of the loop   print num, 'is a prime number'

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

10 equals 2 * 511 is a prime number12 equals 2 * 613 is a prime number14 equals 2 * 715 equals 3 * 516 equals 2 * 817 is a prime number18 equals 2 * 919 is a prime number

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.