Largest number
Finds the largest number in a list. Teaches lists, indexes and a condition inside a loop.
Open in Tokbit Level: Intermediate. Free in the browser, nothing to install, no account needed.
Run the example in Tokbit and step through it: before each step say what will happen and check it in the Variables panel. Then change it your way.
Flowchart
Python program
The same program as Python code. In Tokbit you see it when you switch from Diagram to Python above the flowchart.
"""Finds the largest number in a list. Teaches a list and comparing in a loop."""
numbers: list[int] = [3, 17, 5, 12, 9]
# Start with the first number: it is the largest seen so far.
largest: int = numbers[0]
for i in range(1, len(numbers)):
# A larger number replaces the maximum so far.
if numbers[i] > largest:
largest = numbers[i]
print("The largest number is", largest)
Follow-up tasks
Tasks in which you change the example (with a model solution):