Loading video player…

Beginning Math-Related Built-Ins

Resources mentioned in this lesson:

00:00 In the previous lesson, I gave an overview of the course. This lesson is the first in a multipart section on mathematic and numeric functions. Python comes with lots of stuff, and the built-in functions are those which you can use without importing anything else.

00:15 They’re part of the global namespace; you can call them anywhere in your code. I’m not sure whether you could say they’re part of the standard library or not.

00:23 On one hand, they do ship with Python like the standard library, but on the other hand, they’re often spoke of as their own thing. While I’m on the topic of philosophy, some of the built-in functions aren’t actually functions, so we’re definitely in loose terminology land.

00:37 I’ll talk more about that in a future lesson. As I said, I’m going to start out with some of the math built-in functions, and these include things like how to get an absolute value, doing division to get remainders and quotients, and determining the min and max of collections.

00:54 I’m going to open up the Python REPL by running Python without any arguments and demonstrate these functions for you.

01:01 The absolute value of a number removes any negative sign, so the absolute value of -5 is 5, and likewise, the absolute value of 5 is also 5. Python’s abs() function performs this for you.

01:16 There’s a negative,

01:19 and the corresponding positive. It also works on floating points.

01:24 Python is actually pretty rich when it comes to types of numbers. In addition to the regular built-in integer and floating-point types, there are others as well.

01:34 A complex number contains two components, the real and imaginary parts.

01:41 These kinds of numbers are common in physics and engineering when you need to deal with vectors, and they are how you represent the square root of a negative number.

01:49 To get a complex number in Python, you use the complex constructor, passing in a string like I’ve done here. This one has a real part of -2 and an imaginary part of 3.

02:00 If you’re used to seeing i for imaginary, that’s okay, you probably came from the math side of things. The engineering side typically uses j. By the time imaginary numbers were common in engineering, i was already used for electrical current.

02:14 You can, of course, take the absolute value of a complex number. The absolute value of a complex number is a little more complicated than just removing the sign from the real part.

02:26 It actually is equivalent to determining the length of the vector. This is like using the Pythagorean theorem on the real and imaginary parts to calculate the hypotenuse.

02:37 And since it is absolute, changing the sign of the real component results in the same answer. The complex number constructor is built in, so I’ll actually be talking about it specifically in a later lesson.

02:48 To get at other numeric types, you need to import them. They’re part of the standard library, just not in the built-in namespace. Floating-point numbers are actually imprecise representations of fractions.

02:59 In fact, you can’t store all decimal numbers as float, and that’s without getting into the fact that some fractions repeat forever when you turn them into decimals. For a more precise representation of fractions, you can actually use the fractions module.

03:15 This is the Fraction class, which I can then use to construct a fraction object by passing a string into the constructor.

03:24 This is similar to how the complex constructor works. This is minus one half, so of course I can get an absolute value for it.

03:34 And as you might expect, the negative number goes away. Since floats are imprecise, they’re a bad choice for dealing with things like money. If you’re not convinced, try running 0.1 plus 0.2 in the REPL and see what comes out.

03:48 Instead, for money, you should use the Decimal class, which is a precise decimal implementation. Like the Fraction, I need to import it.

04:02 And of course, I can use abs() on it.

04:07 Python has two kinds of division operators. A single slash for floating-point division, and a double slash for integer division. The integer division operation just throws away the remainder though. So instead, if you want that information, you can use the built-in divmod() function.

04:26 divmod() returns a tuple where the first parameter is the quotient and the second is the remainder. This is the equivalent of integer division for the first part and taking the mod for the second. divmod() also supports floats.

04:45 This is subtly different because the value of the quotient that comes back is a float rather than an integer, which kind of makes sense. The built-in min() and max() functions find the minimum and maximum values in an iterable.

05:04 And they behave kind of like you would expect. In addition to using an iterable, you can just pass in multiple arguments if you prefer. Both functions also optionally take a keyword named default.

05:21 This specifies a value to return if there is no min or max, basically what to do if the collection is empty.

05:31 You need this if you want a default value to come back, because normally without it, you get an exception. The key keyword parameter allows you to run a function on an argument before performing the comparison.

05:44 The function that you run needs to be one that takes a single argument and returns a modified value. For example, I can combine max() with absolute value. And the result is the largest absolute value.

06:02 Note that the value in the iterable gets returned, not the thing computed by the key function.

06:09 If you’d like to learn more about the min() and max() functions, there is a detailed video course and tutorial available. They take you past the quick bit I’ve shown you here and include lots of practical examples.

06:22 Next up, I’ll continue with even more math-related built-in functions.

Become a Member to join the conversation.