The use of for loops are valuable when you need your application to do a repetitive task. Once the task is completed there is some sort of output that is returned. Understanding how to create a for loop is a critical step in utilizing the Python language.
Making for loops
Here is the basic syntax for a for loop
for item in data: do something
The word “for” indicates a for loop. The word “item” is an iteration variable. An iteration variable is a variable that changes value each time the loop goes through the data. It takes on the current value that is being analyzed for whatever purpose the loop has. You can name the iteration variable anything you want but a general rule is to use names that make sense for the context. Otherwise, nobody else will be able to understand your code.
After the colon is where you find “do something” here you put the command for whatever the loop is supposed to do. Below is an actual example of the use of the for loop.
Here is what happened
- At the top, we have our for loop. The iterator variable is “letter” and we are looping through the data of the string “education”.
- The next line is the action the for loop will perform. Essentially, the loop will pull each later from the string “education” and insert them one at a time into the phrase “Give me an”,. Notice how the word “letter” is at the end of our print statement. This the iteration variable that changes each time our for loop goes through the string “education.
- The output is several print statements each containing a different letter from the string “education”
for loops with Breaks
Breaks are used to provide conditions in which the loop will stop. In the example below, we add some code to our cheer that allows you to enter your own cheer. However, the church must be less than 10 letters otherwise you get a message that word is too long. Below is the code
Here is what it does.
- In line 1, you provide a word as indicated by the instructions in the parentheses.
- Line 2 is the for loop. letter is the iteration variable for our word in “Value”
- Line 3 is the if statement. The strong on “Value” is checked to make sure it is 10 characters or less.
- In line 4, if “Value” is greater than 10 characters you get the message that the cheer is too long.
- Line 5 is the break which stops the loop from continuing.
- In line 6, if the word is less than 10 characters you get the cheer with each letter.
Below is the output with less than 10 characters
Here is the output with more than 10 characters
Continue and for loop
The continue clause allows you to check the data and only process it based on certain conditions. In the code below, we are going to change our cheer code so that it removes spaces when making the cheer.
The code is mostly the same with a few exceptions
- The if statement looks for blank spaces and these are left out of the cheer.
- The continue clause tells python to keep going
- Finally, the cheer is given
Below is what the output looks like if you ran this code
You can see that I put many blank spaces in-between the letters but these do not appear in the code. This is because of the continue clause.
Conclusion
for loops are a basic yet powerful tool of programming. In Python, for loops are used for the same reason as other languages and that is for completing repetitive tasks. The examples, here provide some simple ways in which this can be done.