Loading video player…

Continuing Math-Related Built-Ins

Resources mentioned in this lesson:

00:00 In the previous lesson, I showed you built-in functions for absolute values, division, and min and max. In this lesson, I’ll show you even more math-related built-in functions.

00:10 The three built-ins I’ll cover here are dedicated to calculating powers, rounding numbers, and summing up iterables. Back to the REPL to see more built-in functions.

00:22 I’ll start with the built-in for calculating mathematical powers, also known as exponents. It’s shortened to pow(), and it takes two arguments, the base number and the exponent to apply to it. There’s also a short form for this in Python, the double star operation.

00:38 Although double star does the same thing as the pow() function, there are times when you might need a function instead of the operators. I’ll show you some of that later.

00:46 The round() function performs rounding on a number. You can also optionally provide a precision argument to specify how many decimal places.

00:55 Anybody hungry?

00:58 How about some pi?

01:02 The second argument specifies how many decimal places to round to.

01:08 You have to be a little careful with the round() function. It may not work the way you’re used to.

01:18 Rounding is actually a more complicated topic than you might first guess, and there are a number of different ways to implement rounding. You typically want a mechanism that has as little bias in it as possible, meaning the distribution of the rounded numbers should be the same as the distribution of the originals.

01:35 Say I had a function that always rounded up. Rounding up on 1.5 gives 2, while rounding up on -1.5 gives -1, which actually has shifted the distribution of the numbers.

01:48 Performing that operation on a large set of numbers shifts the weight of them in the positive direction. Python uses a mechanism called rounding half to even, which conforms to the IEEE standard 754, which specifies how floats should work.

02:03 To try to mitigate the shifting I spoke about, values that are exactly midway don’t just round up like you were taught in school, but round up or down based on the least significant digit.

02:14 A decimal part of .5 is exactly halfway, so when I rounded 3.5, it looks at the 3, which is odd, and it rounds it up. But when I rounded 2.5, the midway tie gets broken in the other direction and rounds down. This is actually good for the math, but it can be a little counterintuitive as it’s different from what you may have learned in grade school.

02:35 There’s a good deep dive article on all the different ways of rounding that I’ll share with you after I’m done in the REPL here. On to our next built-in, the sum() function. It does what you might think, it sums an iterable.

02:49 1 plus 2 plus 3 gives you 6. I even didn’t use my fingers.

02:55 Unlike with min() and max(), you can pass in an empty iterable, giving you a sum of 0.

03:00 sum() also supports an optional keyword, start, which specifies a starting value for the sum. It defaults to 0, but you can set it to whatever you like.

03:13 This shows our previous 6 plus the starting value of 100. Also, unlike min() and max(), sum() requires an iterable, you can’t just pass numbers as arguments.

03:23 Although that makes it a little inconsistent, the advantage is you don’t have to name the start keyword, you can just pass it in as a value.

03:31 sum() will also work with other data types that you can perform addition on. For example, you can sum a list of lists, which will concatenate the inner lists together.

03:40 This works because when you add two lists, it puts them together. But I would recommend avoiding this, it’s less performant than just using the add operator, and it’s kind of confusing to read in your code.

03:52 You saw how rounding in Python isn’t in the form you most likely learned in school. If you’d like to learn more about this, including different ways of rounding and how they introduce rounding bias, this video course and tutorial are worth checking out.

04:06 Or, if you’d like to see more examples on how to use sum(), you could try this video or tutorial.

04:12 That’s it for the math bits. Next up, I’ll start a new section on how to deal with a variety of data types.

Become a Member to join the conversation.