Python for loops iterate over sequences such as a dictionary, list, set, or tuple. The loop can access the elements within the sequence. Python usage of for loops are mostly used when you have a block of code which you want to repeat a fixed number of times. The Python for statement iterates over the members of a sequence in order, executing the block each time.
Python for loop
The keyword for in the loop iterates over the sequence and executes a sets of statements for each item in the dictionary, list, set, or tuple. If you are not sure what these are, take a look at the below pages to become familiar with sequences in Python:
Python for Loop in a Dictionary
Input:
1 2 3 4 5 6 7 8 9 | for_loop_dictionary = {"brand" : "Lamborghini", "model" : "Gallardo", "color" : "Yellow", "type" : "Coupe"} print("\nPython Dictionary for Loop") for car in for_loop_dictionary: print(f"\tLooping... {car} ") |
Output:
1 2 3 4 5 | Python Dictionary for Loop Looping... brand Looping... model Looping... color Looping... type |
Python for Loop in a List
Input:
1 2 3 4 5 6 | for_loop_list = ["Python", "Java", "C", ".Net"] print("\nPython List for Loop") for language in for_loop_dictionary: print(f"\tLooping... {for_loop_list} ") |
Output:
1 2 3 4 5 | Python List for Loop Looping... ['Python', 'Java', 'C', '.Net'] Looping... ['Python', 'Java', 'C', '.Net'] Looping... ['Python', 'Java', 'C', '.Net'] Looping... ['Python', 'Java', 'C', '.Net'] |
Python for Loop in a Set
Input:
1 2 3 4 5 6 | for_loop_set = {"Mustang", "Camaro", "Civic", "Jetta"} print("\nPython Set for Loop") for model in for_loop_set: print(f"\tLooping... {model} ") |
Output:
1 2 3 4 5 | Python Set for Loop Looping... Jetta Looping... Camaro Looping... Mustang Looping... Civic |
Python for Loop in a Tuple
Input:
1 2 3 4 5 6 | for_loop_tuple = ("milk", "eggs", "apples", "bread") print("\nPython Tuple for Loop") for item in for_loop_tuple: print(f"\tLooping... {item} ") |
Output:
1 2 3 4 5 | Python Tuple for Loop Looping... milk Looping... eggs Looping... apples Looping... bread |
Python continue Statement
The Python continue statement allows the continuance of an iteration after stopping or meeting the criteria of the loop.
Input:
1 2 3 4 5 6 7 8 | for_loop_list = ["Python", "Java", "C", ".Net"] print("\nPython Continue Statement") for language in for_loop_list: if language == "Java": continue print(f"\tPython loop and continue... {language} ") |
Output:
1 2 3 4 | Python Continue Statement Python loop and continue... Python Python loop and continue... C Python loop and continue... .Net |
Python range() Function
The Python range() function allows a loop to return a sequence numbers that’s provided and increment by 1 to the specified number.
The below two examples will produce the same output. In Python, the range will or even the index will always start with 0, unless you specify the start number. In addition, the range number will increment until the values (10) or (0, 10) but not including the number 10.
Input:
1 2 3 4 5 6 7 | print("\nRange Example 1") for x in range(10): print(x) print("\nRange Example 2") for x in range(0, 10): print(x) |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | Range Example 1 0 1 2 3 4 5 6 7 8 9 Range Example 2 0 1 2 3 4 5 6 7 8 9 |
Python for…else Loop
The else keyword in a loop specified the code to be executed when the loop is completed.
Input:
1 2 3 4 5 6 | print("\nRange Example with a for...else Loop") for x in range(5, 15): print(x) else: print("Else is completed after the loop met the conditions.") |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 | Range Example with a for...else Loop 5 6 7 8 9 10 11 12 13 14 Else is completed after the loop met the conditions. |
Python Nested Loop
Python loops can be nested and a nested loop can occur within another loop. Nested loops are very similar to nested if statements.
Input:
1 2 3 4 5 6 7 8 | numbers = [1, 2, 3] letters = ['a', 'b', 'c'] print("\nPython Nested Loop Example") for n in numbers: for l in letters: print(n, l) |
Output:
1 2 3 4 5 6 7 8 9 10 | Python Nested Loop Example 1 a 1 b 1 c 2 a 2 b 2 c 3 a 3 b 3 c |