Page 68 - Information_Practice_Fliipbook_Class11
P. 68

>>> length = input('Enter length of rectangle: ')
              Enter length of rectangle: 6
         >>> breadth = input('Enter breadth of rectangle: ')
              Enter breadth of rectangle: 4
         >>> perimeter = (length + breadth)*2
         >>> perimeter
              '6464'
        Recall that Python considers the user inputs entered interactively as strings. So, the variables length and breadth
        were assigned the values '6' and '4' respectively. Therefore, (length + breadth) resulted in the string '64'.
        Finally, (length + breadth)*2 resulted in '6464'. However, we were interested in the perimeter of the rectangle.
        So, we need to convert length and breadth to numeric values before applying the arithmetic operations. This
        can be done using the function int() as follows:

         >>> perimeter = (int(length) + int(breadth))*2
         >>> perimeter
              20
        Alternatively, we could transform the string inputs to integer values while reading from the user as follows:

         >>> length = int(input('Enter length of rectangle: '))
             Enter length of rectangle: 6
         >>> breadth = int(input('Enter breadth of rectangle: '))
             Enter breadth of rectangle: 4
         >>> perimeter = (length + breadth)*2
         >>> perimeter
             20
        3.10.2 print()


        As the computer screen is the default device for displaying the results, the output produced on invoking the  print()
        function is displayed on the screen.

         >>> print('Hello')
             Hello
        In the above example, 'Hello' is the argument passed to the function print(). The function print() may be
        invoked with an arbitrary number of arguments. For example, the following function call makes use of two arguments:

         >>> print('Hello!', 'How are you?')
              Hello! How are you?
        Next, we make use of print() function to display the value of an arithmetic expression,
         >>> print(2+14)
              16
        In the above example, the print() function accepts the argument  2+14, evaluates it, and displays the result of
        the evaluation (16) on the screen. More generally, we may invoke  the print() function with several arguments
        separated by commas. For instance,
         >>> print('Sum of',  4, 'and',  5, 'is', 4+5)
              Sum of 4 and 5 is 9
        When no arguments are specified, the function print() does not output anything, as shown below:

         >>> print()
         >>>
        Next, we would like to mention a refinement of the syntax for using the print() function as follows:

        Syntax:
        print(value, [,…,] [, sep = <separator>])


          54   Touchpad Informatics Practices-XI
   63   64   65   66   67   68   69   70   71   72   73