Python's for loop allows iteration over items in a collection, such as lists, tuples, strings, and dictionaries, and is ideal for repeatedly executing a block of code on each item. The for loop syntax declares a loop variable that takes each item from the collection in each iteration. The loop can be tweaked with features like break, continue, and else. To iterate from 0 to 10, you can use the for index in range(11) construct. To repeat code a number of times without processing the data of an iterable, use the for _ in range(times) construct. For index-based iteration, you can use for index, value in enumerate(iterable) to access both index and item. Python's for loop is a compound statement with a header and a code block that runs a predefined number of times. The basic syntax of a for loop is for variable in iterable: <body>, where variable is the loop variable and iterable is the data collection. In Python, an iterable is an object that can be iterated over, such as lists, tuples, strings, dictionaries, and sets. You can also have a loop with multiple loop variables, and the loop will run its header once but won't execute its body if the input iterable is empty.
realpython.com
realpython.com
