How to Use the for Loop in Python
This article mainly introduces how to use the for Loop in Python. It is the basic knowledge in getting started with Python. for more information, see
A for loop contains items that traverse all sequences in Python, such as a list or a string.
Syntax:
The for loop syntax is as follows:
?
1 2 |
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:
?
1 2 3 4 5 6 7 8 9 10 |
#! /Usr/bin/python For letter in 'python': # First Example Print 'current Letter: ', letter Fruits = ['bana', '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 generated:
?
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! |
Index through traversal sequence:
An alternative to iteration through each project is the index offset sequence itself. The following is a simple example:
?
1 2 3 4 5 6 7 |
#! /Usr/bin/python Fruits = ['bana', '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:
?
1 2 3 4 |
Current fruit: banana Current fruit: apple Current fruit: mango Good 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.
?
1 2 3 4 5 6 7 8 9 10 |
#! /Usr/bin/python For 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 Else: # else part of the loop Print num, 'is a prime number' |
When the above code is executed, the following results are generated:
?
1 2 3 4 5 6 7 8 9 10 |
10 equals 2*5 11 is a prime number 12 equals 2*6 13 is a prime number 14 equals 2*7 15 equals 3*5 16 equals 2*8 17 is a prime number 18 equals 2*9 19 is a prime number |
Note <>: For more exciting tutorials, please pay attention to the help house programming