Page 133 - Computer Science Class 11 Without Functions
P. 133

Consider the following line beginning # that appears as the first line in Program 6.1:

            #Objective: To experiment with variables and their values
            The above line is an example of a comment. It describes the purpose of the program. As shown above, a comment in
            a line in a Python program begins with the symbol hash (#). As a comment beginning with the hash symbol extends
            up to the end of the line in which the symbol # appears, it is also known as a single-line comment.  Being a comment,
            it was ignored by the Python interpreter during the program execution. A comment may also appear at the end of an
            executable statement. For example,

            num = 20 # assigns the value 20 to num
            However, a comment may also be a multi-line string, enclosed within triple single quotes or triple double quotes. For
            example, the first four lines in the following code snippet specify the purpose of the code:


            Program 6.4 Write a program to use the working of Multiple Line comment.
              01 '''
              02 Objective: To compute area of rectangle
              03 Output: Rectangle's area computed using length and breadth
              04 '''
              05 length, breadth = 10, 5
              06 area = length*breadth
              07 print('length of rectangle = ', length)
              08 print('breadth of rectangle = ',breadth)
              09 print('area of rectangle = ',area)

            6.6 Input and Output

            A computer program will often require inputs from the user, process them, and produce an output for the user. In
            Python, the input() and print() functions are used to perform the input and output operations, respectively.
            6.6.1 input()

            The input() function reads the text entered by a user until a newline is encountered. Suppose we wish to fetch the
            name of a programming language as an input from a user. For this purpose, the function input() may be invoked as
            follows:
             >>> language = input()
                 Python
            On execution of the above statement, Python prompts the user for the name and waits for the input. When the user
            enters the string, Python and hits the enter key, Python assigns the string Python  to the variable language, as
            verified below:
             >>> language
                 'Python'
            The above use of the input() function seems simple and devoid of any problems. If we write a small program that
            needs only one or two inputs and execute it soon after we are done with the program, then this approach works fine.
            But real-life programs may require several inputs and they may be executed days, months, or years after they are
            developed. In such scenarios, it is nearly impossible to remember the inputs required by the program. In fact, most
            of the Software is used by people without having had a role in developing it. Therefore, it is good practice to display a
            suitable message indicating the inputs required, as illustrated below:

             >>> input('Enter  name of a Programming language: ')
                 Enter name of a Programming language: Python
                 'Python'
             >>> language = input('Enter name of a Programming language: ')
                 Enter name of a Programming language: Python
             >>> language
                 'Python'

                                                                                    Basics of Python Programming  131
   128   129   130   131   132   133   134   135   136   137   138