Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 89 - 2617_JSSPS_C-8
P. 89

    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

                                                                                               Introduction to Python   87

| 84 85 86 87 88 89 90 91 92 93 94 |