Skip to content
CodeItRaw
Basics

Lesson 6/7

Sometimes you want to stop the moment you find what you are after. return works in the middle of a loop too: it ends the function there and then. Most tasks with the word "first" are solved this way.

for i in range(len(values)):
if values[i] == key:
return i
return -1
len(values) is the number of elements; range(len(values)) counts from 0 to the last index, so you have both the position (i) and the value (values[i]). If the loop ends and you have not returned, what you wanted is not there.
out = []
for v in values:
if v % 2 == 0:
out.append(v)
return out
When the answer is a list, start with an empty one ([]) and add the elements that qualify to its end with append. The order keeps itself.

Tasks

Tasks open in order. Solve them all and the next lesson opens.

This lesson's tasks open when the lessons before it are finished. You can read the explanation now.Go to the current lesson

  1. 01

    First Sighting

    Function

  2. 02

    Collect the Evens

    Function

If you would rather not wait for the order, every problem is open without locks: Problem list