Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 91 - 2615_KEC
P. 91

    The output is displayed in the Python Shell window.

                               INPUT AND OUTPUT

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

                     input() FUNCTION
                     We use the input() function to take the user’s input while a program is being executed. (This
                     function also evaluates the expression whether the user has entered a string, number or list right
                     after receiving input.) The general syntax of the input() function is as follows:

                     Variable_name = input(<message to be displayed>)
                     Example:
                     name = input("Enter your name: ")

                     Enter your name: Trackpad
                     By default, Python considers the input as string even if you entered a number as input. You cannot
                     perform calculations on string. To perform calculations, you need to convert the string into integer
                     or floating-point value.

                     To do so, Python provides following functions:
                       int(): It is used to change the input data value into integer. For example,

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

                     print() FUNCTION

                     We use the print() function to display the result on the screen. The content is written within the
                     parenthesis. To get the output, follow the given steps:
                     Step  1   Open the Python Shell window.

                     Step  2    In  front of the command prompt,  type  print("Hello  Students")  and press the  Enter
                               key. Do not forget to include the
                               double quotes (" ").

                     Step  3   The output  is  displayed  in  the
                     next line in the same Python Shell window.

                                                                                                 Introduction to Python  89

| 86 87 88 89 90 91 92 93 94 95 96 |