At hackajob HQ, our internal engineers often debate which coding languages are best and for our back-end team, Python is a subject that comes up regularly. In fact, they believe that Python’s inbuilt modules make their lives as programmers so much easier. Below, we’ve captured a snapshot of their discussion regarding some of the tools available in Python, as well what you can achieve with them.
Going through an iterable to get various elements is part of what you’ll do in your day-to-day as a programmer. In particular, the Itertools module uses combinations, statistics and math to make this job easier. Below, we’ve outlined these methods in more detail so you can understand how to use them in Python.
Assume you have a list where A = [1,3,5,7,9] and you want to get as many possible unique combinations of ‘n’ numbers that can be derived from A. You're likely to do the following:
Note the use of 'list()'. This is necessary to get a human readable output as different combinations return a 'generator' object. The possible combinations from the code above are as follows:
Wasn’t that easy? Think about what would happen if you didn’t hard code the value of ‘n’ and didn’t want all the possible combinations that can be derived from list A. Look at the below example to see how this looks:
Instead, why not create a function that gets both the largest and smallest sum of possible combinations of four from list A.
Using just 4 lines of code, you get the answer required.
Assume you have a list where A = [1,3,5,7,9] and you need to find the median value. This simple algorithm involves the following steps:
The above example works, but there’s an easier way to do it. We’ve created an example below that shows you how:
With just one method call and median(array) and this task will be complete, allowing you to focus on improving other aspects of your code. The above example was pretty easy to follow, but what happens if you use an array with an even number of items, e.g. A = [1,3,5,7,9,10]?
You’ll still get a result (which is the average of the two values at the centre, so in this case, ‘5’ and ‘7’), however there are instances where you don’t want this average and instead want one of the mid values. Here, you’ll need to use ‘median_low’ and ‘median_high’ depending on which of the two values you’re looking for.
Python comes with inbuilt mathematical methods that help with performing calculations. We've outlined some examples of what you can do below:
Start by importing the module and getting the Euler number:
Now try to get pi:
Have a go at getting the ‘sin’, ‘cos’ and ‘tan’ of an angle at 60 degrees:
Try and get the natural logarithm of a number:
Now attempt to get the value of 2 to the power of 3:
Having fun? See if you can get the base -10 of a logarithm, or something as simple as the square root of 5:
The math module in particular can assist in many different calculus problems. If you need further inspiration, make sure to run ‘print(dir(math))’.
There’s so much you can do with Python, it’s no wonder that it’s a firm favourite with the engineering team at hackajob. In fact, there’s so many inbuilt modules and tools that we couldn't possibly cover them all in one short article, but we’ve got a challenge for you. Learn at least one new tool each weekend see how you go. In time, you’ll have built up your skills and have a whole library of options in Python to choose from.
Like what you’ve read? Make sure to take a look at our other articles that discuss Python at length.