Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
List basics
Lists with if statements and loops
Lists in functions
17. Lists as function arguments
Summary

Instruction

Excellent! Now, let's take a look at how we can use lists as function arguments. It is actually very intuitive!

def count_positive(input_list):
  counter = 0
  for number in input_list:
    if number > 0:
      counter += 1
  return counter

sample_list = [3, 5, -3, 7, 9, -2, 0]
# should return 4
count_positive(sample_list)

The function above counts how many numbers in a list are greater than 0, and returns that count. Note that input_list was declared just like any other argument type, and we used it inside the for loop just like any other list outside a function. Not really complicated, right?

Exercise

Write a function named count_even() that, when given an input list, returns the number of even numbers in the list.

Stuck? Here's a hint!

To check if a given number is even, use:

number % 2 == 0