for Loops in Python

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.

1

Here is what happened

  1. At the top, we have our for loop. The iterator variable is “letter” and we are looping through the data of the string “education”.
  2. 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.
  3. 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

1

Here is what it does.

  1. In line 1, you provide a word as indicated by the instructions in the parentheses.
  2. Line 2 is the for loop. letter is the iteration variable for our word in “Value”
  3. Line 3 is the if statement. The strong on “Value” is checked to make sure it is 10 characters or less.
  4. In line 4, if “Value” is greater than 10 characters you get the message that the cheer is too long.
  5. Line 5 is the break which stops the loop from continuing.
  6. 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

1

Here is the output with more than 10 characters

1

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.

1

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

1

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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.