Page 308 - Artificial Intellegence_v2.0_Class_9
P. 308

Commands                                          Output

              print("hello")                                                hello
              print()                                                       friends

              print("friends")
              print("hello", end="%")                                       hello%friends

              print("friends")
              print("hello", "friends", "welcome", sep="#",                 hello#friends#welcome@
              end="@")
              print("I am in class", 9)                                     I am in class 9
              a = 5

              b = 10                                                        Sum of two numbers is 15
              print("Sum of two numbers is " + str(a + b))

              age = 14                                                      I am 14 years old
              print("I am ", age, " years old")


                 The input() Function

        The input() function is used to take an input from the user. When, this statement executes then the flow of
        the program stops and waits with the cursor blinking for the user to enter a value. The input given by the user
        is always a string value in Python.

        The syntax to use the input() function is:

            input(prompt)
        Where, prompt is a string that works as a message for the user at the time of input. For example,
           name = input("enter your name: ")

          print(name)
        When the preceding statement executes, it will ask to enter name.  When you enter the name and press the Enter
        key, entered name will be displayed on the screen:

          enter your name: Chirag
          Chirag
        Let us take another example:

          a = input("enter first number")                         #line 1
          b = input("enter second number")                        #line 2
          c = a + b
          print("the sum is", c)
        If we execute the preceding code and enter 4 and 5 as the values of the first and second numbers, then the
        answer will be 45 instead of 9. This is because Python considers every input value as string. If we want to perform
        mathematical calculations, then we have to convert the taken input explicitly by using int( ) or float(  )
        functions. The syntax to convert an input value into an integer or float type is:

          a = int(input(prompt))                                  # to convert into integer
          b = float(input(prompt))                                # to convert into float

              306     Touchpad Artificial Intelligence (Ver. 2.0)-IX
   303   304   305   306   307   308   309   310   311   312   313