|
| 1 | +--- |
| 2 | + |
| 3 | +name: For loop and range function |
| 4 | +theory: | |
| 5 | + Imagine that we have a series of numbers from 0 to 9. We want to add these numbers together. We could do it like this: |
| 6 | +
|
| 7 | + ```python |
| 8 | + sum = 0 |
| 9 | + i = 0 |
| 10 | +
|
| 11 | + while i < 10: |
| 12 | + sum += i |
| 13 | + i += 1 |
| 14 | +
|
| 15 | + print(sum) # => 45 |
| 16 | + ``` |
| 17 | +
|
| 18 | + First, we set the initial sum to 0. Then we run a loop in which the variable `i` starts taking values starting from 0 and going up to 10. At each step we add the value of `i` to our sum and increase `i` by 1. As soon as `i` becomes equal to 10, the loop ends and the program gives us the sum of all numbers from 0 to 9 equal to 45. |
| 19 | +
|
| 20 | + We can rewrite this code into a `for` loop |
| 21 | +
|
| 22 | + ```python |
| 23 | + sum = 0 |
| 24 | +
|
| 25 | + for i in range(10): |
| 26 | + sum += i |
| 27 | +
|
| 28 | + print(sum) # => 45 |
| 29 | + ``` |
| 30 | +
|
| 31 | + The first example uses `while`, which keeps running until `i < 10`. The second uses `for` and iterates from 0 to 9 using `range()`. Both do the same thing: add the numbers from 0 to 9 to the `sum` variable, but they use different ways to iterate. |
| 32 | +
|
| 33 | + ## The `range()` function |
| 34 | +
|
| 35 | + The range function in Python is a built-in function that creates a sequence of numbers within a specific range. It can be used in a for loop to control the number of iterations. |
| 36 | +
|
| 37 | + `range()` has several uses: |
| 38 | +
|
| 39 | + * `range(stop)` creates a sequence from 0 to `stop - 1`. |
| 40 | + * `range(start, stop)` creates a sequence from start to `stop - 1`. |
| 41 | + * `range(start, stop, step)` creates a sequence of numbers from start to `stop - 1`, with step `step`. |
| 42 | +
|
| 43 | + We saw the example with one final value above. Let's consider another one - print the numbers from 1 to 3 to the screen: |
| 44 | +
|
| 45 | + ```python |
| 46 | +
|
| 47 | + for i in range(1, 4): |
| 48 | + print(i) |
| 49 | +
|
| 50 | + # => 1 |
| 51 | + # => 2 |
| 52 | + # => 3 |
| 53 | + ``` |
| 54 | +
|
| 55 | + Now let's try to output the numbers in reverse order |
| 56 | +
|
| 57 | + ```python |
| 58 | + for i in range(3, 0, -1): |
| 59 | + print(i) |
| 60 | +
|
| 61 | + # => 3 |
| 62 | + # => 2 |
| 63 | + # => 1 |
| 64 | + ``` |
| 65 | +
|
| 66 | + In the examples above, we can see that the iteration completes to a final value |
| 67 | +
|
| 68 | +
|
| 69 | +instructions: | |
| 70 | + Implement the `print_table_of_squares(from, to)` function that prints squares of numbers to the screen. It first `from` and last `to` a number prints the string `square of <number> is <result>` |
| 71 | +
|
| 72 | + Call examples: |
| 73 | +
|
| 74 | + ```python |
| 75 | + print_table_of_squares(1, 3) |
| 76 | + # => square of 1 is 1 |
| 77 | + # => square of 2 is 4 |
| 78 | + # => square of 3 is 9 |
| 79 | + ``` |
| 80 | +
|
| 81 | +
|
| 82 | +tips: [] |
| 83 | + |
| 84 | +definitions: [] |
0 commit comments