Page 113 - 2622_Delhi Police Public School_C-6
P. 113

Saving Python Program  INPUT AND OUTPUT

 Follow these steps to save a Python program:  Python provides two commonly used functions, input() and print() for input and output.

 3  Select the desired location   The input() Function
 to save the file.
                 The input() function takes the user’s input while a program is executing. The general syntax of the

 1  Click on the   input() function is as follows:
 File menu.        input(<prompt>)

                 Here, a prompt is the string or message we wish to display on the screen. Example:
                    name = input("Enter your name: ")
 Click on the Save      >>> Enter your name: Mohan
 2  option. The Save As
 dialog box appears.   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,
 4  Enter the name of the file in   5  Click on  Save
 the File name box.   button.  a = int(input("Enter a number "))
                 Now, the valid value entered is converted into an integer value.
 The file will be saved by the given name at the selected location.
                     float(): It is used to change the input value into a floating-point value. For example,
 Running a Python Program  a = float(input("Enter a number "))

 Follow these steps to run a Python program:  Now, the valid value entered is converted into a floating-point value.


 1  Click on Run in   2  The print() Function
 the Menu bar.  Click on Run Module option.
                 The  print()  function  prints  or sends  the  output  to  the  standard  output  device,  which  is  usually
 Hintbot         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
 Press F5 key to run a   also introduces spaces between items automatically.
 program in Python.
                 The general syntax of the print() function is as follows:


 The program will execute and the output will be shown in the Shell window.     print ([<expression 1> , <expression 2>...])
                 For example:
 Factbot           print ("Python is easy").

                  Program 1: To use the input( ) and print( ) functions.
 Interactive Mode is very useful for testing
     a code as it displays the errors one by one.  Program1.py

                   File  Edit  Format   Run   Options   Window    Help

                   #Program to use the input( ) and
                   print( ) functions.                                          Output
 Imagine, comments in Python are secret messages from a wise mentor. What advice or tips   age = input ("Enter your age:")  Enter your age: 12
 would you leave in your code for future programmers to discover?  name = input ("Enter your name: ")  Enter your name: Ankit
                   print (name, "your age is," age)                           Ankit your age is 12






                                                                                                         Python    111
   108   109   110   111   112   113   114   115   116   117   118