Page 130 - Trackpad_V4.0_c6_Flpbook
P. 130

INPUT AND OUTPUT

              Python provides two commonly used functions, input() and print() for input and output.

              The input() Function

              The input() function takes the user’s input while a program is executing. The general syntax of the
              input() function is as follows:

                input(<prompt>)
              Here, a prompt is the string or message we wish to display on the screen. Example:
                  name = input("Enter your name: ")
                  >>> Enter your name: Mohan
              The name 'Mohan' entered by the user will be stored in the variable 'name'.
              By default, Python considers the input as a string, even if you entered a number as input. You cannot
              perform calculations on a string. To perform calculations, you need to convert the string an into integer
              or floating-point value. To do so, Python provides the following functions:

                   int(): It is used to change the input data value into an integer. For example,
                  a = int(input("Enter a number "))
              Now, the valid value entered is converted into an integer value.

                   float(): It is used to change the input value into a floating-point value. For example,
                  a = float(input("Enter a number "))
              Now, the valid value entered is converted into a floating-point value.

              The print() Function

              The  print()  function  prints  or sends  the  output  to  the  standard  output  device,  which  is  usually
              a monitor. This function auto converts the items to strings, i.e., if you try printing a numeric value,
              the print() function will automatically convert it into an equivalent string and print it. The print() function
              also introduces spaces between items automatically.

              The general syntax of the print() function is as follows:

                  print ([<expression 1> , <expression 2>...])
              For example:
                print ("Python is easy").

                Program 1: To use the input( ) and print( ) functions.

                    Program1.py
                 File  Edit  Format   Run   Options   Window    Help

                 #Program to use the input( ) and
                 print( ) functions.                                          Output
                 age = input ("Enter your age:")                           Enter your age: 12
                 name = input ("Enter your name: ")                        Enter your name: Ankit
                 print (name, "your age is," age)                          Ankit your age is 12







                128     Premium Edition-VI
   125   126   127   128   129   130   131   132   133   134   135