Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
for loop (1/5) | Loops
Learn Python from Scratch
course content

Зміст курсу

Learn Python from Scratch

Learn Python from Scratch

1. The basics
2. Arithmetic operations
3. Common data types
4. Conditional statements
5. Other data types
6. Loops
7. Functions

bookfor loop (1/5)

Often we don't need to iterate until something happens - we want to iterate over some set of elements, or something like that.

To construct loop iterating over elements use for loop:

12
for var in smth_iterable: do_smth
copy

Common practice is using range() as an iterable object we will use in the loop.
range(n) function with 1 positive argument generates a numbers from 0 to n (exclusive, i.e. to n-1).
range(m,n) function generates number from m to n exclusive in case if both are positive and m < n.

For example, we can rewrite the example from section 5.1 using for loop (in that exercise we printed numbers from 1 to 9)

123
# using for loop to print numbers from 1 to 9 for i in range(1,10): print(i)
copy

Please note, that unlike while loop, variable i inside for loop shouldn't be defined before. It's like a dummy variable.

Завдання

Using for loop print all the numbers from 10 to 20 squared.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 6. Розділ 3
toggle bottom row

bookfor loop (1/5)

Often we don't need to iterate until something happens - we want to iterate over some set of elements, or something like that.

To construct loop iterating over elements use for loop:

12
for var in smth_iterable: do_smth
copy

Common practice is using range() as an iterable object we will use in the loop.
range(n) function with 1 positive argument generates a numbers from 0 to n (exclusive, i.e. to n-1).
range(m,n) function generates number from m to n exclusive in case if both are positive and m < n.

For example, we can rewrite the example from section 5.1 using for loop (in that exercise we printed numbers from 1 to 9)

123
# using for loop to print numbers from 1 to 9 for i in range(1,10): print(i)
copy

Please note, that unlike while loop, variable i inside for loop shouldn't be defined before. It's like a dummy variable.

Завдання

Using for loop print all the numbers from 10 to 20 squared.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 6. Розділ 3
toggle bottom row

bookfor loop (1/5)

Often we don't need to iterate until something happens - we want to iterate over some set of elements, or something like that.

To construct loop iterating over elements use for loop:

12
for var in smth_iterable: do_smth
copy

Common practice is using range() as an iterable object we will use in the loop.
range(n) function with 1 positive argument generates a numbers from 0 to n (exclusive, i.e. to n-1).
range(m,n) function generates number from m to n exclusive in case if both are positive and m < n.

For example, we can rewrite the example from section 5.1 using for loop (in that exercise we printed numbers from 1 to 9)

123
# using for loop to print numbers from 1 to 9 for i in range(1,10): print(i)
copy

Please note, that unlike while loop, variable i inside for loop shouldn't be defined before. It's like a dummy variable.

Завдання

Using for loop print all the numbers from 10 to 20 squared.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Often we don't need to iterate until something happens - we want to iterate over some set of elements, or something like that.

To construct loop iterating over elements use for loop:

12
for var in smth_iterable: do_smth
copy

Common practice is using range() as an iterable object we will use in the loop.
range(n) function with 1 positive argument generates a numbers from 0 to n (exclusive, i.e. to n-1).
range(m,n) function generates number from m to n exclusive in case if both are positive and m < n.

For example, we can rewrite the example from section 5.1 using for loop (in that exercise we printed numbers from 1 to 9)

123
# using for loop to print numbers from 1 to 9 for i in range(1,10): print(i)
copy

Please note, that unlike while loop, variable i inside for loop shouldn't be defined before. It's like a dummy variable.

Завдання

Using for loop print all the numbers from 10 to 20 squared.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Секція 6. Розділ 3
Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
some-alt