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

The following syntax describes the use of input()function to assign the user input to a variable. input() is used to
                 accept input from the user.
                 <variable> = input([<message-to-prompt-user-to-enter-input>])
                 The input() function reads  the text entered by a user until a newline is encountered. In the above syntax description,
                 input is the keyword and message-to-prompt-user-to-enter-input is the message that is displayed on
                 the screen, followed by the cursor. Note that message-to-prompt-user-to-enter-input is enclosed within
                 square brackets, indicating that its use is optional. Indeed, we have already seen an example of the use of input()
                 function where we did not specify any message-to-prompt-user-to-enter-input. In the above description,
                 variable is to be replaced with a variable name. The function input() accepts user input from the keyboard.
                 On seeing the cursor, the user enters the required inputs and hits the enter key to indicate the end of input. Once
                 the user presses the enter key, the input() function yields the user input, which is assigned to the variable on the
                 left-hand side of the assignment operator. Python considers user inputs as strings. The use of the input() function
                 is illustrated below:

                  >>> rollNo = input('Enter your roll number: ')
                      Enter your roll number: 12
                  >>> name = input('Enter your name : ')
                      Enter your name: Kabir
                  >>> print(rollNo, name)
                      12 Kabir
                 In the above example, the variable rollNo is assigned the value 12 entered by the user. Similarly, the variable name
                 is assigned the value Kabir. As mentioned above, Python interprets the user inputs as strings. Thus, each
                 of the values 12 and Kabir, entered (by the user) is an example of a string. Next, suppose we wish to calculate the
                 perimeter of a rectangle based on the input length and breadth entered by a user.
                  >>> 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
                 6.6.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.



                                                                                         Basics of Python Programming  137
   146   147   148   149   150   151   152   153   154   155   156