Page 390 - Ai_417_V3.0_C9_Flipbook
P. 390

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. Syntax to use the print( ) function is:

              print(object(s), sep = separator, end = end)
              Where,
                 • object can be one or more separated by comma and it can be a variable, literal, expression. An object will be
                 converted to string before printed.
                 • sep parameter specifies the separator between the items you want to print. By default, the separator is a space.
                 You can change this to any string you want.
                 • end parameter allows you to specify what to print at the end of the output. By default, end is set to a newline
                 character ('\n'), which means each print statement will end with a new line.

              Following are some of the examples of the print( ) function:

                                            Commands                                          Output

                    print("hello")                                                hello
                    print("hello", "friends")                                     hello friends
                    print("hello" + "friends")                                    hellofriends

                    (This is concatenation of string using "+"
                    operator)

                    print("hello", "friends", sep="$")                            hello$friends


                    388     Touchpad Artificial Intelligence (Ver. 3.0)-IX
   385   386   387   388   389   390   391   392   393   394   395