Chapter 9: The Doggy-Nator and List Comprehensions

Start Here
Chapter 1: In which a cow surprises you

Chapter 2: The Time traveling cow

Chapter 3: Greedy banker strings

Chapter 4: More on strings and donkeys

Chapter 5: Crooks R Us Bank & Lists

Chapter 6: Orc functions

Chapter 7: Dictionaries, Locks, and Gobbledygook

Chapter 8: Python vs the Zombies

Chapter 9: The Doggy-Nator and List Comprehensions (This chapter)

Chapter 10: In which you Eval the Doggy


You finally meet the Doggy-Inator

You make your way to the mountain. Ayah-fyat-lah-yoh-kuh-tul. You know the words, join the song. As you are making your way to the fire, a shot rings out. Moo-Inator is knocked out. And out steps the main villain- Doggy-Inator.

The Doggy-Nator

Aside story totally unrelated to Python

You maybe wondering where I took the above photo from. You are not? You’d rather go back to learning Python? Well, tough, cause it’s my book.

The photo was taken in the Himalayas. We were in the middle of nowhere, and stopped for tea at a small restaurant. It wasn’t a five star- it was a hut with a oil stove in it. But the owner was quite nice, and his children were helping him (it was a Sunday).

Sitting outside the restaurant were two dogs. They looked completely out of place, as the breed isn’t really mountain dog. And they were very thin- you could see their bones. But they’d had this puppy (above), and the puppy was really fat, maybe because the kids loved to feed him, and the dogs only ate when the puppy was full.

While we eating, the dogs suddenly went wild. They started growling from the pit of their throats, and we thought they were going to attack us. But then they ran off into the woods, and had a loud fighting match with some wild animal. We never found out which animal it was, but the two skinny starving dogs became lions when their little puppy was threatened.

End of useless side story


The Doggy-Inator starts firing attacks at you.

doggy_attacks is a list which contains the amount of damage his attacks will do to you. You start off with a health of 100.

This is a form of the print statement. “%d” tells that you want to print a decimal (or a number). After the % symbol, you give the actual number you want to print. The most common symbols are %d for numbers and %s for strings. Another example:

Anyway, so the Doggy-Nator fires his attacks at you:

your_health -= d

The -= command means, subtract the value, and store it in the current variable. So this line means, take your current health, and subtract d (where d is the damage) from it. The overall effect is, your health goes down each time. To understand this better, print out your health each time:

Oh oh. You just lost 77% of your health. You can’t survive like this for long. Luckily, you are wearing Plot Armor. For those of you who don’t know what this is, Plot Armor is special protection the writer of the book/movie gives to the hero, so the hero always survives any blast, or a thousand bullets fired at him. That’s why the Storm Troopers can fire a thousand bullets at Skywalker, and still miss. The real truth is, they didn’t miss; he was wearing Plot Armor. Just like you are now. So instead of you, the Moo-Inator is hurt.

“No!” you cry out. “Please don’t die.”

“Don’t worry. According to the script, I don’t die until scene 57.”

He gives you his super secret weapon, the BFG 9000 Elder Wand (Made in China). “Use this, it will cancel out Doggy-Inators attacks.”

Now we will come to one of the most powerful techniques in Python: List Comprehension. First, you need to weaken the Doggy’s attacks, using your BFG thingy.

We have created a new variable with the weakened attacks, imaginatively called weak_attacks.

Now, let’s see what happens when the Doggy-Inator attacks us.

See what we did? We created a variable weaken, and used it to weaken the Doggy-Inator’s attacks ten times. So instead of causing us 77% damage, they only caused us 7%.

The key thing was that we ran a separate for loop to reduce the power of the attacks. Specifically, this one:

Python gives us a very powerful method to do this is a single line, called list comprehension.

This will require some explanation. List comprehensions are used when we are working with lists. One of the most common tasks is to take a list, do something with it, and create a new list. Because this is so common, an easy way to do this was created, rather than having to loop all the time.

[x / 10 for x in doggy_attacks]

This is the whole instruction. First, as you may know, lists are created by using the [] brackets. But in this case, we have put instructions in the [ ].

The simplest way to understand list comprehension is,

[ <instruction> for variable in list]

Python will automatically loop over the list, and run the <instruction> each time. In our example:

[x / 10 for x in doggy_attacks]

First, it will loop over doggy_attacks, and use the variable x.

[x / 10 for x in doggy_attacks]

The instruction here is x / 10. So that for each variable in the list, Python will divide it by 10. Since the whole command is in [ ], it will automatically create a list for you. Which is why

returns a list.

Leave a Reply