Answer the following questions in Python. Do all calculations in Python
Print $\pi/2$ with three digits of precision. Use the pi
constant from the math module
In [3]:
from math import pi
print('{:.3}'.format(pi / 2))
Given a variable called angle
, print out the sine of it like so:
The sine of 1.2 radians is 0.932
Make sure that your code uses a variable, not literally 1.2 radians. Make your precision match what is shown above.
In [15]:
from math import *
x = 1.2
print('The sine of {:.2} radians is {:.2}'.format(x, sin(x)))
Using string formatting show the decimal and binary representation of the number 34. Your format function should only take one argument, the number 34.
In [16]:
print('binary: {0:b}, decimal: {0:}'.format(34))
Using a for
loop, print the integers from 0 to 8 in binary such that each line is right-aligned.
In [17]:
for i in range(9):
print('{:5b}'.format(i))