The first 14 days are labelled as beginner level. And so far I have certainly not learned any new concepts, it is all about the basics of programming really: variables, conditional statements, loops, functions and the like. Still, most of what I learned so far came from the daily programming projects. During those projects I rediscovered the random.choice() function in python as a simple way to select a random element from a list. I finally learned to appreciate the power of f-strings in python and I learned also a bit about ascii art and was catapulted back in time to when I learned about Gauss’s formula for the sum of integers. Another thing I learned was a nice trick to cycle through a list. A typical problem with lists is that when you try to index it and ask for an element at an index that doesn’t exist, like in somelist[n] which should retrieve the element in somelist at position n, then you get an index out of range error. In one project the index to pick was defined as n % len(somelist), with % being the modulo operator. This always returns an existing index. len(somelist) returns the number of elements in the list which means the index goes from 0 to len(somelist) -1. By applying this n % len(somelist) structure you always get a number back in the allowed range. If you are not familiar with the modulo operator, this gives you the remainder of the devision: for example 7 % 3 gives the remainder of 7 divided by 3, being 1.
Leave a comment