LearnCodeVisually

Learn Python Programming Visually

See how Python values and program state change one step at a time. Connect short code samples to visual explanations and practice questions.

Browse the free interactive course →

1. Read and run Python code

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.

Start with Python Hello World and the variables and data types module.

2. Understand decisions

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.

Explore conditional logic and comparisons interactively.

Python loop example: build a total

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.

Explore Python loops →

3. Lists and indexing

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.

Compare Python and C examples in the arrays lesson.

4. Functions and reusable code

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.

Open the functions lesson to compare examples.

Practice learning Python

  1. Predict the output of a short program.
  2. Change a variable and trace what happens.
  3. Test a loop with zero, one and multiple iterations.
  4. 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.

def count_passes(scores, pass_mark=50):
    passed = 0
    for score in scores:
        if score >= pass_mark:
            passed += 1
    return passed

print(count_passes([42, 50, 73, 49]))  # 2
print(count_passes([42, 50, 73, 49], 70))  # 1

assert count_passes([]) == 0
assert count_passes([49]) == 0
assert count_passes([50]) == 1
assert count_passes([51]) == 1
assert count_passes([50, 50]) == 2

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

  1. Use a pass mark of 60 with [59, 60, 61]. Predict the result.
  2. Change >= to >. Which existing test fails?
  3. 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.

Continue with the interactive unit testing lesson. Language reference: Python's official tutorial on control flow.