Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Simple While Loop | While Loops: Get Started
Python Loops
course content

Зміст курсу

Python Loops

Python Loops

1. While Loops: Get Started
2. While Loops: Essentials
3. For Loops
4. Nested Loops

Simple While Loop

The While statement is one of the basic programming concepts. It repeatedly executes the piece of code.

Imagine you have a task: write the code to calculate the factorial of the given number. For example, for n = 6 the result is: res = 1*2*3*4*5*6 = 720.

The step by step solution is:

Each operation is a new line of code. How many lines are needed? For huge numbers, you'll get very long instructions.

Instead, we'll use the loops. Take a look at how it works:

There are two main parts: stop condition and body. Body are instructions that need to be executed several times. For the factorial task, the body is multiplication the res by the next number. The condition helps to know when to stop.

If a condition is True, the body is executed one more time. When the condition becomes False, the loop is over and we leave it.

In the factorial task, we multiply all numbers from 1 to n so the loop is over when the multiplier becomes greater than n. This way, the stop condition must be False when it happens and True otherwise.

Here is what the syntax looks like in Python:

12
while condition: instructions
copy

Let's code the factorial task in Python. The body instructions are two lines of code:

  • basic operations like multiplying the result by the new number.
  • counter change.
1234567
n = 6 i = 1 # counter res = 1 # result variable while i<=n: res *= i i += 1 print(i, res)
copy

i is a multiplier which takes all values from 1 to n. It increments by 1 on each iteration. The stop condition is i<=n which is True at the beginning and becomes False when i==n+1. After that, the instructions are over, and the values of i and res are displayed in the console. Now i == n+1.

Завдання

The task is to find all positive numbers that are multiples of 3 less than 20. We'll do that using loops: starting with num = 0 (0 is a multiple of 3), insert num into the list numbers and then increment the num by 3. Insert the next value and so on until num <= 20.

Output the values in the numbers list.

Завдання

The task is to find all positive numbers that are multiples of 3 less than 20. We'll do that using loops: starting with num = 0 (0 is a multiple of 3), insert num into the list numbers and then increment the num by 3. Insert the next value and so on until num <= 20.

Output the values in the numbers list.

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

Все було зрозуміло?

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

Simple While Loop

The While statement is one of the basic programming concepts. It repeatedly executes the piece of code.

Imagine you have a task: write the code to calculate the factorial of the given number. For example, for n = 6 the result is: res = 1*2*3*4*5*6 = 720.

The step by step solution is:

Each operation is a new line of code. How many lines are needed? For huge numbers, you'll get very long instructions.

Instead, we'll use the loops. Take a look at how it works:

There are two main parts: stop condition and body. Body are instructions that need to be executed several times. For the factorial task, the body is multiplication the res by the next number. The condition helps to know when to stop.

If a condition is True, the body is executed one more time. When the condition becomes False, the loop is over and we leave it.

In the factorial task, we multiply all numbers from 1 to n so the loop is over when the multiplier becomes greater than n. This way, the stop condition must be False when it happens and True otherwise.

Here is what the syntax looks like in Python:

12
while condition: instructions
copy

Let's code the factorial task in Python. The body instructions are two lines of code:

  • basic operations like multiplying the result by the new number.
  • counter change.
1234567
n = 6 i = 1 # counter res = 1 # result variable while i<=n: res *= i i += 1 print(i, res)
copy

i is a multiplier which takes all values from 1 to n. It increments by 1 on each iteration. The stop condition is i<=n which is True at the beginning and becomes False when i==n+1. After that, the instructions are over, and the values of i and res are displayed in the console. Now i == n+1.

Завдання

The task is to find all positive numbers that are multiples of 3 less than 20. We'll do that using loops: starting with num = 0 (0 is a multiple of 3), insert num into the list numbers and then increment the num by 3. Insert the next value and so on until num <= 20.

Output the values in the numbers list.

Завдання

The task is to find all positive numbers that are multiples of 3 less than 20. We'll do that using loops: starting with num = 0 (0 is a multiple of 3), insert num into the list numbers and then increment the num by 3. Insert the next value and so on until num <= 20.

Output the values in the numbers list.

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

Все було зрозуміло?

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

Simple While Loop

The While statement is one of the basic programming concepts. It repeatedly executes the piece of code.

Imagine you have a task: write the code to calculate the factorial of the given number. For example, for n = 6 the result is: res = 1*2*3*4*5*6 = 720.

The step by step solution is:

Each operation is a new line of code. How many lines are needed? For huge numbers, you'll get very long instructions.

Instead, we'll use the loops. Take a look at how it works:

There are two main parts: stop condition and body. Body are instructions that need to be executed several times. For the factorial task, the body is multiplication the res by the next number. The condition helps to know when to stop.

If a condition is True, the body is executed one more time. When the condition becomes False, the loop is over and we leave it.

In the factorial task, we multiply all numbers from 1 to n so the loop is over when the multiplier becomes greater than n. This way, the stop condition must be False when it happens and True otherwise.

Here is what the syntax looks like in Python:

12
while condition: instructions
copy

Let's code the factorial task in Python. The body instructions are two lines of code:

  • basic operations like multiplying the result by the new number.
  • counter change.
1234567
n = 6 i = 1 # counter res = 1 # result variable while i<=n: res *= i i += 1 print(i, res)
copy

i is a multiplier which takes all values from 1 to n. It increments by 1 on each iteration. The stop condition is i<=n which is True at the beginning and becomes False when i==n+1. After that, the instructions are over, and the values of i and res are displayed in the console. Now i == n+1.

Завдання

The task is to find all positive numbers that are multiples of 3 less than 20. We'll do that using loops: starting with num = 0 (0 is a multiple of 3), insert num into the list numbers and then increment the num by 3. Insert the next value and so on until num <= 20.

Output the values in the numbers list.

Завдання

The task is to find all positive numbers that are multiples of 3 less than 20. We'll do that using loops: starting with num = 0 (0 is a multiple of 3), insert num into the list numbers and then increment the num by 3. Insert the next value and so on until num <= 20.

Output the values in the numbers list.

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

Все було зрозуміло?

The While statement is one of the basic programming concepts. It repeatedly executes the piece of code.

Imagine you have a task: write the code to calculate the factorial of the given number. For example, for n = 6 the result is: res = 1*2*3*4*5*6 = 720.

The step by step solution is:

Each operation is a new line of code. How many lines are needed? For huge numbers, you'll get very long instructions.

Instead, we'll use the loops. Take a look at how it works:

There are two main parts: stop condition and body. Body are instructions that need to be executed several times. For the factorial task, the body is multiplication the res by the next number. The condition helps to know when to stop.

If a condition is True, the body is executed one more time. When the condition becomes False, the loop is over and we leave it.

In the factorial task, we multiply all numbers from 1 to n so the loop is over when the multiplier becomes greater than n. This way, the stop condition must be False when it happens and True otherwise.

Here is what the syntax looks like in Python:

12
while condition: instructions
copy

Let's code the factorial task in Python. The body instructions are two lines of code:

  • basic operations like multiplying the result by the new number.
  • counter change.
1234567
n = 6 i = 1 # counter res = 1 # result variable while i<=n: res *= i i += 1 print(i, res)
copy

i is a multiplier which takes all values from 1 to n. It increments by 1 on each iteration. The stop condition is i<=n which is True at the beginning and becomes False when i==n+1. After that, the instructions are over, and the values of i and res are displayed in the console. Now i == n+1.

Завдання

The task is to find all positive numbers that are multiples of 3 less than 20. We'll do that using loops: starting with num = 0 (0 is a multiple of 3), insert num into the list numbers and then increment the num by 3. Insert the next value and so on until num <= 20.

Output the values in the numbers list.

Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Секція 1. Розділ 1
Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
We're sorry to hear that something went wrong. What happened?
some-alt