Looping by Element
When working with data types that are iterable, meaning they have a collection of elements that can be looped over, we can write the for loop differently:
# printing all characters in a name using the 'in' keyword name = "John Smith" for letter in name:
print( "Value: { }".format(letter) )
ChApter 4 LIsts And Loops
Go ahead and run that cell. The output will be each letter printed out one at a time. Remember that strings can be indexed and are a collection of characters or symbols, which makes them iterable. This for loop will iterate over each character and run the code within the block with that character/symbol. Table 4-3 goes over the first few iterations of this loop.
Table 4-3. Iteration values for looping over strings with range
Loop Iteration
|
Value of Letter
|
Output
|
1
|
J
|
Value: J
|
2
|
o
|
Value: o
|
3
|
h
|
Value: h
|
4
|
n
|
Value: n
|
5
|
space symbol
|
Value:
|
6
|
s
|
Value: s
| Continue Statement
Now that we’ve seen how a loop works, let’s talk about a few important statements that we can use with loops. The first is the continue statement. Once a continue statement is hit, the current iteration stops and goes back to the top of the loop. Let’s see an example:
# using the continue statement within a foor loop for num in range(5): if num == 3: continue print(num)
Go ahead and run that cell. The output will result in “0, 1, 2, 4” because the continue statement is only read when num is equal to the value of 3. Once the statement is hit, it stops the current iteration and goes back to the top to continue looping on the next iteration. This completely stops the code below continue from being interpreted, so it doesn’t hit the print statement.
Break Statement
One of the most important statements we can use is the break statement. It allows us to break out of a loop at any point in time. Let’s see an example:
# breaking out of a loop using the 'break' keyword for num in range(5): if num == 3: break print(num)
Go ahead and run that cell. The output will result in “0, 1, 2” because we broke the loop completely when num was equal to 3. Once a break is read, the loop completely stops, and no more code within the loop is run. These are useful for stopping a loop when a condition is met.
Note If you use a double loop, the break statement will only break out of the loop that the statement is within. Meaning, it will not break out of both loops if the break statement is used within the inner loop.
Pass Statement
The last of these three statements is pass. The pass statement is simply just a placeholder so that the program doesn’t break. Let’s see an example:
# setting a placeholder using the 'pass' keyword for i in range(5):
# TODO: add code to print number pass
Go ahead and run that cell. Nothing happens, but that’s a good thing. If you take the pass statement out completely, the program will break because there needs to be some sort of code within the block.
ChApter 4 LIsts And Loops
It’s simply there so that we don’t have to write code within the loop just yet. It’s useful for framing out a program.
Note
|
Using “TODO” is general practice for setting a reminder.
|
|
|
|
|
Do'stlaringiz bilan baham: |