Python – loops (for, while, break, continue)


Loops in Python are pieces of code that repeat multiple times.

range(start,stop,step) – this is how the forLoop loop looks in general, where start and stop describe the actual beginning and end of the loop, including the start point, but not including the end point; step – the step with which the computer moves from the start point to the end point (another step is called increment).

Example:
range(1,10,2) means 1, 3, 5, 7, 9

To define the loop itself, use the for function:

for cat in range(1,10,2):
….print(cat)

If no step is specified, the default step is 1.

The loop can take both numbers and strings as an argument. Let’s take an example where the length of a string is used as an argument:

cat = “Mew dear friend!”
for i in range(len(cat)):
….print(cat[i])

As a result, we will see the listed letters of the expression “Mew dear friend!”

We get a similar result if we write the following expression:

cat = “Mew dear friend!”
for myav in cat:
….print(myav)

Cycles can be combined thanks to tabulation:

for cat in range (1):
….for dogs in range (3):
……..print (dogs)
0 1 2 # result


This entry was posted in Python (en). Bookmark the permalink.

Leave a Reply

🇬🇧 Attention! Comments with URLs/email are not allowed.
🇷🇺 Комментарии со ссылками/email удаляются автоматически.