Python Input and Output Operations (I/O) with Examples

Output Formatting Using print() Function

Having seen how to print numbers, let us now see how to format the program’s output to specify the number of digits after the decimal point.

What is Output Formatting 

Output formatting is done to make the output more intelligible and easier to understand for the reader. Other than making the output more intelligible and look fancier, the output formatting specifies the number of digits after the decimal point.

What Is The Reason To Do Output Formating

Without output formatting, Python will output the results of computations in a simple space-separated value. Such ouputs are hard to read and understand.

In addition to making the program’s output more informative, there are other reasons to format the output.

To explain formatting, we will write programs to format the outputs to make them more informative to the reader.

Floating Point Math

In programs involving floating-point (meaning numbers with decimal points), the program generates the results with a large number of digits after the decimal point. Generally, such a high degree of precision is not required for most applications. Therefore, the programmers would specify the number of digits after the decimal point to be printed in the output. This is done by output formatting.

Since Python outputs the floating-point number with up to 15 digits after the decimal point, output formatting will limit the number of digits after the decimal point to what is required for a particular application.

As an example, the price of an item that you buy in a store or on-line, has two digits after the decimal point suxh as like $12.99.

As another example, in a sports competition, if you are running a 100-meter dash, the referee will measure the runner’s time to thousandths of a second, which is three digits after the decimal point.

High Precision Math

On the other extreme, if you were a scientist working on the atomic structure of various elements and need to measure the diameter of a hydrogen atom, you would need a lot more digits after the decimal point. This is so because the size of the hydrogen atom is very small. The hydrogen atom size is 1.06 X 10**(-10) meters. 

In scientific notation, we write it as 1.06e-10. 

In decimal notation, the Hydrogen atom size is 0.000000000106 meters.

So, we see that different numbers of digits after the decimal point are required to be printed for different applications.  Fortunately, python provides us with a neat way to specify the number of digits we would like to print after the decimal point.

Format the Output to Specify the Number of Digits After the Decimal Point

To specify the number of digits to be printed after the decimal point, the argument within the parenthesis of the print() function should be a string

that specifies the number of total digits (count decimal as one digit) to be printed, followed by a decimal,

The decimal point is followed by the number of digits after the decimal point, followed by the letter ‘f’.  

This string is enclosed in quotes. This string is followed by the variable name to be printed, preceded by the symbol %.

Let us explain this using an example: If we want to print the value of pi to three decimal places, we write the following statement:

>>> print(‘%5.3f’ %pi)

In this example,

5 is the total number of digits to be printed. The decimal is also counted as one digits.

3 is the total number of digits after the decimal point.

pi is the name of the variable, whose value is to be printed.

The program below shows how you can specify the number of digits to be printed.

PROGRAM EXAMPLE: PRINTING NUMBERS WITH SPECIFIED NUMBER OF DIGITS AFTER THE DECIMAL POINT
>>> # Define variable ‘pi ‘and assign it a value of 3.14159
>>> pi = 3.14159
>>> print('The value of pi to three decimal places is %5.3f' %pi)
The value of pi to three decimal places is 3.142
>>>
>>> # Define a variable ‘recurring number’ and assign it a value of 1/7
>>> recurring_number = 1/7
>>> # Let’s us print the recurring number (1/7) to 10 digits after the decimal point.
>>> print('The value of 1/7 to ten decimal places is %12.10f' %recurring_number)
The value of 1/7 to ten decimal places is 0.1428571429
>>> # Let us now print 1/7 to 12 decimal places
>>> print('The value of 1/7 to 12 decimal places is %14.12f' % recurring_number)
The value of 1/7 to 12 decimal places is 0.142857142857
>>> # Print the diameter of hydrogem atom and format the output with 13 digits after the decimal.
>>> # 15 total digits including the decimal and 13 digits after the decimal point.
>>> Hydrogen_atom_diameter = 1.06e-10
>>> print('%15.13f' %Hydrogen_atom_diameter)
0.0000000001060
>>>

The next program example shows the use the use of math module to print the value of pi, and format the output to print the output to three decimal places.

PROGRAM EXAMPLE: PRINT FORMATING – Use Math Module

>>> import math
>>> print(‘The value of pi for three decimal digits is %5.3f. ‘ % math.pi)
The value of pi for three decimal digits is 3.142.
>>>

Note in the above program, we need to first import the math module and then use the function math.pi.

More information in output formating at this link.

Enrichment: Number Theory

We will introduce two types of numbers – Rational Numbers and Irrational numbers.

Rational Numbers:

A rational number can be written as the ratio of two integers. Some rational numbers have a fixed number of digits after a decimal point, while others have repeating numbers after the decimal point.  This repetition goes on forever.  This is called a recurring decimal.   Some examples of rational numbers are:

1/4 = 0.25 (In this case, the number of digits after the decimal point finishes at two digits.)

1/3 = 0.33333333……(Repetition of ‘3’ goes on until infinity).

1/9 = 0.1111111…..   (Repetitions of ‘1’ goes on until infinity).

1/7 = 0.142857142857…(The pattern ‘142857’ repeats forever).

Irrational Number:

An irrational number is a real number that cannot be written as the ratio of two integers. Some Famous Irrational Numbers are:

Pi (π): The mathematical constant, represented by Greek letter π (also spelled as pi), is an irrational number. It is defined as the ratio of a circle’s circumference to its diameter. The circumference of a circle is the length (perimeter) of the curve of the circle. 

The number π is an irrational number, meaning it cannot be expressed as a common fraction. Its decimal representation never ends, that is, it has an infinite number of digits in its decimal representation. It is approximately equal to 3.14159. It can be approximated by a fraction such as 22/7.

The ratio of circumference/diameter (C/D) is constant, regardless of the circle’s size, and is equal to π. For example, if a circle has twice the diameter of another circle, it will also have twice the circumference, preserving the ratio C/D.

Math Applications - Input and Output Circle, Diameter, Circumference, Radius, Center, and pi (π)
Circle Circumference, Diameter, Radius, and Center

Square Root of 2 is another irrational number and = 1.4142…..

Square Root of 3 is another example of irrational number and = 1.732…..

The Golden Ratio

The Golden Ratio is another irrational number.  Golden Ratio is denoted by the Greek letter Phi (uppercase Φ, lowercase φ) (pronounced phi). If we take any two successive Fibonacci Numbers, their ratio is very close to the Golden Ratio. Golden Ration is approximately 1.61805….(See the link below for details on Fibonacci Numbers in Maththe Applications chapter.

More on rational and irrational numbers:

<a href=”https://www.math.net/irrational-numbers“>

Copyright © 2019 – 2024 softwareprogramming4kids.com

All Rights Reserved

Verified by MonsterInsights