 Let's develop a class that will allow us to manipulate fractions, also known as rational numbers. The book does this in Chapter 17, but I'm going to change things up a bit to fit my programming style. We'll start with a class constructor that constructs a fraction object, which is our init method, and it's going to have two attributes, numerator and denominator. In a design decision that my fingers may come to regret later on, I'll spell out the words completely, and we'll set the attributes with whatever we were provided in the parameters. Now, let's implement the convert to string method, str, which has one parameter, self, and we'll return the numerator converted to a string, plus a slash, plus the denominator converted to a string. In a break from what we did last time, I'm going to save this as fraction.py, and test it from another file called testfraction.py. We'll start by importing fraction, and then create a variable f1, which will be a new fraction with a numerator of 6 and a denominator of 8, and then we'll print it. Let's save that and run it, and we get, as expected, 6 eighths, which is a bit unsatisfying. We'd like to be able to reduce fractions to lowest terms. To do this, we need to divide the numerator and denominator of a fraction by the greatest common divisor. Here's the code for calculating the greatest common divisor. This is unlike the books method in two major ways. First, it doesn't do any division at all. It only does subtraction. This algorithm was developed by an incredibly brilliant man named Eschard W. Dijkstra, and the other big difference is that I'm putting the function inside the class rather than outside as the book does. It's also worth noting that in this definition, there's no mention of self. This method belongs to the class rather than to any individual object that we've constructed. Here's how we're going to call it. Let's set the divisor to be fraction dot capital F fraction, which is the name of our class, and then we can call GCD with 6 and 8, and then let's print the divisor. When we run this, and let's save this first, by the way, and now when we run this, we get 6 eighths, and this common divisor is 2. The next thing I want to write is a method that reduces a fraction to lowest terms. Here's the one that's modeled after the one in the book. I calculate the greatest common divisor, and then I divide the numerator and the denominator using integer division by my greatest common divisor. Let's see it in action. I'm going to simplify F1 and then print F1. And when I run it, F1 has now been changed to 3 fourths. The main point of the book writing it this way was to show that you can modify the attributes of an object in place, and this is quite an ordinary thing to do in Python, and by the way also in Java. However, a programming paradigm called functional programming much prefers that objects stay immutable. So instead, we're going to write a method called reduce that returns a new reduced fraction and leaves the original untouched. It starts out in exactly the same way. We calculate the greatest common divisor. But this time, rather than modify the fields, we're going to create two new variables. The reduced numerator, which is going to be the numerator integer divided by the common divisor, and reduced denominator, which will be the denominator divided by integer division, the common denominator. And now we're going to create a result that has a new fraction that will return. We'll say our result is a new fraction using the constructor with the reduced numerator and the reduced denominator, and that's what we'll return. Let's go back to our test program. In this case, instead of simplifying F1 and changing it, we're going to set F2 to be F1.reduce. Remember, reduce returns a new fraction. If we print F1 and F2, we'll see that F1 stays as 6 eighths, and F2, our new fraction, is the reduced one, 3 fourths. Now let's take on mathematical operations such as adding fractions. Here's a reminder of how it works. A over B plus C over D is equal to AD plus CB divided by BD. In Python, we can't redefine the plus sign operator, so we'll create an ADD method that adds the current fraction, self, to an other fraction. We'll set the result numerator to be the numerator of the fraction we're working with times the other fraction's denominator, plus the other fraction's numerator times our denominator, and the result denominator will be our denominator times the other fraction's denominator. We'll create that new fraction, reduce it to lowest terms, and return that value. Let's rewrite our test program by setting F1 to be 1 fourth, and F2 to be 2 thirds. We'll tell F1 to add itself to F2, with the result going into F3, and print everything out, and 1 fourth plus 2 third is indeed 11 twelfths. So that works. What about subtraction? Well, we're going to cheat a little bit. Instead of going through all that multiplication and division, we'll say that A over B minus C over D is the same as A over B plus negative C over D, so we'll reuse our addition. This makes our code very short. We'll define a new fraction, which is negative the other fraction's numerator and other fraction's denominator. Call that the subtrahend, and then add our current fraction to the subtrahend, and that does our subtraction. In this case, we'll subtract F1 from F2, which will come out to 2 thirds minus 1 fourth, and that comes out to 5 twelfths. And for our last two operations, let's do multiplication, which is a lot easier than addition. We multiply the numerator straight across, multiply the denominator straight across, and division, where we take the reciprocal of the second fraction, and multiply across for the numerators and the denominators. And here, on lines 42 through 48, is the code. You can pause the video and look at the code, and see that this is exactly what we want. And here's our test program, where we multiply 1 fourth by 2 thirds to get 1 sixth, and divide 1 fourth by 2 thirds to get 3 eighths. And that's our fraction class that uses Python objects to manipulate rational numbers or fractions.