Page 246 - Computer Science V2.0 Class 11
P. 246

Python allows us to import together all the functions and other objects (like classes and data) contained in a module
              and then access them directly without referring to the module name, for example,
               >>> from math import *
               >>> pi
                    3.141592653589793
               >>> e
                    2.718281828459045
               >>> sqrt(25)  #outputs
                    5.0


                       Write a function call that would yield value of π rounded to three decimal places. What value will the
                       function call yield?



              Even though importing all the functions of a module (using from <module> import *) appears to be simple, it is
              not usually wise to import all the functions of a module as some of the function names may conflict with the names of
              data objects or functions defined by the programmer. So, if we are only interested in sqrt() and pow() functions,
              we would prefer to write:

               >>> from math import sqrt, pow
              Some commonly used built-in functions of math module are described below:

              1.  math.ceil(num)
                    Function ceil() takes an integer or floating-point number as the input and returns the least integer greater than
                  or equal to the argument. For instance, the following function call returns the smallest integer greater than or
                  equal to 13.3.
               >>> math.ceil(13.3)
                    14
               >>> math.ceil(-13.3)
                    -13
              Note that in the second example, since -14 < -13.3 < -13, thus, the smallest integer greater than -13.3 is -13.



                       What value will the function calls math.ceil(-1.6), math.ceil(-1), math.ceil(12.99),
                       math.ceil(0.86)return?




              2.  math.floor(num)
                    Function floor() takes an integer or floating-point number as the input and returns the largest integer smaller
                  than or equal to the argument. For instance, the following function call returns the largest integer smaller than or
                  equal to 13.3.

               >>> math.floor(13.3)
                    13
               >>> math.floor(-15.4)
                    -16
              Note that in the second example, since -16 < - 15.4 < -15, thus, the largest integer smaller than or equal to -15.4 is -16.




                       What value will the function calls math.floor(-1.6), math.floor(-1), math.floor(12.99),
                       math.floor(0.36)return?






               232   Touchpad Computer Science (Ver. 2.0)-XI
   241   242   243   244   245   246   247   248   249   250   251