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

11.   round(num[, digits])            Rounds  the given input  >>> round(10.6)
                                                         number  num to  the nearest       11
                                                         integer or up to the specified   >>> round(13.8907, 2)
                                                         precision  (digits)  after       13.89
                                                         the decimal point.           >>> round(-12.0456, 3)
                                                                                          -12.046
                 8.1.2 Type Conversion Functions
                 Python provides several built-in functions for type conversion, for example, int, float, and str. The data types
                 in Python are also called classes. To determine the type of any data element, we may use type function as follows:

                  >>> type(234)
                      <class 'int'>
                 Thus, we may say 234 is an object of type int, or 234 is an object of class int, or 234 is an instance of class int.
                 Similarly, note that 234.50 and '234.50' are objects of type float and str, respectively.
                  >>> type(234.50)
                      <class 'float'>
                  >>> type('234.50')
                      <class 'str'>
                 Python allows us to transform objects of certain data types to other data types which are compatible for conversion,
                 for example,
                  >>> int('234')
                      234
                  >>> str(234)
                      '234'
                  >>> float('234.50')
                      234.5
                  >>> int(234.50)
                      234
                 Note that '234', 234, '234.50', and 234.50 are arguments for the functions int, str, float, and
                 int, respectively.




                         What value does int(12.53) yield?




                 8.2 User-defined Functions

                 Suppose you wish to write a program (also called a Python script or simply a script) to print a triangle, followed by a
                 rhombus, followed by a triangle again. Program 8.1 achieves this objective in the following steps:
                 1.  Printing a triangle (lines 02–05)
                 2.  Leaving a blank line (line 06)

                 3.  Printing a rhombus (lines 08–14)
                 4.  Leaving a blank line (line 15)
                 5.  Printing a triangle (lines 17–20)
                 Note that line numbers are not part of the program and have been mentioned to facilitate the discussion. Also, note
                 that each of lines 01, 07, and 16 that begins with the symbol # comprises only a comment line. While executing the
                 Python code, the interpreter ignores the comments. Thus, execution of the program begins with line 02, followed by
                 execution of statements in lines 03, 04, 05, 06, 08, 09, 10, 11, 12, 13, 14, 15, 17, 18, 19, and 20.


                                                                                             Introduction to Functions  197
   206   207   208   209   210   211   212   213   214   215   216