Input
In order to enter a real number using the input () function, it is necessary to convert the character string (which is the result of the function
\(input()\)) into a real number using the function
\(float()\):
x = float(input())
If you need to enter several values from one line at once, then we use the same method as for integers:
x, y = map(float, input().split())
Output
When outputting real numbers, 16 decimal places are displayed by default. This format is not always necessary. If you want to reduce the output format, then format output is used. To do this, the
format method is applied to the string that we want to output. And inside the line formats are written in braces after the colon
Example:
x=1/6
print("{:f}".format(x)) # format f prints 6 digits in fractional part by default
print("{:.3f}".format(x)) # .3 indicates that it is necessary to print 3 characters after the period
print("{:12.4e}".format(x)) #12.4 - the first number (12) specifies the total number of positions to display the number, the second (4) - the number of digits in the fractional part. Format e - displays a number in scientific format
The screen displays
0.166667
0.167
1.6667e-01