Python For Loops
The for loop is used to iterate over sequences (i.e. lists, tuples, dictionaries, collections, or strings).
This is less similar to the for keyword in other programming languages and more like the iterator methods in other object-oriented programming languages.
By using a for loop, we can execute a set of statements for a list, tuple, each item in a collection, etc.
an actual example
Prints each fruit in the fruits list:
fruits = ["apple", "banana", "cherry"] for x in fruits: print(x)
running example
apple banana cherry
Tip: For loops do not require index variables to be set in advance.
Loop over strings
Even strings are iterable objects that contain a series of characters:
an actual example
Loop over the letters in the word "banana":
for x in "banana": print(x)
running example
banana
break statement
By using the break statement, we can stop the loop before it traverses all the items:
an actual example
If x is "banana", exit the loop:
fruits = ["apple", "banana", "cherry"] for x in fruits: print(x) if x == "banana": break
running example
apple banana
an actual example
Exits the loop when x is "banana", but this time breaks before printing:
fruits = ["apple", "banana", "cherry"] for x in fruits: if x == "banana": break print(x)
running example
continue statement
By using the continue statement, we can stop the current iteration of the loop and move on to the next:
an actual example
No bananas are printed:
fruits = ["apple", "banana", "cherry"] for x in fruits: if x == "banana": continue print(x)
running example
apple cherry
range() function
To loop through a set of code a specified number of times, we can use the range() function.
The range() function returns a sequence of numbers, starting at 0 by default and incrementing by 1 (by default), and ending with the specified number.
an actual example
Use the range() function:
for x in range(10): print(x)
running example
0 1 2 3 4 5 6 7 8 9
Note: range(10) is not a value from 0 to 10, but a value from 0 to 9.
The range() function defaults to a starting value of 0, but you can specify a starting value by adding a parameter: range(3, 10), which means values from 3 to 10 (but not including 10):
an actual example
Use the start parameter:
for x in range(3, 10): print(x)
running example
3 4 5 6 7 8 9
The range() function defaults to incrementing the sequence by 1, but you can specify the increment by adding a third argument: range(2, 30, 3):
an actual example
Use an incremental sequence of 3 (default value is 1):
for x in range(3, 50, 6): print(x)
running example
Else in a For loop
The else keyword in a for loop specifies the block of code to be executed at the end of the loop:
an actual example
Prints all numbers from 0 to 9 and prints a message at the end of the loop:
for x in range(10): print(x) else: print("Finally finished!")
running example
0 1 2 3 4 5 6 7 8 9 Finally finished!
nested loop
Nested loops are loops within loops.
For each iteration of the "outer loop", the "inner loop" will be executed once:
an actual example Print each adjective for each fruit:
adj = ["red", "big", "tasty"] fruits = ["apple", "banana", "cherry"] for x in adj: for y in fruits: print(x, y)
running example
red apple red banana red cherry big apple big banana big cherry tasty apple tasty banana tasty cherry
pass statement
The for statement cannot be empty, but if for some reason you write an empty for statement, use a pass statement to avoid errors.
an actual example
for x in [0, 1, 2]: pass
To this point this article on Python Beginner's Guide (18) Python's For loop is introduced to this article, more related to Python's For loop content, please search for my previous posts or continue to browse the following articles hope that you will support me in the future more!