Skip to content

Commit 998f362

Browse files
authored
Merge pull request #320 from fey/en-for-in-range
add enlish for in range
2 parents 5bd4788 + fe23000 commit 998f362

File tree

6 files changed

+159
-2
lines changed

6 files changed

+159
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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: []
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
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>`
2+
3+
Call examples:
4+
5+
```python
6+
print_table_of_squares(1, 3)
7+
# => square of 1 is 1
8+
# => square of 2 is 4
9+
# => square of 3 is 9
10+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
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:
2+
3+
```python
4+
sum = 0
5+
i = 0
6+
7+
while i < 10:
8+
sum += i
9+
i += 1
10+
11+
print(sum) # => 45
12+
```
13+
14+
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.
15+
16+
We can rewrite this code into a `for` loop
17+
18+
```python
19+
sum = 0
20+
21+
for i in range(10):
22+
sum += i
23+
24+
print(sum) # => 45
25+
```
26+
27+
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.
28+
29+
## The `range()` function
30+
31+
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.
32+
33+
`range()` has several uses:
34+
35+
* `range(stop)` creates a sequence from 0 to `stop - 1`.
36+
* `range(start, stop)` creates a sequence from start to `stop - 1`.
37+
* `range(start, stop, step)` creates a sequence of numbers from start to `stop - 1`, with step `step`.
38+
39+
We saw the example with one final value above. Let's consider another one - print the numbers from 1 to 3 to the screen:
40+
41+
```python
42+
43+
for i in range(1, 4):
44+
print(i)
45+
46+
# => 1
47+
# => 2
48+
# => 3
49+
```
50+
51+
Now let's try to output the numbers in reverse order
52+
53+
```python
54+
for i in range(3, 0, -1):
55+
print(i)
56+
57+
# => 3
58+
# => 2
59+
# => 1
60+
```
61+
62+
In the examples above, we can see that the iteration completes to a final value
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
name: For loop and range function
2+
tips: []
3+
definitions: []

modules/50-loops/80-for-in-range/ru/EXERCISE.md

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
Реализуйте функцию `print_table_of_squares(from, to)`, которая печатает на экран квадраты чисел. Она первое `from` и последнее `to` число печатает строку `square of <число> is <результат>`
32

43
Примеры вызова:

modules/50-loops/80-for-in-range/ru/README.md

-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ print(sum) # => 45
2727

2828
Первый пример использует `while`, который продолжает работать пока `i < 10`. Второй использует `for` и выполняет итерацию от 0 до 9 с помощью функции `range()`. Оба выполняют одно и то же: складывают числа от 0 до 9 в переменную `sum`, но используют разные способы выполнения итераций.
2929

30-
3130
## Функция `range()`
3231

3332
Функция range в Python является встроенной функцией, которая создает последовательность чисел внутри определенного диапазона. Ее можно использовать в цикле for для контроля количества итераций.

0 commit comments

Comments
 (0)