Legacy - For Loops
- 01:35
Learn how to create "For" loops in Python.
Downloads
No associated resources to download.
Transcript
Let us now look at how to write for loops in Python. For loops are traditionally used when you have a block of code that you want to repeat a fixed number of times the Python for loop iterates over the member of a sequence in order executing the block each time. When writing the for loop, we want to again define the initial variables or the range of values to iterate over. The loop starts with a fall, and then it is followed by the range of values to look over. This is followed by a colon, and then we include a statement or an action at the end. Once the iteration is completed, it is important to note that indentation is really important. So once we reach at the end of line or at the end of a colon, in order to give proper indentation, we have to press ENTER rather than manually setting the indentation let Python do the job for you. In the example on the screen, we define a variable consisting of a list of terms related to bonds. We then ask the variable x to iterate over the list and ask it to print the iterated values. This block of code prints the list of terms related to bonds. Key points to note when writing a for loop, defined values or a range of iteration. Start the loop with four provide the variables to iterate over followed by a colon, proper dictation and then the added output