Choosing a data structure
A data structure is a way to arrange information so that a task is simple to perform. A list keeps an order. A dictionary connects a key to a value. A set remembers whether a value has appeared.
Do not start with the most advanced structure. Start by asking what the program needs to do often: keep order, look up a value, or avoid duplicates.
That question matters because the wrong structure can make a solution harder to read without making it any better. The right structure usually follows the task naturally.
Pause after running the example and explain each line in your own words. Notice what the program reads, what it stores, and what it prints, then make one small change and run it again to see exactly what changed.
Practice
Work through the same idea a few times so it starts to feel familiar. Begin by copying the example, then make the single change the instruction asks for, and finally explain to yourself why the result changed.
Your program should be an exact copy of the runnable example above, including any indentation.Keep the program structure the same. A correct attempt has one changed value and one short comment about its effect.# I expect: ...
# Then run the program and compare that comment with the real output.A correct solution uses the central idea from this lesson, prints one result, and stays small enough to trace line by line.Linear search
Linear search checks one item at a time from the beginning. It is a useful first solution because it is easy to trace and works on any list, sorted or unsorted.
The index changes on each pass through the loop. A common mistake is returning False after checking only the first item; the return belongs after the loop when no match was found.
This is the kind of solution you often write before you optimize. It gives you a correct baseline and helps you understand the problem before you try anything more advanced.
Pause after running the example and explain each line in your own words. Notice what the program reads, what it stores, and what it prints, then make one small change and run it again to see exactly what changed.
Practice
Work through the same idea a few times so it starts to feel familiar. Begin by copying the example, then make the single change the instruction asks for, and finally explain to yourself why the result changed.
Your program should be an exact copy of the runnable example above, including any indentation.Keep the program structure the same. A correct attempt has one changed value and one short comment about its effect.# I expect: ...
# Then run the program and compare that comment with the real output.A correct solution uses the central idea from this lesson, prints one result, and stays small enough to trace line by line.Counting with dictionaries
A dictionary can count repeated values. Each word becomes a key and its count becomes the value. This pattern appears in anagrams, frequency questions, duplicate checks, and many interview questions that ask for a summary of the input.
Use .get(word, 0) when a key might not exist yet. It gives a starting count of zero instead of raising a KeyError.
Once you see this pattern, you will notice it again and again. It is one of the clearest examples of how a dictionary can turn repeated items into useful information.
Pause after running the example and explain each line in your own words. Notice what the program reads, what it stores, and what it prints, then make one small change and run it again to see exactly what changed.
Practice
Work through the same idea a few times so it starts to feel familiar. Begin by copying the example, then make the single change the instruction asks for, and finally explain to yourself why the result changed.
Your program should be an exact copy of the runnable example above, including any indentation.Keep the program structure the same. A correct attempt has one changed value and one short comment about its effect.# I expect: ...
# Then run the program and compare that comment with the real output.A correct solution uses the central idea from this lesson, prints one result, and stays small enough to trace line by line.Stacks
A stack keeps the most recently added item at the top. In Python, a list can act as a stack with append and pop, which makes the structure easy to test quickly.
Before calling pop, check that the stack has an item. Trying to pop from an empty list is a common source of errors in bracket-matching problems.
Stacks show up in more places than bracket checking. They are also useful when you need to remember recent decisions or reverse a sequence of actions.
Pause after running the example and explain each line in your own words. Notice what the program reads, what it stores, and what it prints, then make one small change and run it again to see exactly what changed.
Practice
Work through the same idea a few times so it starts to feel familiar. Begin by copying the example, then make the single change the instruction asks for, and finally explain to yourself why the result changed.
Your program should be an exact copy of the runnable example above, including any indentation.Keep the program structure the same. A correct attempt has one changed value and one short comment about its effect.# I expect: ...
# Then run the program and compare that comment with the real output.A correct solution uses the central idea from this lesson, prints one result, and stays small enough to trace line by line.Two pointers
Two pointers are two positions that move through a sequence. They are helpful when the answer depends on items near both ends, such as a palindrome check, or when you want to compare two moving positions instead of every possible pair.
Give each pointer a clear job. Here, left moves right and right moves left after a successful comparison. Stop when they meet or cross.
The strength of this pattern is that it narrows the work quickly while still staying easy to trace by hand.
Pause after running the example and explain each line in your own words. Notice what the program reads, what it stores, and what it prints, then make one small change and run it again to see exactly what changed.
Practice
Work through the same idea a few times so it starts to feel familiar. Begin by copying the example, then make the single change the instruction asks for, and finally explain to yourself why the result changed.
Your program should be an exact copy of the runnable example above, including any indentation.Keep the program structure the same. A correct attempt has one changed value and one short comment about its effect.# I expect: ...
# Then run the program and compare that comment with the real output.A correct solution uses the central idea from this lesson, prints one result, and stays small enough to trace line by line.Tracing an algorithm
Before optimizing, trace the algorithm with a tiny input. Write the value of each variable after every loop step. This makes off-by-one mistakes easier to see and helps you notice where your expectation and the code diverge.
This small explorer is not Python execution. It is a way to practice reading the steps a program takes before you write one yourself.
If you can explain a solution with pencil-and-paper steps, you are in a much better position to improve it or debug it later.
- value is below 5
- take the else branch
Pause after running the example and explain each line in your own words. Notice what the program reads, what it stores, and what it prints, then make one small change and run it again to see exactly what changed.
Practice
Work through the same idea a few times so it starts to feel familiar. Begin by copying the example, then make the single change the instruction asks for, and finally explain to yourself why the result changed.
Your program should be an exact copy of the runnable example above, including any indentation.Keep the program structure the same. A correct attempt has one changed value and one short comment about its effect.# I expect: ...
# Then run the program and compare that comment with the real output.A correct solution uses the central idea from this lesson, prints one result, and stays small enough to trace line by line.