The break statement in Python

The execution of a loop can be interrupted using the break instruction.

Let us have a list lst:

pythonlst = [1, 2, 3, 4, 5]

Let's output all elements from it up to the number 3, at which the execution of the loop will be interrupted. To do this, under the block with the condition, we write the instruction break:

pythonfor el in lst:
  print(el) # 1, 2, 3 if el == 3:
    break

The break instruction can terminate any loops: for, while.

Задание 1

Given a set:

pythontst = {1, 3, 6, 7, -9, 12}

Print its elements up to the first negative number.

Задание 2

Given a list:

pythontst = [7, 1, 2, 5, 0, 3, 9]

Find the sum of the elements of this list up to the first zero.

Задание 3

Given a number:

pythontst = 897654

Form a list from it up to the number 6.