Skip to content
CodeItRaw
Basics

Lesson 4/7

A list is a row of values in order: [3, -1, 0, 8]. Write for v in values and v takes the next element on each turn. You do not need to know how many there are; the loop ends when the list does.

count = 0
for v in values:
if v > 0:
count += 1
return count
The counter pattern: start at 0, add one for every element that meets the condition. count += 1 is short for count = count + 1.

The same pattern collects a sum: total += v. The only difference is what you add inside the loop: 1 when counting, the element itself when summing.

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

    Count the Positives

    Function

  2. 02

    How Many Are There?

    Function

  3. 03

    Sum of the Evens

    Code reading · Break the Code

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