Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Loop Details
#2
Next let's try a for loop. A for loop works a bit like a while loop in that it repeats, but it is generally used when you know how many times you want to repeat something. Here's a simple example:

for bottle in range(99):
    print('{0} bottles of beer on the wall, {0} bottles of beer.'.format(99 - bottle))
    print('Take one down, pass it around, {} bottles of beer on the wall.'.format(98 -bottle))
If you run this code, you will see that it recreates (almost) the repetitive song we teach to children to turn them into alcoholics. That is certainly an easier way to do it than writing each of those lines 99 times in your code.

So what exactly happened here? The format here is 'for <variable> in <iterable>'. Don't worry about what an iterable is right now, that's more of an advanced topic. It includes lists, strings, dictionaries, sets, and certain functions. The range function is one of those functions. It repeatedly returns numbers starting at 0, increasing by one, and stopping when it gets to the number specified.

So, when the for statement is first reached in the program, it gets a number from range, in this case 0. Then it assigns that number to the variable (bottle). Then the for loop runs the block of code indented beneath it, as with the while loop.

Our indented block has two print function calls, each with a format call. The bottle variable is used in the format calls. 99 minus 0 is 99, so the song starts with 99 bottles. 98 minus 0 is 98, so after we take one down on the third line, there are 98 bottles left.

After the indented block of code (the two print calls), the program returns to the for statement. It evaluates the range function again, getting the number 1. Then it goes and runs the indented block of code again, this time with bottle equal to 1.

This continues for some time. Ninety-nine times, in fact, because that's what we told the range function to do. However, on the 100th time through, range stops giving out numbers. (Instead it gives a special StopIteration error, but that's an advanced detail you don't need to worry about.) Note that the last number it gives out is 98, not 99. When range gets to the stop point you gave it, it stops without returning that value. When range stops giving out numbers, the loop stops executing. The program skips from the for statement, past the indented block of code, and starts executing any code that comes after that block of code.

Now, this is a pretty basic and common for loop, using the range function. But there is more we can do with the range function than just count up from zero. Say you get really tired of the 99 Bottles of Beer song rather quickly. Who doesn't? So you only want to start with only 19 bottles of beer on the wall. One way to do this is to give two parameters to the range function:

for bottle in range(80, 99):
    print('{0} bottles of beer on the wall, {0} bottles of beer.'.format(99 - bottle))
    print('Take one down, pass it around, {} bottles of beer on the wall.'.format(98 -bottle))
With two parameters, the range function starts with the first parameter, and keeps increasing by one, stopping when it gets to the second parameter (and again, not giving out that last number). So now, the first time through the loop bottle is equal to 80, and the program says there are 20 bottles of beer on the wall.

But wait! There's more!

The range function can take a third parameter: the increment used from one number to the next. So we could change our loop to only sing the verses for odd numbers:

for bottle in range(0, 99, 2):
    print('{0} bottles of beer on the wall, {0} bottles of beer.'.format(99 - bottle))
    print('Take one down, pass it around, {} bottles of beer on the wall.'.format(98 -bottle))
Note that the three numbers of the range function work just like the three numbers in a slice. So you can use this whenever you are confused about the range function. If you are confused about slice, you can just remember that they work like the range function.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Messages In This Thread
Loop Details - by ichabod801 - Sep-13-2019, 06:41 PM
RE: Loop Details - by ichabod801 - Sep-13-2019, 06:42 PM
RE: Loop Details - by ichabod801 - Sep-13-2019, 06:43 PM
RE: Loop Details - by ichabod801 - Sep-13-2019, 06:44 PM
RE: Loop Details - by ichabod801 - Sep-13-2019, 06:47 PM
RE: Loop Details - by ichabod801 - Sep-13-2019, 06:49 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020