NEBians
search Sign In Register

Lists & Functions

Lesson 3 of 4 Coding game schedule20 min

Loading simulation…

sports_esportsPlay with the blocks and run your code

flagWhat you'll discover

  • arrow_forwardCreate lists and read items by index
  • arrow_forwardGrow lists with append() and measure them with len()
  • arrow_forwardDefine functions with def, parameters and return
  • arrow_forwardWrite a biggest() function that works on any list

Lists: many values, one name

A list holds many values in order: scores = [72, 95, 88]. Each item has a position called an index, counting from ZERO: scores[0] is 72 and scores[2] is 88. Negative indexes count from the end — scores[-1] is the last item, a beloved Python shortcut. Add new items with scores.append(100), count them with len(scores), and loop through them with for s in scores:. Lists are Python's workhorse — marks, players, messages, sensor readings — anything plural lives in a list.

def: building your own commands

You have used built-in functions like print() and len(); def lets you create your own. Write def biggest(numbers): then an indented body, and end with return to send the answer back. Parameters make functions flexible — biggest works on ANY list you pass it, not one specific list. The classic pattern for finding a maximum: start with best = numbers[0], loop through the list, and whenever an item beats best, replace it. Three lines that feel like magic the first time they work.

Surviving the secret tests

Neby checks your functions by secretly running extra code: print(biggest([3, 41, 7])) must say 41, and print(biggest([-9, -2, -7])) must say -2. That negative test catches a sneaky classic bug: starting best = 0 works for positive lists but fails when every number is below zero — the correct start is the list's own first item. Thinking about edge cases like this is precisely what separates code that seems to work from code that truly works.

quizCheck your knowledge

1. With scores = [10, 20, 30], what is scores[1]?
2. Why should biggest() start with best = numbers[0] instead of best = 0?
3. What does scores.append(50) do?