Adventures in Machine Learning

Mastering Fractions in Python: Basics to Complex Operations

Fractions are a fundamental part of mathematics, and their importance cannot be overstated. They are integral in understanding rational number arithmetic and are essential to advancing to higher-level math topics such as algebra and calculus.

In this article, we will delve into the basics of fractions and how to create them. We will also explore the different arithmetic operations on fractions.

Basics of Fractions Module

Fractions are a way of representing a portion or a part of a whole. They consist of two numbers, a numerator and a denominator, which are separated by a forward slash.

The numerator represents the part or portion of the whole, while the denominator represents the total number of equal parts that make up the whole. For example, in the fraction

3/4, the numerator is

3, which represents three equal parts of the whole, while the denominator is 4, which represents the total number of equal parts that make up the whole.

Creating Fractions

To create a fraction instance in Python, we need to instantiate the fraction class. We can create fractions using integers, floats, decimals, or strings.

Let’s take a look at some examples.

Instantiating Fraction Class

We can import the fractions module in Python using the following command:

“`python

import fractions

“`

Next, let’s instantiate a fraction class using integers, floats, and decimals. “`python

# integer

a = fractions.Fraction(

3, 4)

print(a)

#float

b = fractions.Fraction(0.75)

print(b)

#decimal

c = fractions.Fraction(decimal.Decimal(‘0.75’))

print(c)

“`

The output of the above code would be:

“`python

3/4

3/4

3/4

“`

We can also create a fraction instance using a string. “`python

#string

d = fractions.Fraction(‘

3/4′)

print(d)

“`

The output of the above code would be:

“`python

3/4

“`

Arithmetic Operations on Fractions

Arithmetic operations on fractions are similar to those on integers and decimals. In Python, we can perform addition, subtraction, multiplication, division, and exponentiation on fractions.

Performing Mathematical Operations

Let’s take a look at some examples of performing mathematical operations on fractions. “`python

# addition

a = fractions.Fraction(

3, 4)

b = fractions.Fraction(2,

3)

c = a + b

print(c)

# subtraction

d = a – b

print(d)

# multiplication

e = a * b

print(e)

# division

f = a / b

print(f)

# exponentiation

g = a ** b

print(g)

“`

The output of the above code would be:

“`python

17/12

1/12

1/2

9/8

(27/64)

“`

Fraction Instances

Fraction instances are hashable and immutable, which means that they can be used as keys in dictionaries and elements in sets. They cannot be changed after instantiation.

For example:

“`python

# creating a dictionary with fraction instances as keys

a = fractions.Fraction(1, 2)

b = fractions.Fraction(2,

3)

c = fractions.Fraction(

3, 4)

d = {a: ‘one-half’, b: ‘two-thirds’, c: ‘three-quarters’}

print(d)

# creating a set with fraction instances as elements

e = set([a, b, c])

print(e)

“`

The output of the above code would be:

“`python

{Fraction(1, 2): ‘one-half’, Fraction(2,

3): ‘two-thirds’, Fraction(

3, 4): ‘three-quarters’}

{Fraction(1, 2), Fraction(2,

3), Fraction(

3, 4)}

“`

Conclusion

In conclusion, fractions are an essential part of mathematics, and knowing how to manipulate them is critical for success in higher-level math topics. In this article, we explored the basics of fractions and how to create them.

We also explored the different arithmetic operations on fractions and how to use fraction instances as keys in dictionaries and elements in sets. With this knowledge, you will be able to incorporate fractions into your Python programs with ease and confidence.

Combining Math with Fractions

The math module in Python is a standard library that provides access to mathematical functions that are not part of the core language. Combining fractions with math functions can be useful when dealing with complex arithmetic expressions that involve fractions.

In this section, we will explore how to use the math module with fractions.

Using Math Module with Fractions

We can use the math module with fractions by converting them into floating-point numbers using the `float` function and then using the math functions with the converted numbers. However, this method can result in inaccuracies due to the limitations of floating-point arithmetic.

To avoid this issue, we can use the fractions method to convert the result of the math function back into a fraction instance. “`python

import math

import fractions

# square root

a = fractions.Fraction(10, 1)

b = math.sqrt(float(a))

c = fractions.Fraction(b).limit_denominator()

print(c)

# floor

d = fractions.Fraction(1

3, 4)

e = math.floor(float(d))

f = fractions.Fraction(e)

print(f)

# sine function

g = fractions.Fraction(

3, 4) * math.pi

h = math.sin(float(g))

i = fractions.Fraction(h).limit_denominator()

print(i)

“`

The output of the above code would be:

“`python

628

3

31/141421

3

1/2

“`

Example of

Combining Math with Fractions

Let’s take a look at an example of how to combine fractions with math functions to solve a problem. Suppose we need to find the value of the sine of pi/

3 using fractions.

We can do this by multiplying pi/

3 by

3/4 and then applying the sine function. The code for this would look something like this:

“`python

import fractions

import math

# pi/

3 as a fraction

a = fractions.Fraction(math.pi,

3)

#

3/4 as a fraction

b = fractions.Fraction(

3, 4)

# pi/

3 *

3/4

c = a * b

# sine of pi/

3 *

3/4

d = math.sin(float(c))

# convert result back into a fraction

e = fractions.Fraction(d).limit_denominator()

print(e)

“`

The output of the above code would be:

“`python

419/480

“`

Rounding Off Fractions

Rounding off fractions can be useful when we need to work with fractions that do not have a simple denominator or when we want to simplify the fraction to make it easier to understand. In Python, we can use the `math.ceil` and `math.floor` functions to round fractions up and down, respectively.

Rounding Fractions

Let’s take a look at an example of how to round off fractions using the math module in Python. “`python

import fractions

import math

# a fraction with a large denominator

a = fractions.Fraction(1

327, 20000)

# rounding up using math.ceil

b = math.ceil(float(a))

c = fractions.Fraction(b)

print(c)

# rounding down using math.floor

d = math.floor(float(a))

e = fractions.Fraction(d)

print(e)

“`

The output of the above code would be:

“`python

7/50

1

3

3/2000

“`

In the above example, we have a fraction with a large denominator of 20,000. We use the `math.ceil` function to round the fraction up to the nearest whole number and the `math.floor` function to round the fraction down to the nearest whole number.

Conclusion

In this article, we explored how to combine fractions with mathematical functions using the math module in Python. We also learned how to round off fractions using the `math.ceil` and `math.floor` functions.

With this knowledge, you will be able to incorporate fractions into your mathematical computations with ease and precision. In this article, we have explored the basics of fractions in Python, how to create fraction instances, and how to perform mathematical operations on fractions.

We have also learned how to combine fractions with mathematical functions using the math module and how to round off fractions using the `math.ceil` and `math.floor` functions. In this section, we will provide a summary of the fractions module and its functionalities.

Summary of Fractions Module

The fractions module in Python provides support for rational number arithmetic. The module makes it easy to create fraction instances, perform mathematical operations on them, and convert them to other data types.

The functionalities of the fractions module include:

1. Creating

Fraction Instances: We can create fraction instances using integers, floats, decimals, or strings.

The `fractions.Fraction` class can be instantiated using the numerator and denominator or a string representation of the fraction. 2.

Performing Mathematical Operations: We can perform addition, subtraction, multiplication, division, and exponentiation on fraction instances. The arithmetic operations are similar to those on integers and decimals.

3.

Combining Math with Fractions: We can use the math module with fractions by converting them into floating-point numbers using the `float` function and then using the math functions with the converted numbers.

However, this method can result in inaccuracies due to the limitations of floating-point arithmetic. To avoid this issue, we can use the fractions method to convert the result of the math function back into a fraction instance.

4.

Rounding Off Fractions: We can round off fractions using the `math.ceil` and `math.floor` functions to round fractions up and down, respectively.

This can be useful in simplifying fractions or when dealing with fractions that have a large denominator. In conclusion, the fractions module in Python is a powerful tool for dealing with rational numbers.

Its functionalities range from creating fraction instances to performing mathematical operations on them, combining them with mathematical functions, and rounding off fractions. With this knowledge, you will be able to manipulate fractions effectively in your Python programs and advance to more complex mathematical concepts.

In summary, this article has explored the topic of fractions in Python and how to use the fractions module effectively to create fraction instances, perform mathematical operations, combine fractions with mathematical functions, and round off fractions. The article has emphasized the importance of understanding fractions in mathematics and how they are fundamental in rational number arithmetic.

The key takeaways from the article are that the fractions module provides support for rational number arithmetic, has various functionalities, and is a powerful tool in manipulating fractions in Python. Understanding fractions is essential for success in advanced mathematical concepts such as algebra and calculus.

Therefore, taking the time to grasp the fundamentals of fractions will unlock endless possibilities in math and scientific programming.

Popular Posts