Recommended resources
The freely available resources this lesson will direct you to.
- Python Tutorial for Beginners 6: Conditionals and Booleans - If, Else, and Elif Statementswww.youtube.com
- User input in Python is easy + exercises ⌨️www.youtube.com
- 4. More Control Flow Tools — Python 3.14.0 documentationdocs.python.org
- Lecture 2 - CS50's Introduction to Programming with Pythoncs50.harvard.edu
Welcome! This first module refreshes the Python tools you’ll use throughout data structures and algorithms: taking input, storing values, making decisions, repeating work, and organizing logic into functions. You already know the basics of loops, functions, and lists; today’s goal is to combine them into a complete program rather than study each in isolation.
We’ll build a program that reads a list of integers and a threshold, then prints the sum of the values at least as large as the threshold. This is a small task, but it has the same shape as many algorithm problems: understand the input, process each value, and produce one answer. Plan on about 35 minutes, which you can split across two study sessions.
Start with the input contract
Our program expects three lines:
- The number of integers,
n. - Those integers, separated by spaces. This line is blank when
nis zero. - The threshold.
For example:
5
4 9 -2 7 9
7
The qualifying values are 9, 7, and 9, so the program should print 25. If the second line contains a different number of integers from n, it will print an error message instead. For now, assume each supplied number is a valid integer.
The first input detail to get right is that input() returns text, even when the user types digits.
User input in Python is easy + exercises ⌨️
Watch User input in Python is easy + exercises by Bro Code for its short demonstration of capturing input and converting numeric text.
Watch capturing input to see where an entered value goes. Then watch numeric conversion; focus on why arithmetic needs int(...) or float(...) around text returned by input(). Our program uses int(...) because its inputs are whole numbers.
For a line containing several numbers, we can split the text at whitespace and convert each piece:
numbers = []
for token in input().split():
numbers.append(int(token))
If the line is 4 9 -2 7 9, split() gives us the text pieces "4", "9", "-2", "7", and "9". The loop converts each piece before appending it, so numbers ends up as a list of integers, not a list of strings. The empty input line produces an empty list.
Decide what happens for each value
We need one running total. It starts at zero; as the loop visits each number, a condition decides whether that number contributes to the total.
4. More Control Flow Tools — Python 3.14.0 documentation
Read the Python tutorial’s introduction to conditional branches and loops. It supplies the control-flow pieces we need for processing a list.
In Section 4.1, “if Statements,” read conditional branches, from the opening integer-input example through the explanation of elif and else. Then, in Section 4.2, “for Statements,” read sequence iteration and the short words list example immediately beneath it. Focus on the fact that a for loop visits the list’s items in order; you do not need an index when you only need each item’s value.
The processing logic can now be written independently of input and output:
total = 0
for value in numbers:
if value >= threshold:
total += value
total is an accumulator: a variable that holds the result built so far. On every iteration, value is one integer from the list. The condition value >= threshold includes values equal to the threshold. A value that fails the condition leaves total unchanged.
Keeping this logic separate makes it easier to check. It should work whether the list has five values, one value, or no values.
Put the logic in a function
A function lets us name the calculation and keep it apart from the code that reads and prints. That separation will be useful later when you test algorithms with different inputs.
4. More Control Flow Tools — Python 3.14.0 documentation
Continue in the Python tutorial to see how a function receives arguments and returns a result. You only need the basic pattern, not the details of the Fibonacci calculation used in its examples.
In Section 4.8, “Defining Functions,” read function definitions, beginning at the first fib example. Later in the same section, read return and append, beginning at the fib2 example and ending with the explanation of append(). Focus on def, parameters, indentation, return, and adding an item to a list; you can skip the discussion of function scope between those examples.
Here is the complete program. You can save it in a Python file and run it with the three-line input shown earlier.
def sum_at_least(numbers, threshold):
total = 0
for value in numbers:
if value >= threshold:
total += value
return total
def main():
n = int(input())
numbers = []
for token in input().split():
numbers.append(int(token))
threshold = int(input())
if len(numbers) != n:
print("Invalid input: expected", n, "numbers")
return
answer = sum_at_least(numbers, threshold)
print(answer)
main()
Notice the jobs assigned to the two functions. main() handles the input format, checks the stated list length, and prints the result. sum_at_least() receives an already-built list and returns the calculation; it does not need to know how the numbers were entered. In main(), the early return stops the program from calculating an answer after a length mismatch.
Trace and check the program
Tracing a small example is often faster than guessing why code does or does not work. With numbers = [4, 9, -2, 7, 9] and threshold = 7, the accumulator changes as follows:
| Value visited | Qualifies? | Total afterward |
|---|---|---|
4 |
No | 0 |
9 |
Yes | 9 |
-2 |
No | 9 |
7 |
Yes—equality counts | 16 |
9 |
Yes | 25 |
The function returns 25, and main() prints it. Three other cases show why the exact condition and starting value matter:
- With no integers, the loop runs zero times and the sum is
0. - If no value meets the threshold, the sum is also
0. - With
numbers = [-8, -3, 2]andthreshold = -4, the qualifying values are-3and2, so the result is-1. A running sum need not be positive.
When checking your own code, compare the program’s output with a hand trace like this one. Also distinguish a valid empty list (n is zero and the second line is blank) from a length mismatch (n says five but the second line supplies only four integers).
The key pattern from this lesson is to parse input into useful values, process a list with a loop and condition, and return a result from a function. The input code and calculation code have different jobs, which makes each easier to understand and check. Next, you’ll work more closely with strings—the text that input() gives you—using indexing, slicing, iteration, and common string methods.