|
1 | 1 | # About |
2 | 2 |
|
| 3 | +Python has two looping constructs. |
| 4 | +`while` loops for _indefinite_ (uncounted) iteration and `for` loops for _definite_, (counted) iteration. |
| 5 | +The keywords `break`, `continue`, and `else` help customize loop behavior. |
| 6 | + |
| 7 | +<br> |
| 8 | + |
| 9 | +## `While` |
| 10 | + |
| 11 | +[`while`][while statement] loops continue to exectute as long as the loop expression or "test" evaluates to `True` in a [`boolean context`][truth value testing], terminating when it evaluates to `False`. |
| 12 | + |
| 13 | +```python |
| 14 | + |
| 15 | +# Lists are considered "truthy" in a boolean context if they |
| 16 | +# contain one or more values, and "falsy" if they are empty. |
| 17 | + |
| 18 | +>>> placeholders = ["spam", "ham", "eggs", "green_spam", "green_ham", "green_eggs"] |
| 19 | + |
| 20 | +>>> while placeholders: |
| 21 | +... print(placeholders.pop(0)) |
| 22 | +... |
| 23 | +spam |
| 24 | +ham |
| 25 | +eggs |
| 26 | +green_spam |
| 27 | +green_ham |
| 28 | +green_eggs |
| 29 | +``` |
| 30 | + |
| 31 | +<br> |
| 32 | + |
| 33 | +## `For` |
| 34 | + |
| 35 | +The basic [`for`][for statement] loop is better described as a _`for each`_ which cycles through the values of any [iterable object][iterable], terminating when there are no values returned from calling [`next()`][next built-in] (_raising a [`StopIteration`][stopiteration]_). |
| 36 | + |
| 37 | +```python |
| 38 | + |
| 39 | +>>> word_list = ["bird", "chicken", "barrel", "bongo"] |
| 40 | + |
| 41 | +>>> for word in word_list: |
| 42 | +... if word.startswith("b"): |
| 43 | +... print(f"{word.title()} starts with a B.") |
| 44 | +... else: |
| 45 | +... print(f"{word.title()} doesn't start with a B.") |
| 46 | +... |
| 47 | +Bird starts with a B. |
| 48 | +Chicken doesn't start with a B. |
| 49 | +Barrel starts with a B. |
| 50 | +Bongo starts with a B. |
| 51 | + |
| 52 | +``` |
| 53 | + |
| 54 | +<br> |
| 55 | + |
| 56 | +## Sequence Object `range()` |
| 57 | + |
| 58 | +When there isn't a specific `iterable` given, the special [`range()`][range] sequence is used. |
| 59 | +`range()` requires a number before which to `stop`, and can optionally take a `start` parameter. |
| 60 | +If no `start` number is provided, the sequence will begin with 0. |
| 61 | +`range()` objects are `lazy` (_values are generated on request_), support all [common sequence operations][common sequence operations], and take up a fixed amount of memory, no matter how long the sequence. |
| 62 | +Interestingly, `range()` [is not an iterator][range is not an iterator], and can be used many in non-looping contexts. |
| 63 | + |
| 64 | +```python |
| 65 | +# Here we use range to produce some numbers, rather than creating a list of them in memory. |
| 66 | +# The values will start with 1 and stop *before* 7 |
| 67 | + |
| 68 | +>>> for number in range(1, 7): |
| 69 | +... if number % 2 == 0: |
| 70 | +... print(f"{number} is even.") |
| 71 | +... else: |
| 72 | +... print(f"{number} is odd.") |
| 73 | +1 is odd. |
| 74 | +2 is even. |
| 75 | +3 is odd. |
| 76 | +4 is even. |
| 77 | +5 is odd. |
| 78 | +6 is even. |
| 79 | + |
| 80 | + |
| 81 | +# range() can also take a *step* parameter. |
| 82 | +# Here we use range to produce only the "odd" numbers, starting with 3 and stopping *before* 15. |
| 83 | + |
| 84 | +>>> for number in range(3, 15, 2): |
| 85 | +... if number % 2 == 0: |
| 86 | +... print(f"{number} is even.") |
| 87 | +... else: |
| 88 | +... print(f"{number} is odd.") |
| 89 | +... |
| 90 | +3 is odd. |
| 91 | +5 is odd. |
| 92 | +7 is odd. |
| 93 | +9 is odd. |
| 94 | +11 is odd. |
| 95 | +13 is odd. |
| 96 | + |
| 97 | +``` |
| 98 | + |
| 99 | +<br> |
| 100 | + |
| 101 | +## Values and Indexes with `enumerate()` |
| 102 | + |
| 103 | +If both values and indexes are needed, the built-in [`enumerate()`][enumerate] will return an [`iterator`][iterator] over (`index`, `value`) pairs: |
3 | 104 |
|
4 | 105 | ```python |
5 | 106 |
|
@@ -111,7 +212,7 @@ Found the above b-words, out of 6 words in the word list. |
111 | 212 | ... print("Found an S, stopping iteration.") |
112 | 213 | ... break |
113 | 214 | ...# This is not run, because break was triggered |
114 | | -... else: |
| 215 | +... else: |
115 | 216 | ... print(f"Found the above b-words, out of {len(word_list)} words in the word list.") |
116 | 217 | ... |
117 | 218 | Bird (at index 0) starts with a B. |
|
0 commit comments