Page 237 - Ai_V1.0_Class9
P. 237

You can get the data type of any object by using type( ) function. For example,

                   >>> type(10)
                   <class 'int'>
                   >>> type("abc")
                   <class 'str'>
                   >>> type(14.5)
                   <class 'float'>

                         Comments in Python

                 Comments are used to increase the readability of the code. We use them to give a proper understanding of the
                 code in simple English statements. They are completely ignored by the Python interpreter. It is always a good
                 practice to add comments in your code so that if anybody in the future wishes to understand or modify your
                 code, then through comments it will be easy to interpret the instructions. There are two different ways of writing
                 comments in Python. Let us learn about them in detail.

                 Single Line Comment

                 Single line comment starts with hash symbol # followed by the text to be written as a comment that lasts until
                 the end of the line.

                 For example,

                 # assigning a value to a variable
                       num1 = 10
                       num2 = 20
                 # calculating the average

                       (num1 + num2) / 2

                 Multiple Line Comments
                 When we add up comments which occupy two or more lines then we begin the comment with either 3 times
                 single quotes ''' or double quotes """, followed by text on multiple lines and end with single quotes ''' or double
                 quotes """ to mark the end of the comment. For example,

                   """This program calculates
                       the average of

                       two numbers stored in
                       two different variables"""
                       a = 10
                       b = 20
                       c = (a + b) / 2


                         The print() Function

                 The print() function is used to print an output on the screen. It converts the expressions into a string before
                 writing to the screen.




                                                                                        Introduction to Python  235
   232   233   234   235   236   237   238   239   240   241   242