Python uses indentation to group statements. You can print text with print(), bind names to values with assignment, and use functions from the standard library. Unlike C, basic Python variables do not require a type declaration.
A condition controls which block runs. Python uses if, elif and else. Ask yourself what the expression evaluates to before tracing the selected branch. For instance, score >= 50 is true when the score is 50 or higher.
The code below adds the numbers 1 through 4. Python's range(1, 5) includes 1 but excludes 5. The accumulator changes after each step.
total = 0
for number in range(1, 5):
total += number
print(number, total)
# Output:
# 1 1
# 2 3
# 3 6
# 4 10
Trace the changing total: 0 → 1 → 3 → 6 → 10. What would happen if the range ended at 4 instead of 5? Work it out on paper, then compare your prediction with a real run.
A Python list stores elements in order. Indexes start at zero, so in scores = [10, 20, 30], the expression scores[1] evaluates to 20. Lists can grow or shrink, unlike fixed-size arrays in C. Visualizing positions makes indexing easier to understand.
Functions name a set of operations and can return a result. A Python definition starts with def. Trace the values of arguments, local names and the returned value to understand a function call.
Test a loop with zero, one and multiple iterations.
Complete a quiz or assignment after reading a topic.
The site also includes interactive data structure visualizations. Some advanced lesson demonstrations are initially presented with C code or English explanations; use the language selector to find the available examples.
Worked workshop: count scores without losing your total
Suppose you have a list of quiz scores and want to count the scores at or above a pass mark. The input is a list of numbers and a numeric threshold; the output is a count. This exercise assumes the inputs are already numbers. It does not validate text entered by a user.
Separate the calculation from printing so you can reuse it and check it with tests. Each iteration either adds one or leaves the count unchanged. After processing any prefix of the list, the counter equals the number of qualifying scores in that prefix.
Save the code as count_passes.py and run python3 count_passes.py (or python count_passes.py if that is your Python 3 command). It prints 2 and 1 on separate lines. With a normal Python run, a failing assertion raises AssertionError; passing assertions print nothing. Do not use Python's optimization option when running these assertion checks.
Trace your own scores
Change the inputs to see how each score affects the counter. This browser demonstration models the Python loop; it does not execute Python or send your inputs to a server.
Find the mistake before looking at the answer
A learner puts passed = 0 inside the loop. For the input [80, 90, 40], their version returns 0. Explain why, then repair the indentation.
Explanation and repair
The counter resets for each score. The first two iterations each reach 1, but the last iteration resets it to 0 and does not increment it. Initialize the counter once before the loop, as in the working function above. Putting initialization inside the loop also leaves the counter undefined for an empty list.
Three changes to try
Use a pass mark of 60 with [59, 60, 61]. Predict the result.
Change >= to >. Which existing test fails?
Return the qualifying scores instead of their count. Which variable should become a list?
Worked answers
The first result is 2. Using > makes the tests containing 50 fail because equality no longer qualifies. To return scores, initialize passed = [], replace the increment with passed.append(score), and return that list. The original input then returns [50, 73]. The return type has changed, so the assertions must check lists, including [] for empty input.
When using an AI tutor, ask it to explain a failed prediction, then verify the explanation by tracing the values yourself. A plausible explanation is not a substitute for running boundary and empty-input tests.