Page 455 - ComputerScience_Class_11
P. 455

iNtroductioN to PytHoN







                                                                 data ProceSSiNg iN PytHoN
















                        Learning Objectives


                    12.1  Accepting User Input with input()
                    12.2  Displaying Output with print()
                    12.3  Understanding Errors in Python




                 A program isn't useful if it works alone. To create interactive and dynamic applications, a program needs to communicate
                 with the outside world. This process is called Input/Output or I/O. The simplest way to achieve this is through the
                 console (also known as the terminal or command line).
                 Console I/O involves two main actions:

                 •  Input: Accepting data from the user via the keyboard.
                 •  Output: Displaying text, results or prompts to the user on the screen.
                 This text-based interface, known as a Command Line Interface (CLI), is a powerful tool for building applications, from
                 simple scripts to complex software. In this chapter, we will focus on two essential functions for console I/O in Python:
                 input() and print().


                     12.1 ACCEPTING USER INPUT WITH INPUT()
                 The primary way to get data from the user into your Python program is through the built-in input() function. When
                 this function is called, the program pauses its execution and waits for the user to type something on the keyboard. The
                 user's input is then passed back to your program.


                 12.1.1 Using input() for Strings
                 The input()  function can accept an optional string argument, known as a prompt. It helps the user understand
                 what type of information they should enter. By default, any value entered by the user using the input() function is of
                 string data type.
                 The syntax for the input() function is:

                    variable = input(prompt)
                 Variable: The variable stores the value entered by the user.







                                                                                           Data Processing in Python  453
   450   451   452   453   454   455   456   457   458   459   460