
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
as_integer_ratio() in Python for reduced fraction of a given rational
In this tutorial, we are going to write a program that returns two numbers whose ratio is equal to the given float value. We have a method called as_integer_ratio() that helps to achieve our goal.
Let's see some examples.
Input: 1.5 Output: 3 / 2 Input: 5.3 Output: 5967269506265907 / 1125899906842624
Let's examine the code.
Example
# initializing the float value float_value = 1.5 # getting integers tuple using the as_integer_ratio() method integers = float_value.as_integer_ratio() # printing the integers print(f'{integers[0]} / {integers[1]}')
Output
If you run the above code, you will get the following results.
3 / 2
Let's see another example.
Example
# initializing the float value float_value = 5.3 # getting integers tuple using the as_integer_ratio() method integers = float_value.as_integer_ratio() # printing the integers print(f'{integers[0]} / {integers[1]}')
Output
If you run the above code, you will get the following results.
5967269506265907 / 1125899906842624
Conclusion
If you have any queries in the tutorial, ask them in the comment section.
- Related Articles
- Can a rational number be expressed a fraction?
- The ratio by mass for hydrogen and oxygen in water is given as 1: 8 respectively. Calculate the ratio by number of atoms for a water molecule
- Find Positive Integer Solution for a Given Equation in C++
- Find the smallest positive integer value that cannot be represented as sum of any subset of a given array in Python
- How to convert a decimal number to a fraction reduced to its lowest form?
- Product of given N fractions in reduced form in C
- Check if the given array can be reduced to zeros with the given operation performed given number of times in Python
- Java Program to Print a Square Pattern for given integer
- Python program to print all distinct elements of a given integer array.
- Maximum rational number (or fraction) from an array in C++
- Decomposing rational number as a sum of rationals in JavaScript
- Write a program in Python to filter only integer elements in a given series
- Express 6.25 as a fraction in the lowest terms.
- Are the following statements true or false?Give reason for your answer.Every integer is a rational number.
- Check if an integer can be expressed as a sum of two semi-primes in Python

Advertisements