Skip to main content

Input and Output


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

Comments

Popular posts from this blog

Strings in Python

Strings are compound data type made of sequence of characters. Hence from the string, the individual charters can also be directly accessed. Strings are created by using single quotes or double quotes or triple quotes.
Example: >>>s1="Python Programming" >>>s2='Python Programs' >>>s3="""Python is a powerful Programming language"""
This will create a string object s1, s2 and s3 using different forms.
Individual characters can be accessed using different subscripts( +ve or –ve) with the string object.
Example >>>s="Python" >>>s[0] ‘P’ >>>s[2] ‘t’ >>>s[5] ‘n’ >>>s[-1] ‘n’ >>>s[-3] ‘h’ >>>s[-6] ‘P’
We can also slice a string. Segment of a string is called a slice.
>>>s[1:3] ‘yt’
If the first index is not mentioned, the slicing will start from 0. >>>s[:3] ‘Pyt’
>>>s[2:6] ‘thon’
If the last index is not mentioned, the slicing will go till the end o…

Files in Python , Exception handling

While a program is running, its data is in main memory. When the program ends, or the computer shuts down, data in memory disappears. To store data permanently, you have to put it in a file. Files are usually stored on a secondary storage device(hard disk, pen drive, DVD,CD etc). When there are a large number of files, they are often organized into directories (also called “folders”). Each file is identified by a unique name, or a combination of a file name and a directory name. By reading and writing files, programs can exchange information with each other and generate printable formats like PDF. Working with files is a lot like working with books. To use a book, you have to open it. When you’re done, you have to close it. While the book is open, you can either write in it or read from it. In either case, you know where you are in the book. Most of the time, you read the whole book in its natural order, but you can also skip around. All of this applies to files as well. To open a fil…

Classes and Objects in Python

Python is an object-oriented programming language, which means that it provides features that support object-oriented programming. The basic components of object oriented programming are classes and objects. A Class is a blue print to create an object. It provides the definition of basic attributes and functions of objects. Object is a running instance of the class having the identity(name), properties( values) and behaviors(functions). The Object oriented program thus consist of object definitions (classes) and most of the computations and functions are mentioned as operations on the object. Each object definition corresponds to some object or concept in the real world, and the functions that operate on these object correspond to the ways real-world objects interact. We have learned objects of string, list, tuple etc…and used the properties and functionalities of these objects which are built into the Python. Now we are going to create our own(user defined) objects. The class provides basic…