![if !IE]> <![endif]>
Input and Output Functions
A program needs to interact with the user to accomplish the desired task; this can be achieved using Input-Output functions. The input() function helps to enter data at run time by the user and the output function print() is used to display the result of the program on the screen after execution.
In Python, the print() function is used to display result on the screen. The syntax for print() is as follows:
print (“string to be displayed as output ” )
print (variable )
print (“String to be displayed as output ”, variable)
print (“String1 ”, variable, “String 2”, variable, “String 3” ……)
>>>print (“Welcome to Python Programming”) Welcome to Python Programming
>>>x = 5
>>>y = 6
>>>z = x + y
>>> print (“The sum = ”, z)
The sum = 11
>>> print (“The sum of ”, x, “ and ”, y, “ is ”, z)
The sum of 5 and 6 is 11
The print ( ) evaluates the expression before printing it on the monitor. The print () displays an entire statement which is specified within print ( ). Comma ( , ) is used as a separator in print ( ) to print more than one item.
In Python, input( ) function is used to accept data as input at run time. The syntax for input() function is,
Variable = input (“prompt string”)
Where, prompt string in the syntax is a statement or message to the user, to know what input can be given.
If a prompt string is used, it is displayed on the monitor; the user can provide expected data from the input device. The input( ) takes whatever is typed from the keyboard and stores the entered data in the given variable. If prompt string is not given in input( ) no message is displayed on the screen, thus, the user will not know what is to be typed as input.
>>>city=input (“Enter Your City: ”)
Enter Your City:Madurai
>>>print (“I am from “, city)
I am from Madurai
>>> print (I am from", city)
I am from Rajarajan
Note that in example-2, the input( ) is not having any prompt string, thus the user will not know what is to be typed as input. If the user inputs irrelevant data as given in the above example, then the output will be unexpected. So, to make your program more interactive, provide prompt string with input( ).
The input ( ) accepts all data as string or characters but not as numbers. If a numerical value is entered, the input values should be explicitly converted into numeric data type. The int( ) function is used to convert string data as integer data explicitly. We will learn about more such functions in later chapters.
x = int (input(“Enter Number 1: ”))
y = int (input(“Enter Number 2: ”))
print (“The sum = ”, x+y)
Enter Number 1: 34
Enter Number 2: 56
The sum = 90
x,y=int (input("Enter Number 1 :")),int(input("Enter Number 2:"))
print ("X = ",x," Y = ",y)
Enter Number 1 :30
Enter Number 2:50
X = 30 Y = 50
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.