If range() is called with only one argument, then Python assumes start = 0. The start argument is the first value in the range. Additional information can be found in Python's documentation for the range() function.
The range() function provides a sequence of integers based upon the function's arguments. When the values in the array for our for loop are sequential, we can use Python's range() function instead of writing out the contents of our array. In this example we print the result of a small computation based on the value of our iterator variable. We can include more complex logic in the body of a for loop as well. In the example below, we use a for loop to print every number in our array.
Here's an Interactive Scrim of a Python For Loop For Loops in Pythonįor loops repeat a portion of code for a set of values.Īs discussed in Python's documentation, for loops work slightly differently than they do in languages such as JavaScript or C.Ī for loop sets the iterator variable to each value in a provided list, array, or string and repeats the code in the body of the for loop for each value of the iterator variable. In this article, we will look at a couple of examples using for loops with Python's range() function. Loops are one of the main control structures in any programming language, and Python is no different.