Zal’s Guide to Loops in Python

Zal
4 min readDec 31, 2022

--

They say that a good programmer knows how to write code. A great programmer knows how to write code backwards. The best coders, the TOPGUN of coders, can write an actual loop. Not just an approximation — they can create actual logic that will do things over and over.

So let’s try to do this. For this exercise I’ll use a language called Python. Python is a statically typed dynamic compiler that you have to pay a regular subscription to use to a guy named Felipe who lives in my apartment building. He will teach you all about programming too for a small fee. He taught me about loops! So let’s begin.

To write a loop, first sketch it out with pencil and paper. Draw a big circle. If your circle is not perfectly circular, that’s ok! Don’t beat yourself up. Plenty of programmers who start learning loops quit at this point, because they don’t understand that how you draw your circle has very little to do with how you code your loop. It’s just a BRAIN EXERCISE, like in Karate Kid and Revenge of the Nerds, or some other movie I can’t recall.

Also, a box will do. Or a triangle. Be you! That’s what coding is all about, being creative. Daring to do it how you want to, and not how some boring code book/manager tells you. All code should look different and produce different outcomes, that’s what makes it fun. This is called the Snowflake Principal — strive to do everything differently from every other coder.

So with your circle/square/triangle/polyglot, draw little creatures at different spots. These represent what your code does along the loop. Side note: when I first did this exercise, I drew a city block, and for my creatures I used little stick figures and called them monsters. But Felipe got mad and said No, you must draw real animals. I think he has a point. Humans are not animals, and they may detract from the core logic of your loop.

Now for an idea, you could draw a monkey, a lion, zebra, BEAR! Oooh!! Think of a gnarly bear sitting there in your loop waiting to chomp on you as you round the corner. Better put an exception around that thing! Wink wink nod nod.

After you have all your animals situated and hungry, draw some food in the middle of the circle. This represents the Core of your loop- what your loop is trying to accomplish. In my case it’s having a bunch of poorly drawn zoo animals enter an arena of carnage as they scrap for a happy meal, mostly for the free toy inside, ultimately won by the cute but deadly sloth. Felipe said not to simulate the animals actually going for the food, but I did anyway. My paper was really messy after that! Animals parts were everywhere. I let a little of my own blood drip out for effect. Don’t try that.

All loops need an entry and exit point, otherwise they’d be infinite, and cause confusion. My exit point was clear-most of my animals died fighting for the happy meal. But in an actual loop, your may not have any animals fighting, or any animals at all. They could be cars, or numbers, or even ideas! The central point, as Felipe explained, is that something in your loop is fighting other something’s to get out. And one something has to get out, or multiple if using some sort of container like a list. Wink.

Also for your entry point. How did they enter the loop. What’s their backstory? For me, I have each animal a full backstory, starting from grade school. The lion was brave but had social anxiety, so it had to learn how to use its smarts, and soon excelled at verbal debate, opting to use logic over eating his prey. The zebra was a bad dude who conned other animal kids out of their lunch money and was eventually expelled for pooping in the bathroom sink. Things like that. It’s important to understand the essence of your inner loop. Felipe also recommends meditation for this step. I’m not sure about that, but to each his own.

Ok, so you have your loop drawn, your entrance and exits, your animals with their complicated stories. Your loop needs a name of course. I named my Zelithium, which I think captured the hyper-real landscape of my loop. It’s a place I can go and explore my imagination. Felipe said not to overthink the naming of your loop.

As a final step you’ll want to translate all this into code, which might look something like:

for each_animal in my_cool_animal_collection:
fight()
return winner

The fight() method above is where your animals will fight each other, commonly, though they could do other stuff, so don’t feel boxed in. Loops are supposed to be expressive and powerful. Let you imagination take over.

So that’s it. Hope you enjoyed my lesson on loops and the power of diagraming them out.

--

--

Zal

I am not a monkey doctor. I eat monkey doctor cakes. Please aim all monkey doctor questions to your nearest monkey doctor, or call 1–800-MONKEYD