Python Input And Output - shahzade baujiti

Breaking

Wednesday, April 24, 2019

Python Input And Output

Python Input And Output

Python provides methods that can be used to read and write data. Python also provides supports of reading and writing data to Files.

Python "print" Statement
"print" statement is used to print the output on the screen.print statement is used to take string as input and place that string to standard output.Whatever you want to display on output place that expression inside the inverted commas. The expression whose value is to printed place it without inverted commas.
Syntax:

print "expression" or print expression. 

Exampple
a=10 
print "Welcome to the world of Python" 
print a 

Output:
>>>  
Welcome to the world of Python 
10 
>>>      

Input from Keyboard:
Python offers two built-in functions for taking input from user, given below:

1) input()
2) raw_input()
1) input() functioninput() function is used to take input from the user. Whatever expression is given by the user, it is evaluated and result is returned back.

Python input() Syntax:
1.    input("Expression") 

Python input() Function Example
n=input("Enter your expression "); 
print "The evaluated expression is ", n 

Output:
>>>  
Enter your expression 10*2 
The evaluated expression is  20 
>>>     

Python raw_input()
2) raw_input()raw_input() function is used to take input from the user. It takes the input from the Standard input in the form of a string and reads the data from a line at once.

Syntax:
1.    raw_input(?statement?) 

Python raw_input() Example
n=raw_input("Enter your name "); 
print "Welcome ", n 

Output:
>>>  
Enter your name Rajat 
Welcome  Rajat 
>>>    

raw_input() function returns a string. Hence in case an expression is to be evaluated, then it has to be type casted to its following data type. Some of the examples are given below:

Program to calculate Simple Interest.
prn=int(raw_input("Enter Principal")) 
r=int(raw_input("Enter Rate")) 
t=int(raw_input("Enter Time")) 
si=(prn*r*t)/100 
print "Simple Interest is ",si   

Output:
>>>  
Enter Principal1000 
Enter Rate10 
Enter Time2 
Simple Interest is  200 
>>>   

Program to enter details of an user and print them.
name=raw_input("Enter your name ") 
math=float(raw_input("Enter your marks in Math")) 
physics=float(raw_input("Enter your marks in Physics")) 
chemistry=float(raw_input("Enter your marks in Chemistry")) 
rollno=int(raw_input("Enter your Roll no")) 
print "Welcome ",name 
print "Your Roll no is ",rollno 
print "Marks in Maths is ",math 
print "Marks in Physics is ",physics 
print "Marks in Chemistry is ",chemistry 
print "Average marks is ",(math+physics+chemistry)/3  

Output:
>>>  
Enter your name rajat 
Enter your marks in Math76.8 
Enter your marks in Physics71.4 
Enter your marks in Chemistry88.4 
Enter your Roll no0987645672 
Welcome  rajat 
Your Roll no is  987645672 
Marks in Maths is  76.8 
Marks in Physics is  71.4 
Marks in Chemistry is  88.4 
Average marks is  78.8666666667 
>>> 

Python File Handling
Python provides the facility of working on Files. A File is an external storage on hard disk from where data can be stored and retrieved.

Operations on Files:
1) Opening a File: Before working with Files you have to open the File. To open a File, Python built in function open() is used. It returns an object of File which is used with other functions. Having opened the file now you can perform read, write, etc. operations on the File.

Syntax:
obj=open(filename , mode , buffer)  
here,
filename:It is the name of the file which you want to access.
mode:It specifies the mode in which File is to be opened.There are many types of mode. Mode depends the operation to be performed on File. Default access mode is read.

2) Closing a File:Once you are finished with the operations on File at the end you need to close the file. It is done by the close() method. close() method is used to close a File.
Syntax:
fileobject.close()  

3) Writing to a File:write() method is used to write a string into a file.
Syntax:
1.    fileobject.write(string str) 

4) Reading from a File:read() method is used to read data from the File.
Syntax:
fileobject.read(value) 

here, value is the number of bytes to be read. In case, no value is given it reads till end of file is reached.

Program to read and write data from a file.
obj=open("abcd.txt","w") 
obj.write("Welcome to the world of Python") 
obj.close() 
obj1=open("abcd.txt","r") 
s=obj1.read() 
print s 
obj1.close() 
obj2=open("abcd.txt","r") 
s1=obj2.read(20) 
print s1 
obj2.close() 

Output:
>>>  
Welcome to the world of Python 
Welcome to the world 
>>> 

Attributes of File:
There are following File attributes.

Attribute
Description
Name
Returns the name of the file.
Mode
Returns the mode in which file is being opened.
Closed
Returns Boolean value. True, in case if file is closed else false.

Example
obj = open("data.txt", "w") 
print  obj.name 
print  obj.mode 
print  obj.closed 

Output:
>>>  
data.txt 

False 
>>> 

Modes of File:
There are different modes of file in which it can be opened. They are mentioned in the following table.

A File can be opened in two modes:

1) Text Mode.
2) Binary Mode.

Mode
Description
R
It opens in Reading mode. It is default mode of File. Pointer is at beginning of the file.
rb
It opens in Reading mode for binary format. It is the default mode. Pointer is at beginning of file.
r+
Opens file for reading and writing. Pointer is at beginning of file.
rb+
Opens file for reading and writing in binary format. Pointer is at beginning of file.
W
Opens file in Writing mode. If file already exists, then overwrite the file else create a new file.
wb
Opens file in Writing mode in binary format. If file already exists, then overwrite the file else create a new file.
w+
Opens file for reading and writing. If file already exists, then overwrite the file else create a new file.
wb+
Opens file for reading and writing in binary format. If file already exists, then overwrite the file else create a new file.
a
Opens file in Appending mode. If file already exists, then append the data at the end of existing file, else create a new file.
ab
Opens file in Appending mode in binary format. If file already exists, then append the data at the end of existing file, else create a new file.
a+
Opens file in reading and appending mode. If file already exists, then append the data at the end of existing file, else create a new file.
ab+
Opens file in reading and appending mode in binary format. If file already exists, then append the data at the end of existing file, else create a new file.

Methods:
There are many methods related to File Handling. They are given in the following table:

There is a module "os" defined in Python that provides various functions which are used to perform various operations on Files. To use these functions 'os' needs to be imported.

Method
Description
rename()
It is used to rename a file. It takes two arguments, existing_file_name and new_file_name.
remove()
It is used to delete a file. It takes one argument. Pass the name of the file which is to be deleted as the argument of method.
mkdir()
It is used to create a directory. A directory contains the files. It takes one argument which is the name of the directory.
chdir()
It is used to change the current working directory. It takes one argument which is the name of the directory.
getcwd()
It gives the current working directory.
rmdir()
It is used to delete a directory. It takes one argument which is the name of the directory.
tell()
It is used to get the exact position in the file.

1) rename():
Syntax:
os.rename(existing_file_name, new_file_name) 

eg:
import os 
os.rename('mno.txt','pqr.txt') 

2) remove():
Syntax:
1.    os.remove(file_name) 

eg:
1.    import os 
2.    os.remove('mno.txt') 

3) mkdir()
Syntax:
os.mkdir("file_name")

eg:
import os 
os.mkdir("new") 

4) chdir()
Syntax:
os.chdir("file_name")

Example
import os 
os.chdir("new") 

5) getcwd()
Syntax:
os.getcwd()

Example
import os 
print os.getcwd() 

6) rmdir()
Syntax:
os.rmdir("directory_name)

Example
import os 
os.rmdir("new") 

NOTE: In order to delete a directory, it should be empty. In case directory is not empty first delete the files.

No comments:

Post a Comment