Loops in Python

Do not forget to take a look at the previous article conditional sentences in Python to learn conditional sentences and how to use them, and take into account the distances (displacements) while writing in the language of Python.

loops in python

The loops in Python loops in python are divided into two important parts

for loop
while loop
Using the loop loop in Python (while loop in Python)
The use of the code remains within the loop loop as long as the specified condition is correct

a = 1
: while a <5
(print (“a =”, a
a + = 1
The preceding example illustrates the value of variable a being 1, and each time we verify that the value of variable a is less than 5 or not.

If less than 5, type the value of a each time and then increase the value of a by one.

Read also: The most important programming languages ​​currently used

Stop loops in Python (stop loops in python)

To turn off loops in Python you may need to turn off all the iterations, or skip a certain time of frequency.

In Python to stop redundancy provided that a condition equals true we use the phrase break
a = 1
: while a <5
(print (“a =”, a
if (a == 3
break
a + = 1
In Python to override a certain time during repetition due to a condition equal to true we use the ”
i = 0
: while i <6
i + = 1
: if i == 3
continue
(print (i
The above example illustrates the value of variable i being 1, and each time we check that the value of variable a is less than 6 or not.

If less than 6, increase the number of i by one and print.

In this case, check the value of the variable i if you reached 3, skip the commands after that, ie do not print i.

Use of the loop in Python
From the above we conclude that while loop is bound to meet a certain condition and therefore if it is true, the codes begin under the while.

But what about the loop? The loop is used for repetition and movement of items within lists, rows, groups, and dictionary. Thus, a set of commands can be executed on each item individually.

Using the loop for the while loop An element is initially defined so that we use it in the condition but here in the loop it is not inevitable. Here’s an example:

: “for x in” DoratOnline
(print (x
In this example, each character is printed in the word “DoratOnline” which means that it is an amount passed over each letter and printed it and we did not need to define a particular element depends on the for loop.

Stop loops in Python (stop loops in python)
We have already stopped loop loops in Python through the while loop and now we will do this through for loop.

[“fruits = [” apple “,” banana “,” cherry
: for x in fruits
(print (x
: if x == “banana
break
In this example, we repeat the repetition on the list list and we print each item, but once we get to the banana element, the break statement appears and the for loop stops completely.

But what we want is to exclude only one item from printing a banana which is in the middle of the list list Here’s an example

[“fruits = [” apple “,” banana “,” cherry
: for x in fruits
: if x == “banana
continue
(print (x
Using the range function with the for loop in Python
What if you want to print the numbers from 0 to 6, do you make a list or group to contain the numbers in Python?

But what if you want to start from a certain number to a certain number you will write (3.6) range and so will start from 3 to 5
If you want to increase by 3 digits instead of 1 we write (1,20,3) range will start from 1 to 19 and each time will increase 3 and not 1.
: (for x in range (6
(print (x