Iterate and Accumulate: The two skills to master in LC101 Unit 1

LC101 Unit 1 covers a whole lot of ground in six weeks.

I remember fondly our days of playing around with the Turtle module, remember less fondly the chapter on dictionaries, and remember even less fondly Boolean functions. (All this mathy stuff — I’m ready for the pretty fonts and interesting layouts of Unit 2!).


The point is: there’s a lot to learn in Unit 1 and if you’re starting from scratch (like me) it’s hard to know in the moment what is most critical.

Spoiler alert: iterate and accumulate are the most critical. If you take nothing else from Unit 1, take those two skills.

And if you need some help learning them, in this post you’ll find the basic guide to iterating and accumulating I wish I had while I was working through Unit 1. (Note: I assume in this post that you’ve got some basic knowledge of Python. If you don’t have that yet and want to learn, W3Schools has a good starter document here.)

Functional Functions: An Easy Python Template

Iterating and appending are useful in the context of functions, so it helps to know how to write one of those first.

It took me a while to get a feel for writing syntactically correct functions. Until I did, I had this screenshot always open on my computer:

functional function
Screenshot-ed from LaunchCode’s Runestone online text. Chapter 5.

Not only is this function useful for reminding yourself of where all the colons and tabs are supposed to go, it also shows you how to use the “main” function.

Helpful hint: Input and print statements go in the “def main():” portion of your code.

When you’re faced with a tough homework assignment and you’re not sure where to start, try starting with the basic form of a function and see what you can fill in. You can start with such basic questions as:

  • What should I name my function?
  • What information do I already know?
  • What does my problem want me to return? A list? A name? A value?

Alright, after you’ve got your basics down, it’s time to think about how your function will actually … ahem … function.

Iterate in Python

Here is every function ever:

  1. Take a thing
  2. Manipulate the thing
  3. Return a new thing

Often, the thing of step one will actually be a collection of things. It could be a list (a collection of anything – values, strings, etc.), a string (a collection of characters), a dictionary (a collection of key-value pairs), or a tuple (an ordered collection of anything – values, strings, etc.).

To manipulate each thing in the collection of things, you need iteration aka the ability to access each thing in turn.

In Python, we did this with for loops and while loops.

Here’s a basic for loop template:

for loop basic
Screenshot from LaunchCode’s Runestone text.

Some helpful facts about for loops:

  • Get thyself an accumulator. In the sum_to function above, the accumulator is “sum.” The programmer set it at zero to start, and as the function begins adding numbers it accumulates a sum. More info on this in the next section.
  • If you’re starting with a string, you can iterate through each character in the string without breaking the string up first. It looks like this:
for char in string.JPG
Screenshot from Launchcode.

And here’s a basic while loop template:

while loop template.JPG
Screenshot from Launchcode.

Accumulating, Appending, and Adding in Python

Whatever your problem wants you to end up with, you should start with an empty one of those. Get in the habit of doing this as soon as possible.

For example, if the wording of the problem says, “Blah blah blah and return a list of Halloween costume ideas” — you know that you’ll need to produce a list. Start with an empty one of those:


In the sum example from earlier, the accumulator is just a value, which the programmer set to begin at zero:


Or, you could have a problem that asks to return a string of, for example, vowels.


How to accumulate to a value or list

Lists, values, and dictionaries are mutable. That means they are easily changed.

There are a few ways to accumulate to a value or a list, one less pretty, one more pretty, and one that looks fancy.

The less pretty way looks like this:


The more pretty way:

princess 2.png

This fancy append method works on lists, not values:


How to accumulate to a string or tuple

Strings and tuples are immutable — not easily changed.

In order to change them, you’ll have to make a copy. A common example: you can “slice” strings and tuples and assign the result to a new string or tuple.

The numbers in brackets “sliced” the first string into a smaller string and assigned it a new name.

Helpful hint: If you find yourself needing to change a string, think about ways to get it into a list. As an example: each character in a string has an index value — a number that represents where it falls in the string. With the .index method you can easily create a list of each of these values. Now you have a mutable object to work with!

I could say more about tuples and string immutability, but I honestly don’t think I have quite a handle on it all myself yet. So, I’ll refer you to this document:

Strings, Lists and Tuples from Open Book Project

Final thoughts

It’s nice to think of learning as like architecture. The most beautiful buildings start with basic materials, arranged in unassuming ways, with the help of ugly scaffolding:

Lazy stock photo ftw.

This blog post is a part that ugly scaffolding (yay!).

Embrace the simplicity and functionality (and ugliness!) of the scaffolding you need right now: lists of methods, screenshots of basic functions, for loops boiled down to their constituent parts. Bricks and plywood don’t look terribly impressive either, sitting at their construction site.

Me and my LC101 comrades are starting Unit 2 in two days. A good number of the 120 original students will not be joining us. I think it was somewhere between weeks 4 and 6 that I myself turned to my boyfriend, mid-late night study session, and said, “I’m not sure I can finish this.”

I don’t know the reasons my classmates have dropped. But I know that if I had, it would have been because I didn’t trust myself to bridge the gulf between the little knowledge I began LC101 with and the end goal: writing code (that works), building websites, etc.

Every building starts with simple materials. Trust them!