A Program needs to interact with end user to
accomplish the desired task, this is done
using Input-Output facility. Input means the data
entered by the user (end user) of the
program.
In python,
we have raw_input() and input (
) function available for Input.
raw_input()
Syntax of raw_input()
is:
x=raw_input (“prompt
message”)
If prompt message is present, it is displayed on
the monitor after which user can provide the data from keyboard. The function
takes exactly what is typed from keyboard, convert it to string and then return
it to the variable on LHS of =.
Example (in interactive mode)
>>>x=raw_input ("Enter your name: ")
Enter your name: ABC
x is a variable which will get the string (ABC),
typed by user during the execution of
program. Typing of data for the raw_input()
function is terminated by pressing enter key.
We can use raw_input() to enter numeric data also.
In that case we typecast, i.e., change
the datatype using function, the string data
accepted from user to appropriate Numeric
type.
Example
y=int(raw_input(“enter
your roll no”))
enter your roll no 5
will convert the accepted string i.e. 5 to integer before
assigning it to y.
input()
Syntax for input() is:
input (“prompt message”)
If prompt is present, it is displayed on monitor,
after which the user can provide data
from keyboard. Input takes whatever is typed from
the keyboard and evaluates it. As
the input provided is evaluated, it expects valid
python expression. If the input provided is not correct then either syntax
error or exception is raised by python.
Example
x=input("Enter data")
Enter data 5
This will set 5 to x
x= input ("Enter data:")
Enter data: 2+1/2.0
Will supply 2.5 to x
i) Exceptions are raised for non-well formed expressions.
ii) Sometimes well formed expression can wreak havoc.
Print Statement
Syntax:
print expression/constant/variable
Print evaluates the expression before printing it on the
monitor. Print statement outputs
an entire (complete) line and then goes to next line for
subsequent output (s). To print
more than one item on a single line, comma (,) may be used.
Example
>>> print “Hello”
Hello
>>> print 5.5
5.5
>>> print 4+6
10
>>>x=12
>>>print “the value of x=”,x
The value of x=12
We can also use C like format strings
>>>x=2.367448
>>>print “x=%.2f” %x
Pretty Post! Thank you so much for sharing this good content, it was so nice to read and useful to improve my knowledge as an updated one, keep blogging.
ReplyDeletePython Certification Training in Electronic City