Course Content
Python Loops
Python 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:
while condition: instructions
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.
n = 6 i = 1 # counter res = 1 # result variable while i<=n: res *= i i += 1 print(i, res)
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
.
Task
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.
Thanks for your feedback!
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:
while condition: instructions
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.
n = 6 i = 1 # counter res = 1 # result variable while i<=n: res *= i i += 1 print(i, res)
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
.
Task
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.
Thanks for your feedback!
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:
while condition: instructions
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.
n = 6 i = 1 # counter res = 1 # result variable while i<=n: res *= i i += 1 print(i, res)
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
.
Task
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.
Thanks for your feedback!
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:
while condition: instructions
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.
n = 6 i = 1 # counter res = 1 # result variable while i<=n: res *= i i += 1 print(i, res)
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
.
Task
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.