Skip to main content

A Hundred Days of Code, Day 006 - Starting and Quitting Comprehensions

I know I said, I’d start with the basics, Reuven, but please forgive me this once :)
Will do the rest, bottom up :)

Have started up with this, because I’m fascinated with how folks manage to build in so much functionality into short, easy to read statements.
To my mind, comprehensions are the pithy proverbs of the programming world.
So here goes …

Notes

  • This style of writing code, comes from functional programming
  • Functional Programming is another style of writing code
    • It assumes that data is immutable. (Or at least it treats it as such) It does not change the originals I work with. (I assume this means that I always work on a copy or something)
    • we work with sequences of data and create new ones based on the old ones (changing case of a sentence, comes to mind)
    • There are a bunch of techniques/patterns/heuristics that’ll let me do that
    • We can treat functions as data and pass them around as arguments to other functions.
    • The functools module assists with writing such code
  • Why code in such a manner? Some benefits include them being easier to read, easier to debug, shorter, more understandable.
  • Distributed Computing / Multiprocessing across processors and cores/threads makes these techniques more relevant in this day and age
  • My personal need is to write easy to read code, like when I see a well crafted list comprehension
  • If I want to square a list of numbers, my current go to is to generate a new list, using a for loop. Comprehensions are a better way of doing this
  • For loops are far when I want to execute something on each item of something iterable, like a list or a dictionary. when i want to have a statement happen
  • If just want to transmute the elements of list into something else, change each element, like square them all above, then comprehensions are my friend
  • Here’s a list comprehension to square a list of numbers from the list numbers.
    • [one_number * one_number for one_number in numbers]
    • The square brackets make it a list comprehension. If I want dictionary comprehensions, I would abut them with curly braces {}. And if I use (), I’ll get a generator expression (which I know only the bare basics about)
    • For loops and comprehensions might look similar, but they really are different
  • Note: whatever expression you use to generate output has to return something. like the square expression above . because what I want to do is catch all my results in a container. functions (like the print function) that return None as their result, cannot work here. They will display stuff, but store only None in our comprehension container :)
  • I can also write the comprehesion over multiple lines (works because they are bounded by the list brackets here or bounded by other brackets elswhere)
[one_number * one_number         
 for one_number in numbers]
  • here with the breakdown of the lines, the first line becomes my output generator; the expression I want to use and the second line, the data I want to process. Much more readable this way

learning / feedback/ experiences from doing exercises

  • Exercises today required a bit of head scratching and a bit of research, but were relatively simple
  • I should keep in mind that the computer can go crazy too. I kept wondering why my code wouldn’t run and and kept banging my head for an hour. Even Rueven’s solution was what I did. Until I restarted my Jupyter note book and everything worked the way I expected.
  • Should have a time limit on how much struggling I do. From the last week, it looks like I am a real sucker for pain otherwise. Half an hour tops and then go look up the solution.

Have reached the second set of exercises and realised why Reuven told me to start with the basics. I failed with the second one in this set. I just know, there is a simple solution and I cannot seem to get it. This basically means … yes … that my foundations are creaky.
Note to self, walk before you can run. By Jove, you should’ve learnt that by now! :)
So this is me, waving goodbye sorrowfully to comprehensions.
Will start with the basic Python course tomorrow.
But I’ll be back soon enough, damn it!

Read all about my comprehensions journey here