Python

# Python Fractions

“Python” is a high-level and interpreted language of the computer system. It is widely used because of its readability. A fraction in python is not a built-in function. So, we have to import it for usage from the python library when performing the fractions. The fractions in python work with the arithmetic of rational numbers. We can work with floating numbers, integer values, string values, and decimal numbers in python fractions. The most popular built-in function of python we will use for the fraction’s calculation performance will be the “math” function. We will be discussing the fractions in python below in this article with examples.

## The Numerator and Denominator are Important for Creating the Fractional Instance:

Some built-in functions will help in the calculation performance of fractions, which are the numerator and the denominator. The fractions calculation is established on the importance of the denominator and numerator. The numerator value should be “0” and the denominator value should be “1”. The setting is done by default already. The vice versa will raise the error, especially the “0” in the denominator. The error sometimes came up as an “error” or simply a “0” will occur on the display screen.

## Syntax:

“From fractions import as frac

Print ( frac ( value ) ) ”

The above syntax is used for python fractions. As “fractions” is not built-in python, we have to import it. In the first line of the syntax, we have imported the fractions first for every function application. The second line of the syntax has the print function with the “frac”, which is used for the calculation of the fraction. Then, the value is assigned according to the need.

## Implementation of the Python Fractions Examples:

The following are the examples we will cover for a better perspective of the usage of fractions in python:

## Example #01: Executing Floating Point Value on Python Fractions

In this instance, we will be employing the floating-point value on the python fractions. The floating-point numbers are those which have the decimal point usually. First, we will import the fractions as “frac” for the performance of the fractions. Here, we have taken the floating-point number “3.2” as an argument. It will convert the value calculation into the fractions term as numerator and denominator. The output displays the floating point resulting in the value of the fraction successfully. ## Example #02: Executing Square Root Value on Python Fractions

Here, we will be performing the square root value on python fractions. The square root calculation will be done with the help of the “sqrt()”. The sqrt() is a function that is built in the python library. We will be using the “math” library, which is also a built-in function of python. The math function is used for computational calculations. For using the Math function in python, you have to import it as “import math”. Here, in this example, we are calculating the square root of the numbers ”85” and “78”. The output of the square root calculation is shown below: ## Example #03: Executing Floor Function on Python Fractions

In this instance, we will be employing the floor function on the python fractions. The floor value works in a way that gives results similar to rounding off the value. Let us clarify the concept by taking an example. A number 5.7 is closer to 6 than the number 5. So, the floor value here will be “6”. We have imported the “math” function as well for rounding off the number. We have to use the floor function with Math by adding the number as an argument in fractions. We have taken the number here as “88/3” for the execution of the floor value. The actual result of the number “88/3” is “29.33” which means it is closest to the number “29”. We can see the calculated floor value using the floor function below in the output screen. ## Example #04: Executing Mathematical Functions on Python Fractions

In this illustration, we will be looking at the performance of the mathematical functions on python fractions. The mathematical functions on which we will be working are “addition”, “subtraction”, “multiplication” and “division”. Let’s see their execution with examples.

## Performing Subtraction on the Python Fractions

Here, we will be performing the subtraction method on the python fractions. There are always two operands on which the subtraction is performed. For the calculation of the operands, we use the subtraction operator “-“. To show the output we will use the print function, which will display the results of the subtraction performed. We have imported the fractions module as “frac” and will pass the integers into it. The numbers we have taken for executing the subtraction function are “3/7” and “5/4”. The output shows the subtraction performed on the fractions in python. ## Performing Addition on the Python Fractions

Here, we will be performing the addition of the fractions in python. We will be applying the same method as we performed above in subtraction. Just the operator “+” will be used for addition. Importing the module of fractions as “frac” and passing the numbers as arguments. The numbers used for the performance of addition here are “8/5” and “4/1”. Then, the print function is used for printing the display on the screen. The results show the addition performed using the addition on the fractions in python. ## Performing Division on the Python Fractions

Here, we will be executing division on the python fractions. It uses the “/” operator for performing the division. The output result will always be in an integer value and does not contain any point values. The division result uses the floor function technique, which we have discussed above in example 3, ignoring the decimal points. The numbers used for the division performance are “58/53” and “32/23”. The output shows the division performed of fractions in python ## Performing Multiplication on the Python Fractions

In this part of example 4, we will be implementing the multiplication of the python fractions. As we have done in all arithmetic operations, the same approach will be used here. The “*” operator is used for multiplying the operands together. The numbers for the multiplication performance here are taken as an argument “74/63” and “55/45”. Here is the result shown of the multiplication performed of fractions in python. ## Conclusion

The fractions in python are a very useful method used in machine language and high-level mathematical calculations in computing. We talked about all of the important points and how the usage of fractions can be done. We have done the floating-point performance of fractions in python, the floor function of fractions in python, the square root of fractions in python, and the arithmetic operation of fractions in python which includes the implementation of each operation separately like addition, and subtraction, multiplication, and division. 