Mathspp Blog

342 391,958 32,910
articles words lines of code

Nowadays stores come up with all sorts of funky promotions to catch your eye... But how much money do you actually save with each type of promotion?

The walrus operator := can be really helpful, but if you use it in convoluted ways it will make your code worse instead of better. Use := to flatten a sequence of nested ifs or to reuse partial computations.

There is a key hidden in one of three boxes and each box has a coin on top of it. Can you use the coins to let your friend know where the key is hiding?

In Python, if you are doing something that may throw an error, there are many cases in which it is better to "apologise than to ask for permission". This means you should prefer using a try block to catch the error, instead of an if statement to prevent the error.

How should you unpack a list or a tuple into the first element and then the rest? Or into the last element and everything else? Pydon't unpack with slices, prefer starred assignment instead.

Five sailors and their monkey were washed ashore on a desert island. They decide to go get coconuts that they pile up. During the night, each of the sailors, suspicious the others wouldn't behave fairly, went to the pile of coconuts take their fair share. How many coconuts were there in the beginning..?

The "Zen of Python" is the set of guidelines that show up in your screen if you import this. If you have never read them before, read them now and again from time to time. If you are looking to write Pythonic code, write code that abides by the Zen of Python.