- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Decimal Equivalents of 1/2, 1/3, . . . ,1/10 in Python
In this article, we will learn a python program that prints out the decimal equivalents of 1/2, 1/3, 1/4, . . . ,1/10.
Methods Used
The following are the various methods to accomplish this task −
Printing Decimal Equivalent of 1/2
Using the range() function
Using the decimal module
Method 1: Printing Decimal Equivalent of 1/2
We can easily use this code to find the decimal equivalents of 1/2. In this code, we just display the output after storing the result of 1/2 in a variable, inputNumber.
Example
The following program returns the decimal equivalents of 1/2 −
# input number inputNumber = 1/2 # printing the decimal equivalent of the input number print(inputNumber)
Output
On executing, the above program will generate the following output −
0.5
Method 2: Using the range() Function
The range() function returns a sequence of numbers that starts at 0 and increments by 1 (default) and stops before a given number.
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task. −
Initialize a variable with value 1.
Use the for loop to traverse in a range from 2 to 10 with the help of the range() function.
Print 1 (here p) divided by the current number and format it using fstrings.
Example
The following program returns the decimal equivalents of 1/2, 1/3,….,1/10 using the range() function −
# initializing a variable with 1 p = 1 # iterating the for loop in a range from 2 to 10 for q in range(2, 11): # printing 1 (here p) divided by the current number and formatting it print(f"{p}/{q} :", p/q)
Output
On executing, the above program will generate the following output −
1/2 : 0.5 1/3 : 0.3333333333333333 1/4 : 0.25 1/5 : 0.2 1/6 : 0.16666666666666666 1/7 : 0.14285714285714285 1/8 : 0.125 1/9 : 0.1111111111111111 1/10 : 0.1
Method 3: Using the Decimal Module
When working with decimal numbers in Python, one can utilize the decimal module to get more precise/accurate results because it supports arbitrary-precision decimal arithmetic.
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task. −
Use the import keyword to import the decimal module.
Use the for loop to traverse in a range from 2 to 10 with the help of the range() function.
The range() function returns a sequence of numbers that starts at 0 and increments by 1 (default) and stops before a given number.
Use the Decimal() function of the decimal module to get the decimal equivalent of 1 by the current number.
Print the resultant decimal equivalent of the number by formatting it using fstrings.
Example
The following program returns the decimal equivalents of 1/2, 1/3, 1/4,….,1/10 using the decimal module −
# importing decimal module import decimal # iterating the for loop in a range from 2 to 10 for p in range(2, 11): # getting the decimal equivalent of 1 by the current number outputNum = decimal.Decimal(1) / decimal.Decimal(p) # printing the resultant decimal number by formatting it print(f"1/{p}: {outputNum}")
Output
On executing, the above program will generate the following output −
1/2: 0.5 1/3: 0.3333333333333333333333333333 1/4: 0.25 1/5: 0.2 1/6: 0.1666666666666666666666666667 1/7: 0.1428571428571428571428571429 1/8: 0.125 1/9: 0.1111111111111111111111111111 1/10: 0.1
In the above example, the Decimal class represents decimal numbers that are imported from the decimal module. Division can be performed with arbitrary precision using the / operator, which belongs to the Decimal class.
In scenarios when a high level of precision is required, utilizing the decimal module may be more computationally expensive than using the built-in float type.
Conclusion
We learned three distinct techniques in this article for printing out the decimal equivalents of 1/2, 1/3, 1/4,..., and 1/10. We also learned how to iterate through the provided ranges using the range() method. Additionally, we learned how to use the Decimal function to obtain results in floating-point numbers with higher precision and accuracy.